1.0.9 • Published 7 years ago
one-doing-the-rest-waiting v1.0.9
OneDoingTheRestWaiting - ODTRW 1.0.9
one-doing-the-rest-waiting
is a RPC helper using Kue package. It is made to handle long async tasks: first call is executed, other later calls will wait for repsonse of the first call.
Installation
npm install one-doing-the-rest-waiting
Example
// producer.js
const rpc = require('one-doing-the-rest-waiting');
const producer = rpc.createProducer({
prefix: 'example' // prefix for Kue queue
});
producer.discover(channel => {
channel
.request('very-long-async-task', {
param1: 'your-param-1',
param2: 'your-param-2'
})
// other tasks with same `your-task-identify` will wait for this request's response
.waitFor('your-task-identify')
// handle response
.onResponse(response => {
console.log('The task response: ', response);
})
.send(); // send your request
});
// consumer.js
const rpc = require('one-doing-the-rest-waiting');
const consumer = rpc.createConsumer({
prefix: 'example' // prefix for Kue queue
});
consumer.register(channel => {
channel.onRequest((message, done) => {
// after a very long process, call done()
setTimeout(() => {
done({
response: 'your-result'
});
}, 10e3);
});
});
APIs
createProducer(config)
config
{Object} configuration for kue.createQueue()- return: a
Producer
instance
Factory method to create a Producer
instance
createConsumer(config)
config
{Object} configuration for kue.createQueue()- return: a
Consumer
instance
Factory method to create a Consumer
instance
producer.discover(callback)
callback
{Function} with parameterschannel
{ProducingChannel}
Find Consumer
instance with same Kue config
producingChannel.request(taskName, data, config)
taskName
{String}data
{Object} - optionalconfig
{Object} - optionalwaitFor
{String} Task identify to make other tasks waitttl
{Number} Time-to-live of this message (will be supported next version)
- return: a
Message
instance
message.waitFor(taskIdentify)
taskIdentify
- return: current
Message
instance
Sugar method to set config.waitFor
message.onResponse(callback)
callback
{Function} with paremetersresponse
{Object}
- return: current
Message
instance
Register callback that handles request's response
message.send()
Send the message
consumer.register(callback)
Register a Consumer
instance, let other Producer
s find it, with same Kue config
consumingChannel.onRequest(callback)
callback
{Function} with parametersrequest
{Object}done
{Function}
Register callback that handles requests