beanstalkd-worker
v1.3.1
Published
High level library for running beanstalkd workers in Node.js
Downloads
185
Readme
Beanstalkd Worker for node
Features
- Wait for job completion
- Child jobs (keeping parent alive till done)
How-To
Setup
npm install --save beanstalkd-worker
import BeanstalkdWorker from 'beanstalkd-worker';
const worker = new BeanstalkdWorker(
host, // beanstalkd host
port, // beanstalkd port
);
Handling connection errors
It's possible to add a onConnectionError
callback when creating the beanstalkd-worker
.
This callback is called when the connection to the queue fails.
const worker = new BeanstalkdWorker(host, port, {
onConnectionError: (err, tube) => {
// When there is a connection error, you can stop the watchers
tube.stop();
// Eventually, you can restart the watchers
tube.start();
// You can access also the complete worker:
tube.worker.stop();
// Call process.exit if you want to exit your service completly
process.exit(1);
},
});
Spawning jobs
worker.spawn(tube, {
// job payload/values
}, {
delay: 0,
priority: 1000,
timeout: 10 * 60 * 1000 // ms
}).then(function (job) {
console.log(job.id);
});
Handling jobs
worker.handle(tube, function (payload) {
// Complete job
return Promise.resolve();
// Job error
return Promise.reject();
// Spawn a job
this.spawn(someTub);
// Refresh timeout
this.touch();
// Spawn child job and wait for completion before completing this job
await this.child(anotherTube, {/* payload */});
// Await another job
await this.wait(anotherTube, jobId);
// Puts current job back in queue with delay, does not affect retries counter
return this.delay(5000); // ms, default: original timeout
}, {
tries: 3, // Total amount of tries including the first one
backoff: {
initial: 60 * 1000, // ms
exponential: 1.5 // multiple backoff by N each try
}
});
worker.start(); // Enable handlers and start processing jobs, make sure handlers are setup before calling start
Keep in mind that worker will spawn a connection equal to width * amount of tubes. You'll want to make sure that your server is configured to handle that amount of connections (ulimit).
Debugging
Use DEBUG=beanstalkd-worker*
to enable verbose debugging.