From 972e0578f999aadb5924d84768e59445c92f167b Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 2 Dec 2020 00:29:25 +0300 Subject: feat: add schema and model --- lib/model.ts | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/schema.ts | 35 ++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 lib/model.ts create mode 100644 lib/schema.ts (limited to 'lib') diff --git a/lib/model.ts b/lib/model.ts new file mode 100644 index 0000000..77fede0 --- /dev/null +++ b/lib/model.ts @@ -0,0 +1,87 @@ +import { model, Schema, Model } from 'mongoose'; +import createEventSchema, { EventDocument } from './schema'; +import cron from 'cron'; + +interface Event extends EventDocument { + log(message: string): void; + start(): void; + complete(): void; + fail(): void; + computeNextRunAt(): Date; +} + +const CronJob = cron.CronJob; + +const createEventModel = (name: string, contextSchema: Context) => { + const schema = createEventSchema(contextSchema); + + // Schema methods + schema.method('log', function(message: string) { + // TODO: Actually create logs + console.log(message); + }); + + 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') + this.status = 'complete'; + return this.save(); + }); + + schema.method('fail', function(error: Error) { + this.log(error); + this.error = error; + this.status = 'failed'; + return this.save(); + }); + + schema.method('computeNextRunAt', function() { + const job = new CronJob(this.schedule); + const nextRunAt = job.nextDates(); + return nextRunAt.toDate(); + }); + + // Statics + schema.static('findMissedEvents', async function () { + return this.find({ + nextRunAt: { + // TODO: skip single-fire events + $lt: new Date() + }, + }); + }); + + schema.static('findNextEvents', function(limit = 10) { + return this.find( + { + nextRunAt: { + $gt: new Date() + }, + }, + null, + { + sort: { + nextRunAt: 1 + }, + limit + } + ) + }); + + // Hooks + schema.pre>('save', async function() { + this.nextRunAt = this.computeNextRunAt(); + }); + + return model>(name, schema); +}; + + +export default createEventModel; + diff --git a/lib/schema.ts b/lib/schema.ts new file mode 100644 index 0000000..4e5dd69 --- /dev/null +++ b/lib/schema.ts @@ -0,0 +1,35 @@ +import { Schema, Document } from 'mongoose'; + +export interface EventDocument extends Document { + name: string; + schedule: string; + status: 'notStarted' | 'running' | 'complete' | 'failed'; + error?: string; + context: Context; + nextRunAt: Date; + lastRunAt: Date; +} + +const createEventSchema = (contextSchema: Schema) => new Schema({ + name: { + type: String, + required: true, + unique: true + }, + schedule: { + type: String, + required: true + }, + status: { + type: String, + default: 'notStarted' + }, + error: String, + context: contextSchema, + nextRunAt: Date, + lastRunAt: Date +}, { timestamps: true }); + + +export default createEventSchema; + -- cgit v1.2.3