UNPKG

@hotmeshio/hotmesh

Version:

Permanent-Memory Workflows & AI Agents

41 lines (40 loc) 1.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CronHandler = void 0; const cron_parser_1 = require("cron-parser"); const enums_1 = require("../../../modules/enums"); const utils_1 = require("../../../modules/utils"); const logger_1 = require("../../logger"); /** * Safely calculates the delay in seconds until the next execution of a cron job. * Fails silently and returns -1 if the cron expression is invalid. * @param cronExpression The cron expression to parse (e.g. '0 0 * * *'). * @returns The delay in seconds until the next cron job execution (minimum 5 seconds). */ class CronHandler { nextDelay(cronExpression) { try { if (!(0, utils_1.isValidCron)(cronExpression)) { return -1; } const interval = (0, cron_parser_1.parseExpression)(cronExpression, { utc: true }); const nextDate = interval.next().toDate(); const now = new Date(); const delay = (nextDate.getTime() - now.getTime()) / 1000; if (delay <= 0) { return -1; } if (delay < enums_1.HMSH_FIDELITY_SECONDS) { return enums_1.HMSH_FIDELITY_SECONDS; } const iDelay = Math.round(delay); return iDelay; } catch (error) { CronHandler.logger.error('Error calculating next cron job execution delay:', { error }); return -1; } } } exports.CronHandler = CronHandler; CronHandler.logger = new logger_1.LoggerService('hotmesh', 'cron');