Skip to content

Instantly share code, notes, and snippets.

@minzcmu
Last active October 3, 2017 17:26
Show Gist options
  • Save minzcmu/ef21fa4f715a760352cf426c69711a3b to your computer and use it in GitHub Desktop.
Save minzcmu/ef21fa4f715a760352cf426c69711a3b to your computer and use it in GitHub Desktop.
/////////////////////////
// REQUIRE THE PACKAGE //
/////////////////////////
var BusPrototype = require('node-queue-bus').bus;
var RiderPrototype = require('node-queue-bus').rider;
///////////////////////////
// SET UP THE CONNECTION //
///////////////////////////
var connectionDetails = {
package: "ioredis",
host: "127.0.0.1",
password: "",
port: 6379,
database: 0,
};
var appKey = 'builds';
// appKey is always lower-cased by resque-bus
// These subscriptions will put work to do in a "exampleApp_default" queue in resque: "(app_key)_(priority)"
var bus_queue = 'builds_default';
//////////////////////////////
// DEFINE YOUR WORKER TASKS //
//////////////////////////////
var jobs = {
"buildCreateJob": {
perform: function(payload, callback){
console.log("start build in k8s: ", payload.id);
callback();
},
},
"buildFinishJob": {
perform: function(payload, callback){
console.log("POST /build for all jobs need to be triggered: ", payload.id);
callback();
},
}
};
/////////////
// CONNECT //
/////////////
var bus = new BusPrototype({connection: connectionDetails}, jobs);
bus.connect(function(){
bus.subscribe(appKey, 'default', 'buildCreateJob', { bus_event_type : /^build_created/ });
bus.subscribe(appKey, 'default', 'buildFinishJob', { bus_event_type : /^build_finished/ });
});
var rider = new RiderPrototype({connection: connectionDetails, queues: [bus_queue], toDrive: true}, jobs);
rider.connect(function(){
rider.workerCleanup(); // optional: cleanup any previous improperly shutdown workers
rider.start();
});
/////////////////////////
// REGESTER FOR EVENTS //
/////////////////////////
rider.on('start', function(){ console.log("rider started"); });
rider.on('end', function(){ console.log("rider ended"); });
rider.on('cleaning_worker', function(worker, pid){ console.log("cleaning old worker " + worker); });
// rider.on('poll', function(queue){ console.log("rider polling " + queue); });
// rider.on('job', function(queue, job){ console.log("working job " + queue + " " + JSON.stringify(job)); });
// rider.on('reEnqueue', function(queue, job, plugin){ console.log("reEnqueue job (" + plugin + ") " + queue + " " + JSON.stringify(job)); });
// rider.on('success', function(queue, job, result){ console.log("job success " + queue + " " + JSON.stringify(job) + " >> " + result); });
rider.on('failure', function(queue, job, failure){ console.log("job failure " + queue + " " + JSON.stringify(job) + " >> " + failure); });
rider.on('error', function(queue, job, error){ console.log("error " + queue + " " + JSON.stringify(job) + " >> " + error); });
// rider.on('pause', function(){ console.log("worker paused"); });
/////////////////////////
// REQUIRE THE PACKAGE //
/////////////////////////
var BusPrototype = require('node-queue-bus').bus;
///////////////////////////
// SET UP THE CONNECTION //
///////////////////////////
var connectionDetails = {
package: "ioredis",
host: "127.0.0.1",
password: "",
port: 6379,
database: 0,
};
/////////////
// CONNECT //
/////////////
var bus = new BusPrototype({connection: connectionDetails});
bus.connect(function(){
bus.publish('build_created', {
id: 123
});
bus.publish('build_finished', {
id: 123,
status: 'SUCCESS',
eventId: 1,
sha: 'xyz',
username: 'cute',
scmContext: 'github'
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment