Skip to content

Cron-based job scheduler which persists events to MongoDB

Notifications You must be signed in to change notification settings

eug-vs/mongo-cronjob

Repository files navigation

mongo-cronjob

Cron-based job scheduler which persists events to MongoDB

Installation

npm i mongo-cronjob

Usage example

const mongoose = require('mongoose');
const { Client, Scheduler } = require('mongo-cronjob');

// Initialize client with existing connection
const client = new Client(mongoose.connection);
const EventModel = client.Event;
const LogModel = client.Log;

// Initialize scheduler
const scheduler = new Scheduler(EventModel);

scheduler.registerHandler('test', async event => {
  // Your complicated async logic goes here instead
  const { message } = event.context;
  event.log(message);
});

EventModel.create({
  type: 'test', // Matches handler type
  schedule: '*/30 * * * * *', // Every 30 seconds
  context: {
    message: 'Hello, world!'
  }
});

API

Log

Document interface:

interface LogDocument extends Document {
  eventId: string;
  message: string;
}

Event

Document interface:

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

About

Cron-based job scheduler which persists events to MongoDB

Topics

Resources

Stars

Watchers

Forks