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,2 +0,0 @@
import type { Result } from 'ts-results-es'
export type VoidResult = Result<void, void>;

View File

@@ -22,7 +22,6 @@ export interface Emitter {
addListener(eventName: string | symbol, listener: AnyFunction): this;
removeListener(eventName: string | symbol, listener: AnyFunction): this;
emit(eventName: string | symbol, ...payload: any[]): boolean;
on(eventName: string | symbol, listener: AnyFunction): this
}

View File

@@ -1,5 +1,3 @@
import type { UnpackedDependencies } from '../../types/utility';
/**
* A semi-generic container that provides error handling, emitter, and module store.
* For the handler to operate correctly, The only user provided dependency needs to be @sern/client

View File

@@ -18,8 +18,8 @@ import {
import type { Emitter, ErrorHandling, Logging } from './interfaces';
import util from 'node:util';
import type { PluginResult } from '../types/core-plugin';
import type { VoidResult } from './_internal';
import { Result } from 'ts-results-es';
import { VoidResult } from '../types/utility';
/**
* if {src} is true, mapTo V, else ignore
* @param item

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;
}
}