diff options
author | eug-vs <eug-vs@keemail.me> | 2020-12-02 05:31:35 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-12-02 05:31:35 +0300 |
commit | 83393fd06f2a61006a28ebb45524bdd39fdfb6c1 (patch) | |
tree | d318791d5c11cd8437e4b11a77f411200ba8950e | |
parent | 286c2f962db1087fb0d6ee444c218346056d85b3 (diff) | |
download | mongo-cronjob-83393fd06f2a61006a28ebb45524bdd39fdfb6c1.tar.gz |
fix: resovle eslint errors
-rw-r--r-- | .eslintrc.json | 4 | ||||
-rw-r--r-- | lib/event.model.ts | 28 | ||||
-rw-r--r-- | lib/scheduler.ts | 8 | ||||
-rw-r--r-- | package.json | 1 |
4 files changed, 23 insertions, 18 deletions
diff --git a/.eslintrc.json b/.eslintrc.json index 2a3b4a8..8a6f297 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -20,6 +20,8 @@ "no-cond-assign": 0, "no-console": 0, "no-plusplus": 0, - "linebreak-style": 0 + "linebreak-style": 0, + "func-names": 0, + "@typescript-eslint/comma-dangle": 0 } } 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); } diff --git a/package.json b/package.json index f5dd8e3..f4d73da 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "Cron-based job scheduler integrated with mongoDB", "main": "index.js", "scripts": { + "lint": "eslint .", "test": "mocha test/**/*.test.ts" }, "author": "eug-vs", |