diff options
author | eug-vs <eug-vs@keemail.me> | 2020-12-02 02:22:38 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-12-02 02:22:38 +0300 |
commit | 4186fb51f66043b221237c7ba91ac5a95a4f32b0 (patch) | |
tree | 71657f367d899d0fbc165b3c50197d81b8d69007 | |
parent | f6c5ac6632781d813a6e1f1ba957e7f9e4d70738 (diff) | |
download | mongo-cronjob-4186fb51f66043b221237c7ba91ac5a95a4f32b0.tar.gz |
feat: add log model
-rw-r--r-- | lib/event.model.ts (renamed from lib/model.ts) | 14 | ||||
-rw-r--r-- | lib/event.schema.ts (renamed from lib/schema.ts) | 0 | ||||
-rw-r--r-- | lib/log.model.ts | 8 | ||||
-rw-r--r-- | lib/log.schema.ts | 15 | ||||
-rw-r--r-- | lib/scheduler.ts | 2 |
5 files changed, 35 insertions, 4 deletions
diff --git a/lib/model.ts b/lib/event.model.ts index f93928e..a430674 100644 --- a/lib/model.ts +++ b/lib/event.model.ts @@ -1,6 +1,8 @@ import { model, Schema, Model } from 'mongoose'; -import createEventSchema, { EventDocument } from './schema'; import cron from 'cron'; +import createEventSchema, { EventDocument } from './event.schema'; +import { LogDocument } from './log.schema'; +import LogModel from './log.model'; interface Event<Context> extends EventDocument<Context> { log(message: string): void; @@ -8,6 +10,7 @@ interface Event<Context> extends EventDocument<Context> { complete(): void; fail(error: Error): void; computeNextRunAt(): Date; + getLogs(): Promise<LogDocument[]>; } export interface EventModel<Context> extends Model<Event<Context>> { @@ -22,8 +25,9 @@ const createEventModel = <Context>(name: string, contextSchema: Schema): EventMo // Schema methods schema.method('log', function(message: string) { - // TODO: Actually create logs - console.log(message); + const timestamp = new Date().toLocaleString('en'); + console.log(`[${timestamp}] ${this.name}: ${message}`); + return LogModel.create({ eventId: this._id, message }); }); schema.method('start', function() { @@ -52,6 +56,10 @@ const createEventModel = <Context>(name: string, contextSchema: Schema): EventMo return nextRunAt.toDate(); }); + schema.method('getLogs', function() { + return LogModel.find({ eventId: this._id }); + }); + // Statics schema.static('findMissedEvents', async function () { return this.find({ diff --git a/lib/schema.ts b/lib/event.schema.ts index 1bba77d..1bba77d 100644 --- a/lib/schema.ts +++ b/lib/event.schema.ts diff --git a/lib/log.model.ts b/lib/log.model.ts new file mode 100644 index 0000000..7ea9cb9 --- /dev/null +++ b/lib/log.model.ts @@ -0,0 +1,8 @@ +import { model, Model } from 'mongoose'; +import schema, { LogDocument } from './log.schema'; + +const LogModel = model<LogDocument, Model<LogDocument>>('Log', schema); + + +export default LogModel; + diff --git a/lib/log.schema.ts b/lib/log.schema.ts new file mode 100644 index 0000000..3c55de7 --- /dev/null +++ b/lib/log.schema.ts @@ -0,0 +1,15 @@ +import { Schema, Types, Document } from 'mongoose'; + +export interface LogDocument extends Document { + eventId: string; + message: string; +} + +const schema = new Schema({ + eventId: Types.ObjectId, + message: String +}, { timestamps: true }); + + +export default schema; + diff --git a/lib/scheduler.ts b/lib/scheduler.ts index 449ac39..28d09cc 100644 --- a/lib/scheduler.ts +++ b/lib/scheduler.ts @@ -1,6 +1,6 @@ import cron from 'cron'; import Bluebird from 'bluebird'; -import { EventModel } from './model'; +import { EventModel } from './event.model'; const CronJob = cron.CronJob; |