• Stars
    star
    553
  • Rank 80,462 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 4 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

In-memory Node.js and browser job scheduler

toad-scheduler

NPM Version NPM Downloads Build Status Coverage Status

In-memory TypeScript job scheduler that repeatedly executes given tasks within specified intervals of time (e. g. "each 20 seconds"). Cron syntax is also supported in case you need it.

Node.js 12+ and modern browsers are supported

Getting started

First install the package:

npm i toad-scheduler

Next, set up your jobs:

const { ToadScheduler, SimpleIntervalJob, Task } = require('toad-scheduler')

const scheduler = new ToadScheduler()

const task = new Task('simple task', () => { counter++ })
const job = new SimpleIntervalJob({ seconds: 20, }, task)

scheduler.addSimpleIntervalJob(job)

// when stopping your app
scheduler.stop()

Usage with async tasks

In order to avoid unhandled rejections, make sure to use AsyncTask if your task is asynchronous:

const { ToadScheduler, SimpleIntervalJob, AsyncTask } = require('toad-scheduler')

const scheduler = new ToadScheduler()

const task = new AsyncTask(
    'simple task', 
    () => { return db.pollForSomeData().then((result) => { /* continue the promise chain */ }) },
    (err: Error) => { /* handle error here */ }
)
const job = new SimpleIntervalJob({ seconds: 20, }, task)

scheduler.addSimpleIntervalJob(job)

// when stopping your app
scheduler.stop()

Note that in order to avoid memory leaks, it is recommended to use promise chains instead of async/await inside task definition. See talk on common Promise mistakes for more details.

Asynchronous error handling

Note that your error handlers can be asynchronous and return a promise. In such case an additional catch block will be attached to them, and should there be an error while trying to resolve that promise, and logging error will be logged using the default error handler (console.error).

Preventing task run overruns

In case you want to prevent second instance of a task from being fired up while first one is still executing, you can use preventOverrun options:

import { ToadScheduler, SimpleIntervalJob, Task } from 'toad-scheduler';

const scheduler = new ToadScheduler();

const task = new Task('simple task', () => {
    // if this task runs long, second one won't be started until this one concludes
	console.log('Task triggered');
});

const job = new SimpleIntervalJob(
	{ seconds: 20, runImmediately: true },
	task,
    { 
        id: 'id_1',
        preventOverrun: true,
    }
);

//create and start jobs
scheduler.addSimpleIntervalJob(job);

Using IDs and ES6-style imports

You can attach IDs to tasks to identify them later. This is helpful in projects that run a lot of tasks and especially if you want to target some of the tasks specifically (e. g. in order to stop or restart them, or to check their status).

import { ToadScheduler, SimpleIntervalJob, Task } from 'toad-scheduler';

const scheduler = new ToadScheduler();

const task = new Task('simple task', () => {
	console.log('Task triggered');
});

const job1 = new SimpleIntervalJob(
	{ seconds: 20, runImmediately: true },
	task,
    { id: 'id_1' }
);

const job2 = new SimpleIntervalJob(
	{ seconds: 15, runImmediately: true },
	task,
    { id: 'id_2' }
);

//create and start jobs
scheduler.addSimpleIntervalJob(job1);
scheduler.addSimpleIntervalJob(job2);

// stop job with ID: id_2
scheduler.stopById('id_2');

// remove job with ID: id_1
scheduler.removeById('id_1');

// check status of jobs
console.log(scheduler.getById('id_1').getStatus()); // returns Error (job not found)

console.log(scheduler.getById('id_2').getStatus()); // returns "stopped" and can be started again

Cron support

You can use CronJob instances for handling Cron-style scheduling:

      const task = new AsyncTask('simple task', () => {
        // Execute your asynchronous logic here
      })
      const job = new CronJob(
        {
          cronExpression: '*/2 * * * * *',
        },
        task,
        {
          preventOverrun: true,
        }
      )
      scheduler.addCronJob(job)

Note that you need to install "croner" library for this to work. Run npm i croner in order to install this dependency.

Usage in clustered environments

toad-scheduler does not persist its state by design, and has no out-of-the-box concurrency management features. In case it is necessary to prevent parallel execution of jobs in clustered environment, it is highly recommended to use redis-semaphore in your tasks.

