|
| 1 | +###Channel Coffescript Usage Example |
| 2 | + |
| 3 | +1. Ensure you have a Redis server installed and running. For example, once installed, you can run it locally by |
| 4 | + |
| 5 | + redis-server & |
| 6 | + |
| 7 | +1. Require `node-redis-queue` Channel |
| 8 | + |
| 9 | + Channel = require('node-redis-queue').Channel |
| 10 | + |
| 11 | +1. Create a Channel instance and connect to Redis |
| 12 | + |
| 13 | + channel = new Channel() |
| 14 | + channel.connect -> |
| 15 | + console.log 'ready' |
| 16 | + myMainLogic() |
| 17 | + |
| 18 | + Alternatively, you can provide an existing Redis connection (i.e., a redis client instance) |
| 19 | + |
| 20 | + channel.attach redisConn |
| 21 | + |
| 22 | +1. Optionally, handle error events |
| 23 | + |
| 24 | + channel.on 'error', (error) -> |
| 25 | + console.log 'Stopping due to: ' + error |
| 26 | + process.exit() |
| 27 | + |
| 28 | +1. Optionally, handle lost connection events |
| 29 | + |
| 30 | + channel.on 'end', -> |
| 31 | + console.log 'Connection lost' |
| 32 | + |
| 33 | +1. Optionally, clear previous data from the queue, providing a callback |
| 34 | + to handle the data. |
| 35 | + |
| 36 | + channel.clear queueName, -> |
| 37 | + console.log 'cleared' |
| 38 | + doImportantStuff() |
| 39 | + |
| 40 | +1. Optionally, push data to your queue |
| 41 | + |
| 42 | + channel.push queueName, myData |
| 43 | + |
| 44 | +1. Optionally, pop data off your queue |
| 45 | + |
| 46 | + channel.pop queueName, (myData) -> |
| 47 | + console.log 'data = ' + myData |
| 48 | + |
| 49 | + or, alternatively, pop off any of multiple queues |
| 50 | + |
| 51 | + channel.popAny queueName1, queueName2, (myData) -> |
| 52 | + console.log 'data = ' + myData |
| 53 | + |
| 54 | + Once popping data from a queue, avoid pushing data to the same queue from the same connection, since |
| 55 | + a hang could result. This appears to be a Redis limitation when using blocking reads. |
| 56 | + |
| 57 | +1. When done, quit the Channel instance |
| 58 | + |
| 59 | + channel.disconnect() |
| 60 | + |
| 61 | + or, alternatively, if consuming data from the queue, end the connection |
| 62 | + |
| 63 | + channel.end() |
| 64 | + |
| 65 | + or, if there may be a number of redis commands queued, |
| 66 | + |
| 67 | + channel.shutdownSoon() |
| 68 | + |
| 69 | +###WorkQueueMgr Coffeescript Usage Example |
| 70 | + |
| 71 | +1. Ensure you have a Redis server installed and running. For example, once installed, you can run it locally by |
| 72 | + |
| 73 | + redis-server & |
| 74 | + |
| 75 | +1. Require `node-redis-queue` WorkQueueMgr |
| 76 | + |
| 77 | + WorkQueueMgr = require('node-redis-queue').WorkQueueMgr |
| 78 | + |
| 79 | +1. Create a WorkQueueMgr instance and connect to Redis |
| 80 | + |
| 81 | + mgr = new WorkQueueMgr() |
| 82 | + mgr.connect -> |
| 83 | + console.log 'ready' |
| 84 | + myMainLogic() |
| 85 | + |
| 86 | + Alternatively, you can provide an existing Redis connection (i.e., a redis client instance) |
| 87 | + |
| 88 | + mgr.attach redisConn |
| 89 | + |
| 90 | +1. Optionally, handle error events |
| 91 | + |
| 92 | + mgr.on 'error', (error) -> |
| 93 | + console.log 'Stopping due to: ' + error |
| 94 | + process.exit() |
| 95 | + |
| 96 | +1. Optionally, handle lost connection events |
| 97 | + |
| 98 | + mgr.on 'end', -> |
| 99 | + console.log 'Connection lost' |
| 100 | + |
| 101 | +1. Create a work queue instance |
| 102 | + |
| 103 | + queue = mgr.createQueue queueName |
| 104 | + |
| 105 | +1. Optionally, clear previous data from the queue, providing a callback |
| 106 | + to handle the data. |
| 107 | + |
| 108 | + queue.clear -> |
| 109 | + console.log 'cleared' |
| 110 | + doImportantStuff() |
| 111 | + |
| 112 | +1. Optionally, send data to your queue |
| 113 | + |
| 114 | + queue.send myData |
| 115 | + |
| 116 | +1. Optionally, consume data from your queue and call ack when ready to consume another data item |
| 117 | + |
| 118 | + queue.consume (myData, ack) -> |
| 119 | + console.log 'data = ' + myData |
| 120 | + ... |
| 121 | + ack() |
| 122 | + |
| 123 | + or, alternatively, |
| 124 | + |
| 125 | + queue.consume (myData, ack) -> |
| 126 | + console.log 'data = ' + myData |
| 127 | + ... |
| 128 | + ack() |
| 129 | + , arity |
| 130 | + |
| 131 | + where arity is an integer indicating the number of async callbacks to schedule in parallel. See demo 04 for example usage. |
| 132 | + |
| 133 | + If multiple queues are being consumed, they are consumed with highest priority given to the queues consumed first |
| 134 | + (i.e., in the order in which the consume statements are executed). |
| 135 | + |
| 136 | + Note that ack(true) may be used to indicate that no further data is expected from the given work queue. |
| 137 | + This is useful, for example, in testing, when a clean exit from a test case is desired. |
| 138 | + |
| 139 | + Once consuming from a queue, avoid sending data to the same queue from the same connection |
| 140 | + (i.e., the same mgr instance), since a hang could result. This appears to be a Redis limitation when using |
| 141 | + blocking reads. You can test `mgr.channel.outstanding` for zero to determine if it is OK to send on the same connection. |
| 142 | + |
| 143 | +1. Optionally, destroy a work queue if it no longer is needed. Assign null to the queue variable to free up memory. |
| 144 | + |
| 145 | + queue.destroy() |
| 146 | + queue = null |
| 147 | + |
| 148 | +1. When done, quit the WorkQueueMgr instance |
| 149 | + |
| 150 | + mgr.disconnect() |
| 151 | + |
| 152 | + or, alternatively, if consuming data from the queue, end the connection |
| 153 | + |
| 154 | + mgr.end() |
| 155 | + |
| 156 | + or, if there may be a number of redis commands queued, |
| 157 | + |
| 158 | + mgr.shutdownSoon() |
| 159 | + |
0 commit comments