Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@tngtech/momo-scheduler

Package Overview
Dependencies
Maintainers
6
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tngtech/momo-scheduler

momo is a scheduler that persists jobs in mongodb

  • 2.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
55
decreased by-8.33%
Maintainers
6
Weekly downloads
 
Created
Source

Momo scheduler

npm license downloads npm type definitions

Momo is a light-weight, easy-to-use scheduler that persists jobs in a MongoDB and supports interval-based scheduling as well as cron jobs. In essence, it provides an easy way to tell your application to either "run that job every five minutes" or "run that job at 9 AM every weekday".

Features

  • allows concurrent jobs
  • allows immediate jobs
  • supports long-running jobs
  • allows error handling
  • supports cluster mode (e.g. several instances using the same job database)
    • only one schedule per name will attempt to schedule jobs
    • if an active schedule stops running (e.g. the application instance was stopped), another schedule with the same name (e.g. running on a different instance) will take over

Installation

Install via npm:

npm install @tngtech/momo-scheduler

or yarn:

yarn add @tngtech/momo-scheduler

You're all set!

Usage

import { MongoSchedule, MomoJob, MomoJobBuilder, MomoErrorEvent, MomoEvent } from 'momo-scheduler';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { MongoScheduleBuilder } from './MongoScheduleBuilder';

const mongo = await MongoMemoryServer.create();
const mongoSchedule = await new MongoScheduleBuilder()
  .withConnection({
    url: mongo.getUri(),
    collectionsPrefix: 'momo',
    scheduleName: 'MySchedule',
    pingIntervalMs: 10_000,
  })
  .build();

const intervalJob: MomoJob = new MomoJobBuilder()
  .withName('interval job')
  .withConcurrency(2)
  .withMaxRunning(3)
  .withSchedule('5 seconds')
  .withParameters({foo: 'bar'})
  .withHandler(() => console.log('This is a momo job that runs twice every five seconds!'))
  .build();
await mongoSchedule.define(intervalJob);

const cronJob: MomoJob = new MomoJobBuilder()
  .withName('cron job')
  .withConcurrency(1)
  .withMaxRunning(1)
  .withCronSchedule('0 0 * * 1-5')
  .withParameters({foo: {bar: "baz"}})
  .withHandler(() => console.log('This is a momo job that runs every weekday at midnight!'))
  .build();
await mongoSchedule.define(cronJob);

const cronJobWithNoParameters: MomoJob = new MomoJobBuilder()
  .withName('cron job without parameters')
  .withConcurrency(1)
  .withMaxRunning(3)
  .withCronSchedule('0 0 * * 1-3')
  .withHandler(() => console.log('This is a momo job that runs on Monday, Tuesday and Wednesday!'))
  .build();
await mongoSchedule.define(cronJobWithNoParameters);

// optional: listen to error and debug events
mongoSchedule.on('error', (error: MomoErrorEvent) => {
  /* handle error */
});
mongoSchedule.on('debug', (debug: MomoEvent) => {
  /* ... */
});

await mongoSchedule.start();

// all your application code...

// gracefully disconnect the scheduler
await mongoSchedule.disconnect();

MomoJob

You can instantiate a momo job using the MomoJobBuilder class. It provides the following setter methods:

setterparametersmandatorydefault valuedescription
withNamestringtrueThe name of the job. Used as a unique identifier.
withSchedulenumber or string,number or string (default: 0)falseEither this setter or withCronSchedule() must be called. Specifies the schedule for starting the job. Time intervals can be given in human-readable formats (like '1 minute', 'ten days' or 'twenty-one days and 2 hours' ) or in milliseconds. Check the documentation of human-interval for details. If the job never ran before, the job will run after firstRunAfter for the first time. Just like the interval, firstRunAfter can also be given in a human-readable format or as milliseconds.
withCronSchedulestringfalseEither this setter or withCronSchedule() must be called. Specifies the cron schedule according to which the job will run. See https://www.npmjs.com/package/cron#cron-ranges for the allowed cron ranges.
withConcurrencynumberfalse1How many instances of a job are started at a time.
withMaxRunningnumberfalse0Maximum number of job executions that is allowed at a time. Set to 0 for no max. The schedule will trigger no more job executions if maxRunning is reached. However, there is no guarantee that the schedule always respects the limit; in rare cases with multiple Momo instances maxRunning may be exceeded.
withHandlerfunctiontrueThe function to execute.
withParametersJobParametersfalseThe parameters with which the function provided by withHandler is called.

MongoSchedule

You can instantiate a mongo schedule using the MongoScheduleBuilder class. It provides the following setter methods:

setterparametersmandatorydefault valuedescription
withJobjob: MomoJobtrueAdds a job to the schedule.
withConnectionoptions: MomoOptionstrueThe connection options of the schedule.

The start/stop/cancel/remove methods can take a job's name as an optional parameter. Only the job with the provided name is started/stopped/cancelled/removed. If there is no job with this name on the schedule, nothing is done. If the parameter is omitted, all jobs are started/stopped/cancelled/removed.

