@cley_faye/js-cron
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

Manage scheduled tasks in-process using cron definitions

Schedule tasks from your long-running Node script using cron syntax.

Features

This project is very similar to node-cron and node-schedule with a minor difference: consecutive execution can't overlap.

If a task takes longer to run than the interval between two scheduled execution, the next execution can either be skipped (the default), or cause an immediate rescheduling once it completes.

Ideally, I should submit patches to the aforementionned projects if there is interest into this feature.

Installation

The library is provided on npmjs.

npm install @cley_faye/js-cron

Usage

Schedule a task

import {
  schedule,
  Overrun,
} from "@cley_faye/js-cron";

const task = schedule(
  "* * * * *",
  () => {
    // Task body
    // Can return a promise
  },
  {
    overrun: Overrun.SKIP,
  },
);

The task body can return a promise, in which case the task will be considered running until the promise resolves.

Currently, the task returned supports only one method: Task#cancel(), to cancel the task and remove it from the scheduler.

The syntax for the cron argument is parsed using cron-parser.

Options

The only option supported for now is overrun. It can take the following values:

  • Overrun.SKIP: if the previous occurrence is still running, skip the new one until we reach the next scheduled time (default)
  • Overrun.AFTER: if the previous occurrence is still running, run the new step immediately when it finishes

Control the scheduler

You can stop all scheduling at once by calling stop():

import {stop} from "@cley_faye/js-cron";

stop();

Tasks already running will complete asynchronously.

Readme

Keywords

none

Package Sidebar

Install

npm i @cley_faye/js-cron

Weekly Downloads

1

Version

2.0.2

License

MIT

Unpacked Size

17.3 kB

Total Files

11

Last publish

Collaborators

  • cley_faye