Handle the distribuited execution of scheduled tasks persisted on Firebase. One task executed per worker.
const cronRef = firebase;const Cron = ;const cron = cronRef;cron;cron;
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.
To increase the performance of your Realtime Database, add on Firebase the following indexing rules:
npm install firebase-cron-queueoryarn add firebase-cron-queue
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
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
Execute the async callback when the worker (the process) executes a task. The task object is passed as argument to it.
Get the specific task object
taskId: String The ID of the task to retrieve
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.
Gracefully remove the worker from the queue, without actually closing the process.
Any current task may still be executed
Despite the above, please be aware that
firebase-cron-queue is in beta version and not ready for large scale dataset.