aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md35
1 files changed, 35 insertions, 0 deletions
diff --git a/README.md b/README.md
index 4356a70..7983c29 100644
--- a/README.md
+++ b/README.md
@@ -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
+
+