scheduler ids

This commit is contained in:
Jacob Nguyen
2024-07-05 14:46:31 -05:00
parent c252854954
commit 210aa41c7e
6 changed files with 59 additions and 19 deletions

View File

@@ -4,6 +4,7 @@ import type {
InputCommand,
InputEvent,
Module,
ScheduledTask,
} from '../types/core-modules';
import { partitionPlugins } from './functions'
import type { Awaitable } from '../types/utility';
@@ -46,3 +47,8 @@ export function discordEvent<T extends keyof ClientEvents>(mod: {
return eventModule({ type: EventType.Discord, ...mod, });
}
export function scheduledTask(i : ScheduledTask) {
return i
}

View File

@@ -2,16 +2,24 @@ import { CronJob } from 'cron';
export class TaskScheduler {
private __tasks: Map<string, CronJob> = new Map();
scheduleTask(taskName: string, cronExpression: string, task: () => void): boolean {
scheduleTask(taskName: string, cronExpression: string | Date, task: () => void, tz: string| undefined): boolean {
if (this.__tasks.has(taskName)) {
console.warn("While scheduling a task",
"found another task of same name. Not scheduling",
taskName, "again");
return false;
}
try {
const job = new CronJob(cronExpression, task);
const job = CronJob.from({
cronTime: cronExpression,
onTick: task,
timeZone: tz
});
job.start();
this.__tasks.set(taskName, job);
return true;
} catch (error) {
console.error("While scheduling a task " + error);
return false;
}
}