const cron = require('node-cron') const Property = require('../components/property/propertyModel') const propertyService = require('../components/property/propertyService') const { client } = require('../setup/typesense') let lastIndexedAt = new Date(0) let lastIndexedCount = null let isIndexing = false const checkAndIndexProperties = async () => { if (isIndexing) { console.log('[Property Index Cron] Indexing already in progress. Skipping this run.') return } isIndexing = true try { const mongoCount = await Property.countDocuments({ isActive: true }) // Check Typesense collection document count let typesenseCount = 0 let collectionExists = false try { const collection = await client.collections('properties').retrieve() typesenseCount = collection.num_documents || 0 collectionExists = true } catch (err) { collectionExists = false } // Count new or updated properties in MongoDB since last index const query = { isActive: true, $or: [ { updatedAt: { $gt: lastIndexedAt } }, { createdAt: { $gt: lastIndexedAt } } ] } const changedCount = await Property.countDocuments(query) const shouldIndex = !collectionExists || (typesenseCount === 0 && mongoCount > 0) || mongoCount !== typesenseCount || (lastIndexedCount !== null && mongoCount !== lastIndexedCount) || changedCount > 0 if (shouldIndex) { console.log( `[Property Index Cron] Changes detected (MongoDB: ${mongoCount}, Typesense: ${typesenseCount}, New/Updated: ${changedCount}). Starting indexing...` ) const result = await propertyService.indexAllProperties() lastIndexedAt = new Date() lastIndexedCount = mongoCount console.log(`[Property Index Cron] Successfully indexed ${result.totalCount} properties.`) } else { console.log( `[Property Index Cron] No new or updated properties found since ${lastIndexedAt.toISOString()} (MongoDB: ${mongoCount}, Typesense: ${typesenseCount}). Skipping.` ) } } catch (err) { console.error('[Property Index Cron] Error during property indexing check:', err) } finally { isIndexing = false } } const startPropertyIndexCron = () => { // Schedule every 5 minutes const task = cron.schedule('*/5 * * * *', async () => { console.log('[Property Index Cron] Running scheduled 5-minute property index check...') await checkAndIndexProperties() }) console.log('[Property Index Cron] Scheduled to run every 5 minutes (*/5 * * * *)') // Initial check after startup setTimeout(() => { checkAndIndexProperties().catch((err) => { console.error('[Property Index Cron] Initial index check failed:', err) }) }, 10000) return task } module.exports = { startPropertyIndexCron, checkAndIndexProperties, }