diff --git a/README.md b/README.md index f55c61b7..7ac9101a 100644 --- a/README.md +++ b/README.md @@ -141,25 +141,27 @@ Before publishing a message do not forget to set the destination queue of the me 'use strict'; const {Message, Producer} = require('redis-smq'); -const message = new Message(); - -message - .setBody({hello: 'world'}) - .setTTL(3600000) // in millis - .setQueue('test_queue'); - -message.getId() // null - const producer = new Producer(); -producer.produce(message, (err) => { - if (err) console.log(err); - else { - const msgId = message.getId(); // string - console.log('Successfully produced. Message ID is ', msgId); - } -}); +producer.run((err) => { + if (err) throw err; + const message = new Message(); + message + .setBody({hello: 'world'}) + .setTTL(3600000) // in millis + .setQueue('test_queue'); + message.getId() // null + producer.produce(message, (err) => { + if (err) console.log(err); + else { + const msgId = message.getId(); // string + console.log('Successfully produced. Message ID is ', msgId); + } + }); +}) ``` +Starting with v7.0.6, before producing messages you need first to run your producer instance. + See [Producer Reference](/docs/api/producer.md) for more details. #### Consumer Class diff --git a/docs/api/message.md b/docs/api/message.md index 628a87a8..a4799154 100644 --- a/docs/api/message.md +++ b/docs/api/message.md @@ -287,21 +287,19 @@ message.getBody(); // 123 ### Message.prototype.getId() ```javascript -const { Message, Producer } = require('redis-smq'); +const { Message } = require('redis-smq'); const message = new Message(); message.setQueue('test_queue').setBody('some data'); message.getId(); // null - -new Producer().produce(message, (err) => { +producer.produce(message, (err) => { if (err) console.log(err); else { const messageId = message.getId(); // c53d1766-0e56-4362-8aab-ef70c4eb03ad console.log('Message ID is ', messageId); } }) - ```` ### Message.prototype.getTTL() @@ -364,13 +362,12 @@ message.getCreatedAt(); // 1530613595087, in millis ### Message.prototype.getPublishedAt() ```javascript -const {Message, Producer} = require('redis-smq'); +const { Message } = require('redis-smq'); const message = new Message(); message.setQueue('test_queue').setBody('Test message'); message.getPublishedAt(); // null -const producer = new Producer(); producer.produce(message, (err) => { if (err) console.log(err); else { @@ -382,7 +379,7 @@ producer.produce(message, (err) => { ### Message.prototype.getScheduledAt() ```javascript -const {Message, Producer} = require('redis-smq'); +const { Message } = require('redis-smq'); const message = new Message(); message.setScheduledRepeat(6); @@ -390,7 +387,6 @@ message.getScheduledAt(); // null message.setQueue('test_queue'); message.setBody('Test message'); -const producer = new Producer(); producer.produce(message, (err) => { if (err) console.log(err); else { diff --git a/docs/api/producer.md b/docs/api/producer.md index 97c93127..4afbd8f5 100644 --- a/docs/api/producer.md +++ b/docs/api/producer.md @@ -19,6 +19,25 @@ const producer = new Producer(config); - `config` *(object): Optional.* See [Configuration](/docs/configuration.md) for more details. +### Producer.prototype.run() + +Start your producer instance. No connection to Redis server is opened until this method is called. + +Starting with v7.0.6 producer instances are no longer automatically started upon creation. + +You have to run a producer before producing messages. + +**Syntax** + +```javascript +run(cb); +``` + +**Parameters** +- `cb(err, status)` *(function): Required.* Callback function. + - `err` *(Error | null | undefined).* Error object. + - `status` *(boolean).* Indicate whether the operation completed successfully. + ### Producer.prototype.produce() **Syntax** @@ -33,17 +52,14 @@ producer.produce(message, cb); - `cb(err)` *(function): Required.* Callback function. ```javascript -const { Message, Producer } = require('redis-smq'); +const { Message } = require('redis-smq'); const message = new Message(); - message .setBody({ hello: 'world' }) .setTTL(3600000) .setScheduledDelay(10000) // in millis .setQueue('test_queue'); - -const producer = new Producer(); producer.produce(message, (err) => { if (err) console.log(err); else console.log('Successfully produced') @@ -58,23 +74,6 @@ Before publishing a message, make sure that: Otherwise, an error will be returned. -### Producer.prototype.run() - -Start your producer instance. No connection to Redis server is opened until this method is called. - -Contrary to consumer instances, producer instances are automatically started upon creation. - -**Syntax** - -```javascript -run(cb); -``` - -**Parameters** -- `cb(err, status)` *(function): Required.* Callback function. - - `err` *(Error | null | undefined).* Error object. - - `status` *(boolean).* Indicate whether the operation completed successfully. - ### Producer.prototype.shutdown() Gracefully shut down your producer instance and go offline. diff --git a/docs/priority-queues.md b/docs/priority-queues.md index d56eac7e..23cda649 100644 --- a/docs/priority-queues.md +++ b/docs/priority-queues.md @@ -41,15 +41,13 @@ See [Message Priority](/docs/api/message.md#messagemessagepriority) for more det Before producing a message with a priority, make sure that queue of the message is a priority queue. Otherwise, an error will be returned. ```javascript -const {Message, Producer} = require('redis-smq'); +const { Message } = require('redis-smq'); const msg1 = new Message(); msg1.setPriority(Message.MessagePriority.HIGH).setQueue('test_queue'); - -const producer = new Producer(); producer.produce(msg1, (err) => { - if (err) console.log(err); - else console.log('Successfully produced') + if (err) console.log(err); + else console.log('Successfully produced') }); ``` diff --git a/docs/scheduling-messages.md b/docs/scheduling-messages.md index 3e0fc9e2..95da0522 100644 --- a/docs/scheduling-messages.md +++ b/docs/scheduling-messages.md @@ -14,9 +14,7 @@ using the `produce()` method. ```javascript 'use strict'; -const { Message, Producer } = require('redis-smq'); - -const producer = new Producer(); +const { Message } = require('redis-smq'); const message = new Message(); message