From 1747c138b4860d03850b03af11583ead8b6e4db4 Mon Sep 17 00:00:00 2001 From: Carlos Santos <4a.santos@gmail.com> Date: Tue, 8 Apr 2025 09:54:33 +0200 Subject: [PATCH] backend: Simplify lock duration calculation in TaskSchedulerService and remove unused cron interval method --- .../src/services/task-scheduler.service.ts | 24 +------------------ 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/backend/src/services/task-scheduler.service.ts b/backend/src/services/task-scheduler.service.ts index 0ce13ca..b967a6b 100644 --- a/backend/src/services/task-scheduler.service.ts +++ b/backend/src/services/task-scheduler.service.ts @@ -5,7 +5,6 @@ import { CronJob } from 'cron'; import { MutexService } from './mutex.service.js'; import { MeetLock } from '../helpers/redis.helper.js'; import ms from 'ms'; -import { CronExpressionParser } from 'cron-parser'; export type TaskType = 'cron' | 'timeout'; @@ -65,7 +64,7 @@ export class TaskSchedulerService { if (type === 'cron') { this.logger.debug(`Scheduling cron task "${name}" with schedule "${scheduleOrDelay}"`); const cronExpression = this.msStringToCronExpression(scheduleOrDelay); - const lockDuration = this.getCronIntervalDuration(cronExpression); + const lockDuration = Math.max(ms(scheduleOrDelay) - ms('1m'), ms('59s')); const job = new CronJob(cronExpression, async () => { try { @@ -123,27 +122,6 @@ export class TaskSchedulerService { } } - protected getCronIntervalDuration(cronExpression: string): number { - try { - // Parse the cron expression using cron-parser - const interval = CronExpressionParser.parse(cronExpression); - - // Get the next interval time - const next = interval.next().getTime(); - - // Get the current time - const afterNext = interval.next().getTime(); - - // Calculate the interval duration in milliseconds - const intervalMs = afterNext - next; - // Return the interval duration minus 1 minute for ensuring the lock expires before the next iteration - return Math.max(intervalMs - ms('1m'), ms('10s')); - } catch (error) { - this.logger.error('Error parsing cron expression:', error); - throw new Error('Invalid cron expression'); - } - } - /** * Converts a human-readable time string to a cron expression. *