1.0.3 • Published 7 years ago
redis-fabric v1.0.3
Usage
A simple example.
let Fabric = require('redis-fabric');
const fabric = Fabric();
// variables
const jobs_queue = "jobs";
fabric.subscribe(jobs_queue, (msg) => {
console.log(msg);
}, true);
fabric.emit(jobs_queue, 'a new message');
API
Initialization of the framework.
const fabric = Fabric(url, visibility_timeout, monitor_interval)
url
- is the connection url to the redis server. A default is set by not specifying the value to'redis://localhost:6379'
;visibility_timeout
- the period of time that messages are hidden from the monitor; if not specified the default value is 30 secondsmonitor_interval
- interval time in seconds that the monitor uses to check for dead messages; if not specified the default value is 5 seconds.
Subscribe to a queue
Example:
fabric.subscribe(jobs_queue, (msg) => {
console.log(msg);
}, true);
The complete function definition is
fabric.subscribe(queue, callback, ack)
queue
- (string) is the queue you want to listen to. It automaticly generates a new processing queue with the formatqueue_processing
in order to store all messages that are in the process of being resolved. Every message has an expiration period equal to thevisibility_timeout
. If the message expires and it wasn't deleted from thequeue_processing
it will be retried on thequeue
.callback
- The function that is called every time a message is spotted on thequeue
ack
- !WARNING this must be set to true so the monitor doesn't retry the message after thevisibility_timeout
Emit a message
fabric.emit(jobs_queue, 'a new message');
The complete function definition is
fabric.emit(queue, message);
queue
- The queue u want to send a message to.message
- The message. It will be automatically converted into a string. the message will be encapsulated in an object that includestimestamp
and a uniqueid
and apayload
. ex:{"id":"BkJV3xpag","timestamp":"2017-04-13T13:24:55.245Z","payload":"A message with id:BkJV3xpag"}
Running a dead messages monitor
If you need to retry the messages that might failed during processing you can run a monitor that checks every monitor_interval
seconds for timed out messages. If a message was not processed during the visibility_timeout
the it will be retried by being put on
the job queue
.
const fabric = require('redis-fabric')('redis://localhost:6379', null, 5);
const jobs_queue = "jobs";
fabric.monitor(jobs_queue);
In this example the monitor will check every 5 seconds for dead messages. There is no need to specify the visibility_timeout
as
it is not used in the monitor functionality.
const fabric = require('redis-fabric')();
const jobs_queue = "jobs";
fabric.monitor(jobs_queue);
Here we only use default values (visibility_timeout
30 seconds, monitor_interval
5 seconds) for the monitor that will start listening to the localhost
Redis server.