Skip to content

Commit

Permalink
more descriptive errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed Jul 5, 2024
1 parent 210aa41 commit 8709720
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
21 changes: 8 additions & 13 deletions src/core/schedule.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/handlers/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

}

0 comments on commit 8709720

Please sign in to comment.