Skip to content

Latest commit

 

History

History
219 lines (139 loc) · 6.32 KB

JAVASCRIPT_USAGE_EXAMPLES.md

File metadata and controls

219 lines (139 loc) · 6.32 KB

###Channel Javascript Usage Example

  1. Ensure you have a Redis server installed and running. For example, once installed, you can run it locally by

     redis-server &
    
  2. Require node-redis-queue Channel

     var Channel = require('node-redis-queue').Channel;
    
  3. Create a Channel instance and connect to Redis

     var channel = new Channel();  
     channel.connect(function() {
       console.log('ready');
       myMainLogic();
     });
    

Alternatively, you can provide an existing Redis connection (i.e., a redis client instance)

    channel.attach(redisConn);
  1. Optionally, handle error events

     channel.on('error', function(error) {  
         console.log('Stopping due to: ' + error);  
         process.exit();
     });
    
  2. Optionally, handle lost connection events

     channel.on('end', function() {
       console.log('Connection lost');
     });
    
  3. Optionally, handle drain events

     channel.on('drain', function() {
       console.log('Command queue too full -- slowing input');
       throttleInput();
     });
    
  4. Optionally, handle timeout events

     channel.on('timeout', function(queueNames...) {
       console.log('Timed out waiting for ', queueNames);
     });
    
  5. Optionally, clear previous data from the queue, providing a callback.

     channel.clear(function() {
       console.log('cleared');
       doImportantStuff();
     });
    
  6. Optionally, push data to your queue

     channel.push(queueName, myData);
    
  7. Optionally, pop data off your queue, providing a callback to handle the data

     channel.pop(queueName, function(myData) {  
         console.log('data = ' + myData); 
     });
    

    or, alternatively, pop off any of multiple queues

     channel.popAny(queueName1, queueName2, function(myData) {
         console.log('data = ' + myData);
     });
    

    Once popping data from a queue, avoid pushing data to the same queue from the same connection, since a hang could result. This appears to be a Redis limitation when using blocking reads.

  8. To avoid blocking indefinitely there are variations of the above that accept a timeout parameter.

     channel.popTimeout(queueName, timeout, function(myData) {
         console.log('data = ' + myData);
     });
    

    or, alternatively, pop off any of multiple queues

     channel.popAnyTimeout(queueName1, queueName2, timeout, function(myData) {
         console.log('data = ' + myData);
     });
    

    where the timeout is in seconds.

  9. When done, quit the Channel instance

     channel.disconnect();
    

or, alternatively, if consuming data from the queue, end the connection

    channel.end();

or, if there may be a number of redis commands queued,

    channel.shutdownSoon();

###WorkQueueMgr Javascript Usage Example

  1. Ensure you have a Redis server installed and running. For example, once installed, you can run it locally by

     redis-server &
    
  2. Require node-redis-queue WorkQueueMgr

     var WorkQueueMgr = require('node-redis-queue').WorkQueueMgr;
    
  3. Create a WorkQueueMgr instance and connect to Redis

     var mgr = new WorkQueueMgr();  
     mgr.connect(function () {
       console.log('ready');
       myMainLogic();
     });
    

Alternatively, you can provide an existing Redis connection (i.e., a redis client instance)

    mgr.attach(redisConn);
  1. Optionally, handle error events

     mgr.on('error', function(error) {   
         console.log('Stopping due to: ' + error);  
         process.exit();
     });
    
  2. Optionally, handle lost connection events

     mgr.on('end', function {   
       console.log('Connection lost');
     });
    
  3. Optionally, handle drain events

     mgr.on('drain', function() {
       console.log('Command queue too full -- slowing input');
       throttleInput();
     });
    
  4. Optionally, handle timeout events

     mgr.on('timeout', function(queueNames...) {
       console.log('Timed out waiting for ', queueNames);
     });
    
  5. Create a work queue instance

     var queue = mgr.createQueue(queueName);
    
  6. Optionally, clear previous data from the queue, providing a callback to handle the data.

     queue.clear(queueName, function() {
       console.log('cleared');
       doImportantStuff();
     });
    

    Alternatively, you can clear multiple queues at once.

     mgr.clear(queueName1, queueName2, function() {
       console.log('cleared both queues');
       doImportantStuff();
     });
    
  7. Optionally, send data to your queue

     queue.send(myData);
    
  8. Optionally, consume data from your queue and call ack when ready to consume another data item

     queue.consume(function(myData, ack) {  
         console.log('data = ' + myData);   
         ...
         ack();
     });
    

    or, alternatively,

     queue.consume(function(myData, ack) {
         console.log('data = ' + myData);
         ...
         ack();
     }, arity);
    

    where arity is an integer indicating the number of async callbacks to schedule in parallel. See demo 04 for example usage.

    If multiple queues are being consumed, they are consumed with highest priority given to the queues consumed first (i.e., in the order in which the consume statements are executed).

    Note that ack(true) may be used to indicate that no further data is expected from the given work queue. This is useful, for example, in testing, when a clean exit from a test case is desired.

    Once consuming from a queue, avoid sending data to the same queue from the same connection (i.e., the same mgr instance), since a hang could result. This appears to be a Redis limitation when using blocking reads. You can test mgr.channel.outstanding for zero to determine if it is OK to send on the same connection.

    Following the arity parameter, one may provide a timeout parameter so that the operations do not block indefinitely.

  9. Optionally, destroy a work queue if it no longer is needed. Assign null to the queue variable to free up memory.

     queue.destroy();
     queue = null;
    
  10. When done, quit the WorkQueueMgr instance

     mgr.disconnect();
    

or, alternatively, if consuming data from the queue, end the connection

    mgr.end();

or, if there may be a number of redis commands queued,

    mgr.shutdownSoon();