Created
April 10, 2019 06:53
-
-
Save loftywaif002/ac3d56b9d90f9afb9110d613d8c0f9f1 to your computer and use it in GitHub Desktop.
Firebase queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//QUEUE POPULATOR | |
var Firebase = require('firebase'); | |
// Same ref as in queue_worker.js | |
var ref = new Firebase('https://<your-firebase>.firebaseio.com/queue'); | |
// This doesn't need to be set every time, but helps us | |
// define the spec for the task in this example | |
ref.child('specs').set({ | |
task_1: { | |
in_progress_state: 'task_1_in_progress', | |
finished_state: null, | |
timeout: 10000 | |
} | |
}); | |
// Add tasks onto the queue | |
var taskNumber = 0; | |
setInterval(function() { | |
ref.child('tasks').push({ | |
taskNumber: ++taskNumber | |
}); | |
}, 1000); | |
// WORKERS | |
var Queue = require('firebase-queue'), | |
Firebase = require('firebase'); | |
// The location of the Queue - can be any Firebase Location | |
var ref = new Firebase('https://<your-firebase>.firebaseio.com/queue'); | |
// Creates the Queue | |
var options = { | |
specId: 'task_1', | |
numWorkers: 10 | |
}; | |
var queue = new Queue(ref, options, function(data, progress, resolve, reject) { | |
// Read and process task data | |
console.log(data); | |
// Do some work | |
var percentageComplete = 0; | |
var interval = setInterval(function() { | |
percentageComplete += 20; | |
if (percentageComplete >= 100) { | |
clearInterval(interval); | |
} else { | |
progress(percentageComplete); | |
} | |
}, 1000); | |
// Finish the task | |
setTimeout(function() { | |
resolve(); | |
}, 5000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment