1.0.3 • Published 7 years ago

redis-fabric v1.0.3

Weekly downloads
1
License
MIT
Repository
-
Last release
7 years ago

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 seconds
  • monitor_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 format queue_processing in order to store all messages that are in the process of being resolved. Every message has an expiration period equal to the visibility_timeout. If the message expires and it wasn't deleted from the queue_processing it will be retried on the queue.
  • callback - The function that is called every time a message is spotted on the queue
  • ack - !WARNING this must be set to true so the monitor doesn't retry the message after the visibility_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 includes timestamp and a unique id and a payload. 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.

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago