diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-12-04 05:15:52 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-04 05:15:52 +0300 |
commit | 7e427aa9d3d806a8f26773b92016dbf5b5f11ff4 (patch) | |
tree | 90893102bcd5c958cb67a2f87ba340a1330b346e | |
parent | 1338c0e7c76ccc3d168bdb7d2f3f7dba3fa0ed79 (diff) | |
download | mongo-cronjob-master.tar.gz |
-rw-r--r-- | README.md | 35 |
1 files changed, 35 insertions, 0 deletions
@@ -1,6 +1,11 @@ # mongo-cronjob Cron-based job scheduler which persists events to MongoDB +## Installation +``` +npm i mongo-cronjob +``` + ## Usage example ```js const mongoose = require('mongoose'); @@ -28,3 +33,33 @@ EventModel.create({ } }); ``` + +## API +### Log +Document interface: +```ts +interface LogDocument extends Document { + eventId: string; + message: string; +} +``` + +### Event +Document interface: +```ts +interface EventDocument<Context = any> extends Document { + type: string; // Scheduler will try to find a handler for this type + schedule: string; // Should be a valid crontab + status?: 'notStarted' | 'running' | 'complete' | 'failed'; + error?: string; // Latest error thrown by handler + context: Context; + nextRunAt?: Date; // Updated on 'save' hook (based on schedule) + lastRunAt?: Date; +} +``` +Every field except for `event.context` is managed by `Scheduler` automatically. For logging and managing status (useful in handlers), following methods are available: + - `event.log(message: string): Promise<Log>` - create a `Log` instance and also redirect to console + - `event.fail(error: Error | string): Promise<Event>` - set status to `failed` and log the error nicely + - `event.getLogs(): Promise<Log[]>` - return all logs for this event + + |