diff options
-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 + + |