i think cron works

This commit is contained in:
Jacob Nguyen
2024-05-15 16:42:21 -05:00
parent 0d82658fc5
commit d905f08993
10 changed files with 49 additions and 14 deletions

View File

@@ -1,4 +1,9 @@
import type { LogPayload, Logging, ErrorHandling } from '../interfaces';
import { AnyFunction } from '../../types/utility';
import cron from 'node-cron'
import { EventEmitter } from 'events';
import type { CronEventCommand, Module } from '../../types/core-modules'
import { EventType } from './enums';
/**
* @internal
* @since 2.0.0
@@ -41,3 +46,39 @@ export class DefaultLogging implements Logging {
console.warn(`WARN: ${this.date().toISOString()} -> ${payload.message}`);
}
}
export class Cron extends EventEmitter {
tasks: string[] = [];
modules: Map<string, CronEventCommand> = new Map();
private sanityCheck(eventName: string | symbol) : asserts eventName is string {
if(typeof eventName === 'symbol') throw Error("Cron cannot add symbol based listener")
if(!cron.validate(eventName)) {
throw Error("Invalid cron expression while adding")
}
}
addCronModule(module: Module) {
if(module.type !== EventType.Cron) {
throw Error("Can only add cron modules");
}
this.modules.set(module.name!, module as CronEventCommand);
}
addListener(eventName: string | symbol, listener: AnyFunction): this {
this.sanityCheck(eventName);
const retrievedModule = this.modules.get(eventName);
if(!retrievedModule) throw Error("Adding task: module " +eventName +"was not found");
cron.schedule(retrievedModule.pattern, listener, {
name: retrievedModule?.name!
});
return this;
}
removeListener(eventName: string | symbol, listener: AnyFunction) {
this.sanityCheck(eventName);
const retrievedModule = this.modules.get(eventName);
if(!retrievedModule) throw Error("Removing cron: module " +eventName +"was not found");
const task= cron.getTasks().get(retrievedModule.name!)
if(!task) throw Error("Finding cron task with"+ retrievedModule.name + " not found");
task.stop();
super.removeListener(eventName, listener);
return this;
}
}