firebase-cron-queue v0.1.2
firebase-cron-queue
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:
"cron": {
  "workers": {
    ".indexOn": "state"
  },
  "tasks": {
    ".indexOn": "state"
  }
 }Install
npm install firebase-cron-queue
or
yarn add firebase-cron-queueTesting
npm testLicense
MITAPI
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: ObjectContextual data to save in the task object
- runOnce: BooleanIf to run the task only one time. By default the task is always re-scheduled
- timeout: NumberTask expiration time in milliseconds, after a worker started its execution. By default it is 60 minutes
onTaskRun(asyncCallback)Execute the async callback when the worker (the process) executes a task. The task object is passed as argument to it.
getTask(taskId)
@return {Promise}Get the specific task object
taskId: String The ID of the task to retrieve
getTasks()
@return {Promise}Get all the tasks
.removeTask(taskId)Remove a specified task
taskId: String. The ID of the task to remove
startWorker()Start the worker for the current process.
It will listen for new tasks and will pick the job at its turn.
quitWorker()
@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.