arquebus
A tiny library for background workers with redis-persisted jobs.
- Robust & consistent error handling
- Aims to stay simple; less code means fewer bugs (less test coverage is needed)
- Scalable number of workers
- Optional scheduler for delayed jobs
- Bring your own redis connection
- Comprehensive
example/
and test suite (omitted from npm package)
Like resque but with a few key differences:
- Javascript centric
- Simplified API (internal optimizations make it incompatible with resque)
- Use Redis to its fullest, while reducing round-trips, with Lua scripts
Delayed jobs are for example, rather than spread around multiple lists, requiring additional round-trips to track and clean, stored in a single Sorted Set
(sorted by time) and moved to a queue List
atomically, with zremlpush.lua
. Each job is also given a cryptographically random id. Partly so you can track jobs via logging, but also to ensure the serialized job data is unique when added to a sorted set of other jobs which may have identical parameters.
Workers also use mrpop.lua
to request a job from multiple queues in a single round-trip (and without blocking the connection like brpop).
Operational vs programmer errors
Programmer errors in the form of exceptions thrown from a job handler will not be caught and will crash the process, unless you do something silly like process.on('uncaughtException')
. It would be incorrect for this library to use domains, adding complexity while attemping to handle these kinds of errors.
Taken from error handling best practices:
Operational errors represent run-time problems experienced by correctly-written programs. These are not bugs in the program. In fact, these are usually problems with something else: the system itself, the system's configuration, the network, or a remote service:
- failed to connect to server
- failed to resolve hostname
- invalid user input
- request timeout
- server returned a 500 response
- socket hang-up
- system is out of memory
Programmer errors are bugs in the program. These are things that can always be avoided by changing the code. They can never be handled properly (since by definition the code in question is broken).
- tried to read property of "undefined"
- called an asynchronous function without a callback
- passed a "string" where an object was expected
- passed an object where an IP address string was expected
Usage
let arquebus = require('arquebus')
let redis = require('redis').createClient()
enqueue()
Example
arquebus.enqueue(redis, {queue:'low', type:'ping'}, console.log)
arquebus.enqueue(redis, {queue:'hi', type:'ping', params: {
foo: true,
bar: 'baz'
}}, console.log)
createWorker()
createMultiWorker()
Example
let worker = arquebus.createWorker({
redis: require('redis').createClient(),
queues: ['hi', 'md', 'lo'],
jobs: {
ping(done, params, job) {
console.log('ping params:', params)
done(new Error('not implemented'))
}
}
})
worker.close()
createScheduler()
Example
let scheduler = createScheduler({
redis: require('redis').createClient(),
interval: 5000
})
scheduler.close()
Development
The test suite requires a disposable redis instance since fakeredis
does not support the eval
command
docker run -dp 9031:6379 redis:3.0.6-alpine
export REDIS_URI="redis://192.168.99.100:9031"
npm -s test [-- <mocha options>]