firebase-simple-queue
![License](https://img.shields.io/npm/l/firebase-simple-queue.svg)
Simple queue implementation for firebase cloud functions and realtime database
The queue will sequentially run each task added in the queue
If your calllback is asyncronous, make sure to await each asyncronous subtasks or async functions, otherwise the queue could not work properly
![](https://github.com/dflourusso/firebase-simple-queue/raw/HEAD/example.gif)
Installation
yarn add firebase-simple-queue
or using npm
npm install --save firebase-simple-queue
Usage
Initialize the queue in cloud functions index
import queue from 'firebase-simple-queue'
function callback(task, context) {
return new Promise(resolve => {
setTimeout(() => {
resolve()
}, 5000)
})
}
const { onCreateTask, onFinishTask, onRetryTask } = queue('somePath/{someParam}/queue', callback)
export {
onCreateTask,
onFinishTask,
onRetryTask
}
Add an index in database.rules
{
"rules": {
"queue": {
"tasks": {
".indexOn": "_error"
}
}
}
}
Example
Now in your client just add a task in the queue
const myTaskObject = { attr1: 'Test1', another_attr: 123 }
database
.ref('queue')
.child('tasks')
.push(myTaskObject)
What about errors
If a task throws an error, the details will be in a _error
key inside the task. To try run a task with error again, just remove the key _error
The task with the key _error
will be ignored in the execution queue. Ex: If I have 3 tasks in the queue and the first one is in error, the queue will only execute tasks 2 and 3.
Development
Just edit the code, commit, and run ./publish.sh
Contribution
You can open an issue or send a pull request
Author
Daniel Fernando Lourusso - dflourusso@gmail.com