2023-03-22 01:30:04 -04:00
|
|
|
const { MeiliSearch } = require('meilisearch');
|
2023-12-14 07:49:27 -05:00
|
|
|
const Conversation = require('~/models/schema/convoSchema');
|
2024-05-02 08:48:26 +02:00
|
|
|
const Message = require('~/models/schema/messageSchema');
|
2023-12-14 07:49:27 -05:00
|
|
|
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
|
|
|
|
2024-05-02 08:48:26 +02:00
|
|
|
class MeiliSearchClient {
|
|
|
|
static instance = null;
|
|
|
|
|
|
|
|
static getInstance() {
|
|
|
|
if (!MeiliSearchClient.instance) {
|
|
|
|
if (!process.env.MEILI_HOST || !process.env.MEILI_MASTER_KEY) {
|
|
|
|
throw new Error('Meilisearch configuration is missing.');
|
|
|
|
}
|
|
|
|
MeiliSearchClient.instance = new MeiliSearch({
|
|
|
|
host: process.env.MEILI_HOST,
|
|
|
|
apiKey: process.env.MEILI_MASTER_KEY,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return MeiliSearchClient.instance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2024-05-02 08:48:26 +02:00
|
|
|
const client = MeiliSearchClient.getInstance();
|
2023-03-22 01:30:04 -04:00
|
|
|
|
|
|
|
const { status } = await client.health();
|
2024-05-02 08:48:26 +02:00
|
|
|
if (status !== 'available' || !process.env.SEARCH) {
|
2023-03-22 01:30:04 -04:00
|
|
|
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;
|
|
|
|
|
2024-05-02 08:48:26 +02:00
|
|
|
logger.debug(`[indexSync] There are ${messageCount} messages and ${messagesIndexed} indexed`);
|
|
|
|
logger.debug(`[indexSync] There are ${convoCount} convos and ${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-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;
|