mirror of
https://github.com/sern-handler/handler
synced 2026-06-19 22:32:14 +00:00
scheduler ids
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user