rabbit-worker v0.0.1
rabbit-worker
Very basic node module for creating and consuming from RabbitMQ work queues
Why?
Because I am writing a lot of small services that use this sort of queue. It is just a light rabbit.js wrapper (if for some weird reason you were hoping to use this project, I would instead you use that library directly) that defaults to persistant queues and automatically reconnects on connection failures.
Synopsis
var rabbitWorker = require('rabbit-worker');
var worker = new rabbitWorker.Worker('localhost', 'work_queue_1', function (message, ack) {
console.log('got message', message);
ack(); //important! Signals to the queue that the work has been completed
});
var tasker = new rabbitWorker.Tasker('localhost', 'work_queue_1');
if (tasker.isReady()) {
tasker.publish('New message');
} else {
tasker.once('ready', tasker.publish.bind(tasker, 'New message');
}
TODO
- Support for publisher confirms would be rad (I am currently living dangerously and not using them in the interest of code simplicity). Would require either a modification in the upstream rabbit.js library or that this module be converted to use amqplib as its upstream library directly (amqplib being the core module that rabbit.js relies on).
jsdoc-to-markdown output
rabbit-worker
Light wrapper over the rabbit module to easily create persistent workers and taskers.
Version: 0.0.1
Author: notnarb
- rabbit-worker
- ~Worker(rabbitserver, routingKey, workerFunction)
- ~Tasker(rabbitserver, routingKey) ⇐ event.EventEmitter
- .publish(message)
- .isReady() ⇒ Boolean
rabbit-worker~Worker(rabbitserver, routingKey, workerFunction)
connects to the specified rabbitmq server as a worker for the specified routing key
Kind: inner method of rabbit-worker
Param | Type | Description |
---|---|---|
rabbitserver | String | The hostname of the rabbit server to use |
routingKey | String | the routing key to listen on |
workerFunction | function | the function to call for each message. This function is called with 2 arguments: 'message', and 'ack'. Message is a string representation of the data passed and 'ack' is a function that must be called to acknowledge the function. Note: for simplicity: the function 'requeue' and 'discard' are omitted though they wouldn't be hard to add |
Example
var worker = new Worker('localhost', 'work_queue_1', function (message, ack) {
console.log('got message', message);
ack(); //important! Signals to the queue that the work has been completed
})
rabbit-worker~Tasker(rabbitserver, routingKey) ⇐ event.EventEmitter
connects to the specified rabbitmq server as a publisher for the specified routing key. Emits a 'ready' event when it can start taking publish commands.
Kind: inner method of rabbit-worker
Extends: event.EventEmitter
Param | Type | Description |
---|---|---|
rabbitserver | String | The hostname of the rabbit server to use |
routingKey | String | the routing key to listen on |
Example
var tasker = new Tasker('localhost', 'work_queue_1');
if (tasker.isReady()) {
tasker.publish('New message');
} else {
tasker.once('ready', tasker.publish.bind(tasker, 'New message');
}
- ~Tasker(rabbitserver, routingKey) ⇐ event.EventEmitter
- .publish(message)
- .isReady() ⇒ Boolean
tasker.publish(message)
Publishes a message to the queue
Kind: instance method of Tasker
Throws:
- Error - if the queue is currently unavailable, this throws an error
Param | Type | Description |
---|---|---|
message | String | message to publish to the queue |
tasker.isReady() ⇒ Boolean
Checks to see if the tasker is currently connected to the queue
Kind: instance method of Tasker
Returns: Boolean - - true if currently can accept publish arguments
9 years ago