Requires Node >= 4, or Babel. (Note that Node 0.12's end of life is December 2016.)
Note: this is an incomplete port. As of October 2016 it contains only a few core Qless features (e.g., enqueueing, processing, succeeding, and failing jobs). However these are tested and should be usable. See TODO for missing features. Development is stalled as of October 2016, but should hopefully resume in the future. Perhaps this will be useful to someone, and everyone should feel free to contributing and submit PRs.
// myproject/enqueue.js
'use strict';
const qless = require('qless');
const client = new qless.Client();
client.queue('myqueue').put('MyClass', {foo: 'bar'}, {}, (err, res) => {
if (err) {
console.log("error: ", err, err.message);
} else {
console.log('success');
process.exit(0);
}
});
// myproject/worker.js
'use strict';
const qless = require('qless');
qless.klassFinder.setModuleDir(__dirname + '/jobs');
const client = new qless.Client();
const worker = new qless.SerialWorker('myqueue', client);
worker.run(err => {
console.log("ERROR IN WORKER: ", err);
// normally won't happen unless a serious (e.g. redis) error
// NOT triggered when a job [safely] fails
});
To enable debugging, run with:
DEBUG='qless:*' node worker.js
// myproject/jobs/MyJob.js
module.exports = {
perform(job, cb) {
console.log(job.data.foo);
cb();
// to fail:
// cb('my error');
// job.fail('my error')'; cb();
}
}
// myproject/jobs/generator2job.js
'use strict';
const co = require('co');
module.exports = function generator2job(generatorFn) {
return {
perform(job, cb) {
co(generatorFn(job)).then(val => cb(), err => cb(err))
}
};
};
// myproject/jobs/MyJob.js
'use strict';
const Promise = require('bluebird');
function *doSomeStuff() {
console.log("doing some stuff");
yield Promise.delay(2000);
console.log("I've done some stuff");
throw new Error('wombat power!');
console.log("I've done some more stuff!");
}
function *perform(job) {
console.log(`Performing job with foo=${job.data.foo}... please wait!`);
yield doSomeStuff();
console.log(`I'm out of here! Done with foo=${job.data.foo}`);
};
module.exports = require('./generator2job')(perform);
This repo contains a compiled version of the qless lua scripts in source
control. It also contains a link to the main qless-core repo as a
submodule. If the upstream qless-core scripts change, you can recompile
the lua scripts with a npm run build
. You may have to pull the latest
version of the qless-core scripts first by doing a git pull
in the
qless-core
directory.