functionparametersdescription
connectoptions: MomoConnectionOptionsCreates a new MongoSchedule connected to your database. See below for the available options.
definejob: MomoJobCreates a new MomoJob on the schedule.
startStarts jobs that are on the schedule.
stopStops jobs, but does not remove them from either the schedule or the database.
cancelStops and removes jobs from the schedule, does not remove them from the database.
removeStops and removes jobs from both the schedule and the database.
startJobname: stringStarts the job with the provided name (if on the schedule).
stopJobname: stringStops the job with the provided name (if on the schedule), but does not remove it from either the schedule or the database.
cancelJobname: stringStops and removes the job with the provided name (if on the schedule) from the schedule, does not remove it from the database.
removeJobname: stringStops and removes the job with the provided name (if on the schedule) from both the schedule and the database.
countonlyStarted: boolean (optional)Returns the number of jobs on the schedule. Only started jobs are counted if parameter is set to true.
listReturns descriptions of all jobs on the schedule.
checkname: stringReturns execution information of the job with the provided name from the database. This also works if the job is not on the schedule.
clearRemoves all jobs from the database. This also removes jobs that are not on this schedule, but were defined by other schedules. However, does NOT stop job executions - this will cause currently running jobs to fail. Consider using stop/cancel/remove methods instead!
getname: stringReturns a description of the job. Returns undefined if no job with the provided name is defined.
runname: string, parameters: JobParameters (default: undefined), delay: number (default: 0)Runs the job with the provided name once, independently from the schedule, after the specified delay. If no parameters are provided, the job will be called with no parameters. Note that maxRunning is respected, ie. the execution is skipped if the job is already running maxRunning times.
on'debug' or 'error', functionDefine a callback for debug or error events.
MomoConnectionOptions
propertytypeoptionaldefaultdescription
urlstringfalseThe connection string of your database.
collectionsPrefixstringtrueno prefixA prefix for all collections created by Momo.
pingIntervalMsnumbertrue60_000The keep alive ping interval of the schedule, in milliseconds. After twice the amount of time has elapsed without a ping of your Momo instance, other instances would take over. You might want to reduce this if you have jobs running on short intervals.
MomoJobDescription

The job description returned by the list and get functions of the MongoSchedule contains the following properties:

propertytypeoptionaldescription
namestringfalseThe name of the job. Used as a unique identifier.
intervalstringfalseSpecifies the time interval at which the job is started.
concurrencynumberfalseHow many instances of a job are started at a time.
maxRunningnumberfalseMaximum number of job executions that is allowed at a time. Set to 0 for no max. The job will not be started anymore if maxRunning is reached.
schedulerStatus{ interval: string, running: number }trueOnly present if the job was started, reports the number of currently running executions of the job and the time interval at which job execution is triggered. This might differ from the top-level interval as the interval of an already started job is not changed automatically when the job is updated.

The MongoSchedule is an EventEmitter, emitting 'debug' and 'error' events. You can define callbacks to handle them:

mongoSchedule.on('error', ({ error, data, message, type }: MomoErrorEvent) => {
  console.log(`An error of type ${type} occurred`, { error, data, message });
});

mongoSchedule.on('debug', ({ data, message }: MomoEvent) => {
  console.log('A debug event occurred', { data, message });
});

MomoEvent and MomoErrorEvent

eventpropertytypedescription
bothmessagestringSome information about the event that occurred.
bothdata (optional){ name?: string; ... }Contains additional information like the name of the affected job.
errortypeMomoErrorTypeone of: 'defining job failed', 'scheduling job failed', 'executing job failed', 'stopping job failed', 'an internal error occurred'
errorerror (optional)ErrorThe root cause of the error.

Job Examples

import { MomoJobBuilder } from './MomoJobBuilder';

const example1 = new MomoJobBuilder()
  .withName('example 1')
  .withInterval('5 minutes')
  .withHandler(() => console.log('This is momo'))
  .build();

const example2 = new MomoJobBuilder()
  .withName('example 2')
  .withInterval('5 minutes', 60 * 1000) // first run after one minute
  .withHandler(() => console.log('This is momo'))
  .build();

const example3 = new MomoJobBuilder()
  .withName('example 3')
  .withInterval('5 minutes', '4 minutes') // first run after four minutes
  .withHandler(() => console.log('This is momo'))
  .build();

const example4 = new MomoJobBuilder()
  .withName('example 4')
  .withCronSchedule('0 0 * * 1-5') // every weekday at midnight
  .withHandler(() => console.log('This is momo'))
  .build();

Assume it is 12:00 AM when the MongoSchedule with these four example jobs is started.

  • example 1 will be run immediately, at 12:00, and then every five minutes. Adding firstRunAfter: 0 explicitly is equivalent as this is the default value.
  • example 2 will be run after 1 minute (the configured firstRunAfter), at 12:01, and then every five minutes (next is 12:06).
  • example 3 will be run after 4 minutes (the configured firstRunAfter), at 12:04, and then every five minutes (next is 12:09).
  • example 4 will be run at midnight every weekday

Now assume the MongoSchedule is stopped at 12:02 and then immediately started again.

  • example 1 will be run 5 minutes (the configured interval) after the last execution, at 12:05. The job is NOT run immediately because it already ran before.
  • example 2 will be run 5 minutes (the configured interval) after the last execution, at 12:06. The job is NOT run after 1 minute (the configured firstRunAfter) because it already ran before.
  • example 3 will be run 4 minutes after the start (because of the configured firstRunAfter), at 12:06, because it never ran before, and then every five minutes (next is 12:11).
  • example 4 will be run at midnight every weekday

If your program is not running while a cron job should start, the missed run will not be started when your program resumes. This is the case for interval jobs though. The benefit of cron jobs is, that you can control the exact time of execution. They are also not restricted to a constant interval.

Supported Node Versions

momo-scheduler is currently tested on the node LTS versions 16 and 18.

License

This project is open source and licensed under Apache 2.0.

Licenses and Copyrights of dependencies can be found in THIRD_PARTY_NOTICE for the latest release.

For contribution guidelines see CONTRIBUTING.md.

Keywords

FAQs

Package last updated on 12 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc