From 7e427aa9d3d806a8f26773b92016dbf5b5f11ff4 Mon Sep 17 00:00:00 2001 From: Eugene Sokolov Date: Fri, 4 Dec 2020 05:15:52 +0300 Subject: docs: add installation and API sections --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'README.md') 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 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` - create a `Log` instance and also redirect to console + - `event.fail(error: Error | string): Promise` - set status to `failed` and log the error nicely + - `event.getLogs(): Promise` - return all logs for this event + + -- cgit v1.2.3