hotrod-worker

0.1.1 • Public • Published

hotrod-worker

Utilities for building RabbitMQ-based workers

Install

npm install hotrod-worker

Overview

This module helps build a RabbitMQ worker which processes 1 message at a time and acks each message if processed successfully.

Example usage:

var i = 0;
var stop = worker.run(RABBIT_URL, QUEUE_NAME, RABBIT_CONN_RETRY_SECS, function processMsg(msg) {
    var body = msg.content.toString();
    console.log('Processing message:', body);
    return Promise.resolve(++i); // optional return value
}, function msgProcessed(msgIndex) {
    // optional `msgProcessed` function called with return value from `processMsg`
    console.log('Processed message', msgIndex);
});

See usage in hotrod-rabbit-to-http also.

API

  • run(rabbitUrl, queueName, connRetrySecs, processMessage, processedResultCB)
    • rabbitUrl [string] - URL for RabbitMQ. Something like amqp://guest:guest@localhost:5672
    • queueName [string] - Rabbit queue name
    • connRetrySecs [number] - If the RabbitMQ connection dies, how often to rety connection. If falsey, the process will exit when connection dies.
    • processMessage [function(msg)] - Do something with the message from RabbitMQ. Can optionally return a promise to indicate when async operations have handled message successfully. If no error thrown from function or if optional returned promise resolves successfully, then message is acked back to RabbitMQ. Returned or resolved result is passed to optional msgProcessed function
    • msgProcessed [function(result)] optional - Invoked when message processed successfully and acked back to RabbitMQ. Will be passed any returned or resolved result from processMessage

Some other lower-level functions are also exposed if you want to customize further. Have a look at the source

Notes

Because the processMessage function can return a promise to indicate success, you can use libraries like promise-retry to make a best effort to handle message and then let the process crash if it fails.

License

The MIT License (MIT)

Copyright (c) Panoptix CC

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Dependencies (3)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i hotrod-worker

    Weekly Downloads

    1

    Version

    0.1.1

    License

    MIT

    Last publish

    Collaborators

    • emertechie