Here is an example:

import { randomUUID } from 'crypto'

import type { Redis } from 'ioredis'
import type { LockOptions } from 'redis-semaphore'
import { Mutex } from 'redis-semaphore'
import { AsyncTask } from 'toad-scheduler';

export type BackgroundJobConfiguration = {
    jobId: string
}

export type LockConfiguration = {
    lockName?: string
    refreshInterval?: number
    lockTimeout: number
}

// Abstract Job
export abstract class AbstractBackgroundJob {
    public readonly jobId: string
    protected readonly redis: Redis

    protected constructor(
        options: BackgroundJobConfiguration,
        redis: Redis,
    ) {
        this.jobId = options.jobId
        this.redis = redis
    }

    protected abstract processInternal(executionUuid: string): Promise<void>

    async process() {
        const uuid = randomUUID()

        try {
            await this.processInternal(uuid)
        } catch (err) {
            console.error(logObject)
        }
    }

    protected getJobMutex(key: string, options: LockOptions) {
        return new Mutex(this.redis, this.getJobLockName(key), options)
    }

    protected async tryAcquireExclusiveLock(lockConfiguration: LockConfiguration) {
        const mutex = this.getJobMutex(lockConfiguration.lockName ?? 'exclusive', {
            acquireAttemptsLimit: 1,
            refreshInterval: lockConfiguration.refreshInterval,
            lockTimeout: lockConfiguration.lockTimeout,
        })

        const lock = await mutex.tryAcquire()
        // If someone else already has this lock, skip
        if (!lock) {
            return
        }

        return mutex
    }

    protected getJobLockName(key: string) {
        return `${this.jobId}:locks:${key}`
    }
}

// Job example

const LOCK_TIMEOUT_IN_MSECS = 60 * 1000
const LOCK_REFRESH_IN_MSECS = 10 * 1000

export class SampleJob extends AbstractBackgroundJob {
  constructor(redis: Redis) {
    super(
      {
        jobId: 'SampleJob',
      },
      redis,
    )
  }

  protected async processInternal(executionUuid: string): Promise<void> {
    // We only want a single instance of this job running in entire cluster, let's see if someone else is already processing it
    const lock = await this.tryAcquireExclusiveLock({
      lockTimeout: LOCK_TIMEOUT_IN_MSECS,
      refreshInterval: LOCK_REFRESH_IN_MSECS,
    })

    // Job is already running, skip
    if (!lock) {
      this.logger.debug(`Job already running in another node, skipping (${executionUuid})`)
      return
    }

    try {
      // Process job logic here
      await this.sampleJobLogic()
    } finally {
      await lock.release()
    }
  }

  private async sampleJobLogic() {
    // dummy processing logic
    return Promise.resolve()
  }


// Job registration
function createTask(job: AbstractBackgroundJob): AsyncTask {
    return new AsyncTask(
        job.jobId,
        () => {
            return job.process()
        },
    )
}

API for schedule

  • days?: number - how many days to wait before executing the job for the next time;
  • hours?: number - how many hours to wait before executing the job for the next time;
  • minutes?: number - how many minutes to wait before executing the job for the next time;
  • seconds?: number - how many seconds to wait before executing the job for the next time;
  • milliseconds?: number - how many milliseconds to wait before executing the job for the next time;
  • runImmediately?: boolean - if set to true, in addition to being executed on a given interval, job will also be executed immediately when added or restarted.

API for jobs

