mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
more descriptive errors
This commit is contained in:
@@ -1,26 +1,21 @@
|
||||
import { CronJob } from 'cron';
|
||||
import { Err, Ok, type Result } from 'ts-results-es'
|
||||
export class TaskScheduler {
|
||||
private __tasks: Map<string, CronJob> = 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<void, string> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user