aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugene Sokolov <eug-vs@keemail.me>2020-12-04 05:15:52 +0300
committerGitHub <noreply@github.com>2020-12-04 05:15:52 +0300
commit7e427aa9d3d806a8f26773b92016dbf5b5f11ff4 (patch)
tree90893102bcd5c958cb67a2f87ba340a1330b346e
parent1338c0e7c76ccc3d168bdb7d2f3f7dba3fa0ed79 (diff)
downloadmongo-cronjob-master.tar.gz
docs: add installation and API sectionsHEADmaster
-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
+
+