From 2014e0ea8edacd530c48913ea535305a979fc241 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Sat, 6 Jul 2024 14:12:03 -0500 Subject: [PATCH] refactor n better signatures for task scheduler --- src/core/modules.ts | 5 ++- src/core/structures/default-services.ts | 46 ++++++++++++++----------- src/handlers/event-utils.ts | 2 +- src/handlers/tasks.ts | 15 ++------ src/types/core-modules.ts | 4 +-- 5 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/core/modules.ts b/src/core/modules.ts index 81ffbab..24c34d8 100644 --- a/src/core/modules.ts +++ b/src/core/modules.ts @@ -47,8 +47,7 @@ export function discordEvent(mod: { return eventModule({ type: EventType.Discord, ...mod, }); } - -export function scheduledTask(i : ScheduledTask) { - return i +export function scheduledTask(ism: ScheduledTask) { + return ism } diff --git a/src/core/structures/default-services.ts b/src/core/structures/default-services.ts index c4126ad..4c7cf27 100644 --- a/src/core/structures/default-services.ts +++ b/src/core/structures/default-services.ts @@ -1,4 +1,5 @@ -import type { LogPayload, Logging, ErrorHandling } from '../interfaces'; +import { ScheduledTask } from '../../types/core-modules'; +import type { LogPayload, Logging, ErrorHandling, Disposable } from '../interfaces'; import { CronJob } from 'cron'; /** @@ -41,21 +42,28 @@ export class DefaultLogging implements Logging { } -export class TaskScheduler { - private __tasks: Map = new Map(); +export class TaskScheduler implements Disposable { + private __tasks: Map> = new Map(); - schedule(taskName: string, cronExpression: string | Date, task: () => void, tz: string| undefined) { - if (this.__tasks.has(taskName)) { + schedule(uuid: string, task: ScheduledTask, deps: Dependencies) { + if (this.__tasks.has(uuid)) { throw Error("while scheduling a task \ - found another task of same name. Not scheduling " + - taskName + "again." ); + found another task of same name. Not scheduling " + + uuid + "again." ); } try { - const job = CronJob.from({ cronTime: cronExpression, onTick: task, timeZone: tz }); - job.start(); - this.__tasks.set(taskName, job); + const onTick = async function(this: CronJob) { + task.execute({ + deps, id: uuid, + lastTimeExecution: this.lastExecution, + nextTimeExecution: this.nextDate().toJSDate() + }) + } + const job = CronJob.from({ cronTime: task.trigger, onTick, timeZone: task.timezone }); + job.start(); + this.__tasks.set(uuid, job); } catch (error) { - throw Error(`while scheduling a task ${taskName} ` + error); + throw Error(`while scheduling a task ${uuid} ` + error); } } @@ -69,17 +77,15 @@ export class TaskScheduler { return false; } - private restartTask(taskName: string): boolean { - const job = this.__tasks.get(taskName); - if (job) { - job.start(); - return true; - } - return false; - } - get tasks(): string[] { return Array.from(this.__tasks.keys()); } + + dispose() { + for(const [id,] of this.__tasks){ + this.kill(id); + this.__tasks.delete(id); + } + } } diff --git a/src/handlers/event-utils.ts b/src/handlers/event-utils.ts index a6a4089..fbe2565 100644 --- a/src/handlers/event-utils.ts +++ b/src/handlers/event-utils.ts @@ -236,7 +236,7 @@ export async function callInitPlugins(_module: Module, deps: Dependencies, emit? return module } -async function callPlugins({ args, module, deps, params }: ExecutePayload) { +export async function callPlugins({ args, module, deps, params }: ExecutePayload) { let state = {}; for(const plugin of module.onEvent??[]) { const result = await plugin.execute(...args, { state, deps, params, type: module.type }); diff --git a/src/handlers/tasks.ts b/src/handlers/tasks.ts index a722df4..cf01605 100644 --- a/src/handlers/tasks.ts +++ b/src/handlers/tasks.ts @@ -1,26 +1,17 @@ import * as Files from '../core/module-loading' import { UnpackedDependencies } from "../types/utility"; -import { ScheduledTask } from "../types/core-modules"; +import type { ScheduledTask } from "../types/core-modules"; import { CronJob } from "cron"; import { relative } from "path"; import { fileURLToPath } from "url"; export const registerTasks = async (tasksPath: string, deps: UnpackedDependencies) => { - const taskManager = deps['@sern/scheduler'] for await (const f of Files.readRecursive(tasksPath)) { let { module } = await Files.importModule(f); - //module.name is assigned by Files.importModule<> // the id created for the task is unique - const uuid = module.name!+"/"+relative(tasksPath,fileURLToPath(f)) - taskManager.schedule(uuid, module.trigger, function(this: CronJob) { - module.execute({ - deps, - id: uuid, - lastTimeExecution: this.lastExecution, - nextTimeExecution: this.nextDate().toJSDate() - }) - }, module.timezone) + const uuid = module.name+"/"+relative(tasksPath,fileURLToPath(f)) + taskManager.schedule(uuid, module, deps) } } diff --git a/src/types/core-modules.ts b/src/types/core-modules.ts index 3b7d302..f03b795 100644 --- a/src/types/core-modules.ts +++ b/src/types/core-modules.ts @@ -224,7 +224,7 @@ export interface SernSubCommandGroupData extends BaseApplicationCommandOptionsDa } -interface ScheduledTaskContext { +export interface ScheduledTaskContext { /** * An object of dependencies configured in `makeDependencies` */ @@ -243,10 +243,10 @@ interface ScheduledTaskContext { nextTimeExecution: Date | null; } + export interface ScheduledTask { name?: string; trigger: string | Date; - description?: string; timezone?: string; execute(tasks: ScheduledTaskContext): Awaitable }