Learn how companies like Phone2Action are using npm in their front-end developmentJoin the conversation »


0.1.2 • Public • Published


Handle the distribuited execution of scheduled tasks persisted on Firebase. One task executed per worker.

const cronRef = firebase.database().ref("cron");
const Cron = require("firebase-cron-queue");
const cron = new Cron(cronRef);
cron.setTask("example", "*/30 * * * * *");
cron.onTaskRun(async (task) => {
    if (task.id === "example") {
        return "hello!"

How it works

The process (also known as worker) will periodically checks for tasks and will pick the first worker available to execute the first task in queue. A single worker will run only one task at the time, but of course more workers will execute multiple tasks. Changes are atomically updated.

Tasks can hold a data object, that the worker can use for contextual reference.

When a task is done, a response object will be saved inside the task object.

Automatic failover system

For many reasons like an unexpected shutdown/network error/long time job/etc.., workers and tasks may remain in a "inconsistent way" on the database. Because of that, all free workers will periodically act as controller, by cleaning and releasing any expired task/worker.

Production environment

To increase the performance of your Realtime Database, add on Firebase the following indexing rules:

  "workers": {
    ".indexOn": "state"
  "tasks": {
    ".indexOn": "state"

### Install

npm install firebase-cron-queue
yarn add firebase-cron-queue

### Testing

npm test

### License



new Cron(refCron, [options])

Instantiate the main class. If no options are specified, a worker is also started.

refCron: FirebaseReference It is the Firebase reference to the /cron path, where all the tasks and workers will be stored.

options: Object May contains the following settings:

  • startWorker: Boolean. If to use the process as a worker. May disable to just use the tasks management API
  • onError: Function. A function which the worker will log any critical exception
  • debug: Function. A function which the worker will log any step info. Set to null to disable it
  • handleProcessExit: Boolean. If to automatically remove the worker from the database when the process is closed. It uses the process.on() method of Node

setTask(taskId, cronTime, [options])

Add or update a task.

taskId: String Choose a unique name for the task

cronTime: String Time schedule of the task, written in cron format

options May contains the following settings:

  • data: Object Contextual data to save in the task object
  • runOnce: Boolean If to run the task only one time. By default the task is always re-scheduled
  • timeout: Number Task expiration time in milliseconds, after a worker started its execution. By default it is 60 minutes


Execute the async callback when the worker (the process) executes a task. The task object is passed as argument to it.

@return {Promise}

Get the specific task object

taskId: String The ID of the task to retrieve

@return {Promise}

Get all the tasks


Remove a specified task

taskId: String. The ID of the task to remove


Start the worker for the current process.

It will listen for new tasks and will pick the job at its turn.

@return {Promise}

Gracefully remove the worker from the queue, without actually closing the process.

Any current task may still be executed

Recommendation notes:

Despite the above, please be aware that firebase-cron-queue is in beta version and not ready for large scale dataset.




npm i firebase-cron-queue

DownloadsWeekly Downloads






Last publish


  • avatar