diff --git a/src/core/schedule.ts b/src/core/schedule.ts index 966da128..30c10c50 100644 --- a/src/core/schedule.ts +++ b/src/core/schedule.ts @@ -1,26 +1,21 @@ import { CronJob } from 'cron'; +import { Err, Ok, type Result } from 'ts-results-es' export class TaskScheduler { private __tasks: Map = new Map(); - scheduleTask(taskName: string, cronExpression: string | Date, task: () => void, tz: string| undefined): boolean { + scheduleTask(taskName: string, cronExpression: string | Date, task: () => void, tz: string| undefined): Result { if (this.__tasks.has(taskName)) { - console.warn("While scheduling a task", - "found another task of same name. Not scheduling", - taskName, "again"); - return false; + return Err("while scheduling a task \ + found another task of same name. Not scheduling " + + taskName + "again." ); } try { - const job = CronJob.from({ - cronTime: cronExpression, - onTick: task, - timeZone: tz - }); + const job = CronJob.from({ cronTime: cronExpression, onTick: task, timeZone: tz }); job.start(); this.__tasks.set(taskName, job); - return true; + return Ok.EMPTY; } catch (error) { - console.error("While scheduling a task " + error); - return false; + return Err(`while scheduling a task ${taskName} ` + error); } } diff --git a/src/handlers/tasks.ts b/src/handlers/tasks.ts index 1f5a80f4..c869e93d 100644 --- a/src/handlers/tasks.ts +++ b/src/handlers/tasks.ts @@ -14,16 +14,14 @@ export const registerTasks = async (tasksPath: string, deps: UnpackedDependencie //module.name is assigned by Files.importModule<> // the id created for the task is unique - const uuid = module.name!+":"+relative(tasksPath,fileURLToPath(f)) - taskManager.scheduleTask(uuid, module.pattern, function(this: CronJob) { + const uuid = module.name!+"/"+relative(tasksPath,fileURLToPath(f)) + taskManager.scheduleTask(uuid, module.pattern, function(this: CronJob) { module.execute({ deps, runningTasks: taskManager.tasks(), lastTimeExecution: this.lastExecution, nextTimeExecution: this.nextDate().toJSDate() }) - }, - module.timezone) + }, module.timezone).unwrap() } - }