diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/event.model.ts | 28 | ||||
-rw-r--r-- | lib/scheduler.ts | 8 |
2 files changed, 19 insertions, 17 deletions
diff --git a/lib/event.model.ts b/lib/event.model.ts index 1b6d324..62675d4 100644 --- a/lib/event.model.ts +++ b/lib/event.model.ts @@ -18,32 +18,32 @@ export interface EventModel<Context> extends Model<Event<Context>> { findMissedEvents(): Event<Context>[]; } -const CronJob = cron.CronJob; +const { CronJob } = cron; const createEventModel = <Context>(name: string, contextSchema: Schema): EventModel<Context> => { const schema = createEventSchema(contextSchema); // Schema methods - schema.method('log', function(message: string) { + schema.method('log', function (message: string) { const timestamp = new Date().toLocaleString('en'); console.log(`[${timestamp}] ${this.type}: ${message}`); return LogModel.create({ eventId: this._id, message }); }); - schema.method('start', function() { - this.log('Event started') + schema.method('start', function () { + this.log('Event started'); this.lastRunAt = new Date(); this.status = 'running'; return this.save(); }); - schema.method('complete', function() { - this.log('Event complete') + schema.method('complete', function () { + this.log('Event complete'); this.status = 'complete'; return this.save(); }); - schema.method('fail', function(error: Error) { + schema.method('fail', function (error: Error) { this.log(error); this.log('Event failed'); this.error = error; @@ -51,13 +51,13 @@ const createEventModel = <Context>(name: string, contextSchema: Schema): EventMo return this.save(); }); - schema.method('computeNextRunAt', function() { + schema.method('computeNextRunAt', function () { const job = new CronJob(this.schedule); const nextRunAt = job.nextDates(); return nextRunAt.toDate(); }); - schema.method('getLogs', function() { + schema.method('getLogs', function () { return LogModel.find({ eventId: this._id }); }); @@ -67,16 +67,16 @@ const createEventModel = <Context>(name: string, contextSchema: Schema): EventMo nextRunAt: { // TODO: skip single-fire events $lt: new Date() - }, + } }); }); - schema.static('findNextEvents', function(limit = 10) { + schema.static('findNextEvents', function (limit = 10) { return this.find( { nextRunAt: { $gt: new Date() - }, + } }, null, { @@ -85,11 +85,11 @@ const createEventModel = <Context>(name: string, contextSchema: Schema): EventMo }, limit } - ) + ); }); // Hooks - schema.pre<Event<Context>>('save', async function() { + schema.pre<Event<Context>>('save', async function () { this.nextRunAt = this.computeNextRunAt(); }); diff --git a/lib/scheduler.ts b/lib/scheduler.ts index e1437bd..8715733 100644 --- a/lib/scheduler.ts +++ b/lib/scheduler.ts @@ -4,15 +4,18 @@ import { EventModel, Event } from './event.model'; export type Handler = (event: Event<any>) => void; -const CronJob = cron.CronJob; +const { CronJob } = cron; const defaultPollingInterval = '*/10 * * * * *'; class Scheduler { private jobs: cron.CronJob[]; + private pollingJob: cron.CronJob; + private handlers: Record<string, Handler>; + public Model: EventModel<any>; constructor(model: EventModel<any>, pollingInterval = defaultPollingInterval) { @@ -79,8 +82,7 @@ class Scheduler { event.start(); await handleEvent(event); return event.complete(); - } else throw new Error('No handler found') - + } throw new Error('No handler found'); } catch (error) { return event.fail(error); } |