From 6c1bb2fa3dce99a8ae1570ee85d949184f519577 Mon Sep 17 00:00:00 2001 From: devthejo Date: Wed, 23 Jul 2025 15:20:13 +0200 Subject: [PATCH] fix(cron): decouple cron from notification window --- .../src/watchers/geodata-cleanup-cron.js | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/services/watchers/src/watchers/geodata-cleanup-cron.js b/services/watchers/src/watchers/geodata-cleanup-cron.js index 92cadbf..4cae5c2 100644 --- a/services/watchers/src/watchers/geodata-cleanup-cron.js +++ b/services/watchers/src/watchers/geodata-cleanup-cron.js @@ -9,7 +9,7 @@ const { } = require("~/constants/time") const tasks = require("~/tasks") -const CLEANUP_CRON = "0 9-19 * * *" // Run every hour from 9h to 19h +const CLEANUP_CRON = "0 */4 * * *" // Run every 4 hours const MAX_PARALLEL_PROCESS = 10 const COLDGEODATA_DEVICE_KEY_PREFIX = "device:geodata:" const COLDGEODATA_OLD_KEY_PREFIX = "old:device:geodata:" @@ -30,6 +30,13 @@ module.exports = async function () { return async function geodataCleanupCron() { logger.info("watcher geodataCleanupCron: daemon started") + // Helper function to check if current time is within notification window (9-19h) + function isWithinNotificationWindow() { + const now = new Date() + const hour = now.getHours() + return hour >= 9 && hour < 19 + } + // Process geodata cleanup with single loop for both notifications and cleanup async function processGeodataCleanup() { const now = Math.floor(Date.now() / 1000) // Current time in seconds @@ -107,7 +114,7 @@ module.exports = async function () { ) } } - // Handle notification (36h+ but less than 48h) + // Handle notification (36h+ but less than 48h) - only during 9-19h window else if (age > notificationAge) { const notifiedKey = `${COLDGEODATA_NOTIFIED_KEY_PREFIX}${deviceId}` @@ -115,7 +122,7 @@ module.exports = async function () { // Check if we've already notified for this device const alreadyNotified = await redisCold.exists(notifiedKey) - if (!alreadyNotified) { + if (!alreadyNotified && isWithinNotificationWindow()) { // Enqueue task to notify user about lost background geolocation try { await addTask(tasks.BACKGROUND_GEOLOCATION_LOST_NOTIFY, { @@ -134,6 +141,14 @@ module.exports = async function () { } // Mark as notified with 48h expiry (cleanup age) await redisCold.set(notifiedKey, "1", "EX", cleanupAge) + } else if ( + !alreadyNotified && + !isWithinNotificationWindow() + ) { + logger.debug( + { deviceId, age: `${Math.floor(age / 3600)}h` }, + "Skipping notification outside business hours (9-19h)" + ) } } catch (error) { logger.error(