  • start(): void - starts, or restarts (if it's already running) the job;
  • stop(): void - stops the job. Can be restarted again with start command;
  • getStatus(): JobStatus - returns the status of the job, which is one of: running, stopped.

API for scheduler

  • addSimpleIntervalJob(job: SimpleIntervalJob): void - registers and starts a new job;
  • addLongIntervalJob(job: SimpleIntervalJob): void - registers and starts a new job with support for intervals longer than 24.85 days;
  • addIntervalJob(job: SimpleIntervalJob | LongIntervalJob): void - registers and starts new interval-based job;
  • stop(): void - stops all jobs, registered in the scheduler;
  • getById(id: string): Job - returns the job with a given id.
  • existsById(id: string): boolean - returns true if job with given id exists, false otherwise.
  • stopById(id: string): void - stops the job with a given id.
  • removeById(id: string): Job | undefined - stops the job with a given id and removes it from the scheduler. If no such job exists, returns undefined, otherwise returns the job.
  • startById(id: string): void - starts, or restarts (if it's already running) the job with a given id.

More Repositories

1

asynchronous-local-storage

Asynchronous local storage implementation based on Node.js ALS with fallback to cls-hooked for older Node.js versions
TypeScript
46
star
2

layered-loader

Data loader with support for caching and fallback data sources
TypeScript
43
star
3

toad-cache

In-memory cache for Node.js and browser
JavaScript
17
star
4

nodejs-benchmark-tournament

Collection of various benchmark, comparing everything Node.js-related to each other
JavaScript
14
star
5

awilix-manager

Wrapper over awilix to support more complex use-cases
TypeScript
11
star
6

protobuf-fieldmask

Library for generating and applying FieldMask
JavaScript
8
star
7

message-queue-toolkit

Useful utilities, interfaces and base classes for message queue handling
TypeScript
8
star
8

objection-utils

Utils for Objection.js ORM
JavaScript
7
star
9

photofinish

Benchmarking library for Node.js that emphasizes the convenience of use.
TypeScript
6
star
10

undici-retry

Library for handling retry logic with undici HTTP client
TypeScript
6
star
11

objection-swagger

JSON schema YAML generator for Objection.js models. Intended to be used together with automatic Swagger generators
JavaScript
6
star
12

unzipomatic

Modern unzipping library for Node.js
TypeScript
3
star
13

fastify-s3-buckets

Fastify plugin for ensuring existence of defined s3 buckets on the application startup
JavaScript
2
star
14

grpc-middleware

Lightweight and unobtrusive middleware library for Node.js gRPC server
JavaScript
2
star
15

swampmachine

Java
2
star
16

gmx-word-counter

GMX-V 2.0 compliant word counting implementation for Node.js
TypeScript
2
star
17

nockback-harder

Convenience wrapper for mock record/replay functionality in nock library
TypeScript
2
star
18

ld37

Ludum Dare 37 Entry by Transient Team
Java
2
star
19

canned-enterprise-node

Reusable solutions for Node.js
TypeScript
2
star
20

protobuf-generator

Generate Protocol Buffer schemas from blueprints defined in JS with JSON-Schema
JavaScript
2
star
21

chatgpt-cloudflare

ChatGPT API client, compatible with Cloudflare Workers
TypeScript
2
star
22

node-persistence-best-practices

Node.js persistence layer best practices: example project
TypeScript
2
star
23

validation-utils

Validation common utils for Javascript
TypeScript
2
star
24

toad-uri-js

A modern RFC 3986/3987 compliant, scheme extendable URI/IRI parsing/normalizing/resolving/serializing library for JavaScript.
TypeScript
2
star
25

potato-golem

TypeScript framework for making games, based on Phaser 3
TypeScript
2
star
26

fastify-prometheus-nitro

Prometheus plugin for fastify, focusing on lowest possible overhead
TypeScript
1
star
27

ktor-scheduler

Cluster-friendly task-scheduler for ktor
Kotlin
1
star
28

express-swagger-oauth-scopes

Middleware to grant/block access to endpoints based on Swagger security entries
JavaScript
1
star
29

cloudflare-game-keeper

Game state management system, based on Cloudflare workers
1
star
30

node-simple-template

Fastify/TypeScript template for simple webservices
TypeScript
1
star
31

fastify-distributed-schedule

Fastify plugin that creates scheduled jobs with given concurrency if they do not yet exist
JavaScript
1
star
32

cli-testlab

Node.js test framework for CLI utilities
TypeScript
1
star
33

knex-utils

Utilities and helpers for knex.js
JavaScript
1
star
34

ninjas-and-rockstars

A game about building software
TypeScript
1
star
35

algorithmicon

Code kata exercises and useful code snippets (monoku)
JavaScript
1
star
36

in-the-cradle-of-stars

1
star
37

fastify-dependency-injection

Dependency injection support for fastify framework
JavaScript
1
star
38

dealing-with-complexity

Source code for the "Dealing with Complexity" workshop
JavaScript
1
star