2023-03-22 01:30:04 -04:00
|
|
|
const { MeiliSearch } = require('meilisearch');
|
2023-12-14 07:49:27 -05:00
|
|
|
const Message = require('~/models/schema/messageSchema');
|
|
|
|
|
const Conversation = require('~/models/schema/convoSchema');
|
|
|
|
|
const { logger } = require('~/config');
|
|
|
|
|
|
2023-08-24 21:59:11 +02:00
|
|
|
const searchEnabled = process.env?.SEARCH?.toLowerCase() === 'true';
|
2023-12-14 07:49:27 -05:00
|
|
|
let currentTimeout = null;
|
2023-03-22 01:30:04 -04:00
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
|
async function indexSync(req, res, next) {
|
2023-08-24 21:59:11 +02:00
|
|
|
if (!searchEnabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 01:30:04 -04:00
|
|
|
try {
|
2023-07-08 11:41:51 -04:00
|
|
|
if (!process.env.MEILI_HOST || !process.env.MEILI_MASTER_KEY || !searchEnabled) {
|
2023-03-22 09:38:38 -04:00
|
|
|
throw new Error('Meilisearch not configured, search will be disabled.');
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 01:30:04 -04:00
|
|
|
const client = new MeiliSearch({
|
|
|
|
|
host: process.env.MEILI_HOST,
|
2023-07-14 09:36:49 -04:00
|
|
|
apiKey: process.env.MEILI_MASTER_KEY,
|
2023-03-22 01:30:04 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { status } = await client.health();
|
2023-12-14 07:49:27 -05:00
|
|
|
// logger.debug(`[indexSync] Meilisearch: ${status}`);
|
2023-03-22 01:30:04 -04:00
|
|
|
const result = status === 'available' && !!process.env.SEARCH;
|
|
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
|
throw new Error('Meilisearch not available');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const messageCount = await Message.countDocuments();
|
|
|
|
|
const convoCount = await Conversation.countDocuments();
|
|
|
|
|
const messages = await client.index('messages').getStats();
|
|
|
|
|
const convos = await client.index('convos').getStats();
|
|
|
|
|
const messagesIndexed = messages.numberOfDocuments;
|
|
|
|
|
const convosIndexed = convos.numberOfDocuments;
|
|
|
|
|
|
2023-12-14 07:49:27 -05:00
|
|
|
logger.debug(
|
|
|
|
|
`[indexSync] There are ${messageCount} messages in the database, ${messagesIndexed} indexed`,
|
|
|
|
|
);
|
|
|
|
|
logger.debug(
|
|
|
|
|
`[indexSync] There are ${convoCount} convos in the database, ${convosIndexed} indexed`,
|
|
|
|
|
);
|
2023-03-22 01:30:04 -04:00
|
|
|
|
|
|
|
|
if (messageCount !== messagesIndexed) {
|
2023-12-14 07:49:27 -05:00
|
|
|
logger.debug('[indexSync] Messages out of sync, indexing');
|
2023-07-28 11:46:15 -04:00
|
|
|
Message.syncWithMeili();
|
2023-03-22 01:30:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (convoCount !== convosIndexed) {
|
2023-12-14 07:49:27 -05:00
|
|
|
logger.debug('[indexSync] Convos out of sync, indexing');
|
2023-07-28 11:46:15 -04:00
|
|
|
Conversation.syncWithMeili();
|
2023-03-22 01:30:04 -04:00
|
|
|
}
|
|
|
|
|
} catch (err) {
|
2023-12-14 07:49:27 -05:00
|
|
|
// logger.debug('[indexSync] in index sync');
|
2023-03-22 17:15:32 -04:00
|
|
|
if (err.message.includes('not found')) {
|
2023-12-14 07:49:27 -05:00
|
|
|
logger.debug('[indexSync] Creating indices...');
|
2023-03-23 13:16:07 -04:00
|
|
|
currentTimeout = setTimeout(async () => {
|
2023-03-22 17:15:32 -04:00
|
|
|
try {
|
|
|
|
|
await Message.syncWithMeili();
|
|
|
|
|
await Conversation.syncWithMeili();
|
|
|
|
|
} catch (err) {
|
2023-12-14 07:49:27 -05:00
|
|
|
logger.error('[indexSync] Trouble creating indices, try restarting the server.', err);
|
2023-03-22 17:15:32 -04:00
|
|
|
}
|
|
|
|
|
}, 750);
|
2023-12-15 15:47:40 -05:00
|
|
|
} else if (err.message.includes('Meilisearch not configured')) {
|
|
|
|
|
logger.info('[indexSync] Meilisearch not configured, search will be disabled.');
|
2023-03-22 17:15:32 -04:00
|
|
|
} else {
|
2023-12-14 07:49:27 -05:00
|
|
|
logger.error('[indexSync] error', err);
|
2023-03-22 17:15:32 -04:00
|
|
|
// res.status(500).json({ error: 'Server error' });
|
|
|
|
|
}
|
2023-03-22 01:30:04 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 13:16:07 -04:00
|
|
|
process.on('exit', () => {
|
2023-12-14 07:49:27 -05:00
|
|
|
logger.debug('[indexSync] Clearing sync timeouts before exiting...');
|
2023-03-23 13:16:07 -04:00
|
|
|
clearTimeout(currentTimeout);
|
|
|
|
|
});
|
|
|
|
|
|
2023-03-22 01:30:04 -04:00
|
|
|
module.exports = indexSync;
|