Giter Club home page Giter Club logo

amqplib-auto-recovery's Introduction

amqplib-auto-recovery

Automatic connection recovery for amqplib. Node.js 6+.

Installation

npm install amqplib-auto-recovery --save

Usage

Wrap amqp client with auto recovery decorator (like shown below). API stays absolutely the same as before *. The only difference is that amqp.connect(...)'s callback will be executed each time (re)connection is attempted (exponential backoff is turned on by default).

const amqp = require('amqplib/callback_api');
const withAutoRecovery = require('amqplib-auto-recovery');

withAutoRecovery(amqp, {
  // onError: (err) => { console.log(err.message) },
  // isErrorUnrecoverable: (err) => false
  // for more see src/amqplib-auto-recovery.js
}).connect("amqp://localhost", (err, con) => {
    if (err) {
      console.error(`Failed to connect (${err.message})`);
      return
    }
    console.info("Connection established");
    con.createChannel((err, ch) => {
      if (err) {
        console.error(`Failed to create a channel (${err.message})`);
        return
      }
      ch.prefetch(1, false);
      ch.assertQueue(consumer.queue, {durable: true});
      ch.consume(consumer.queue, (msg) => {
        // handle msg   
      });
    });
  });

* with exception to closed property (added to connection/channel), which might come in handy in situations like:

      // ...
      ch.consume(consumer.queue, (msg) => {
        // simulating a long-running task
        setTimeout(() => {
          if (ch.closed) {
            console.log("Channel had been closed before task was completed");
            return
          }
          ch.ack(msg); // throws IllegalOperationError if channel is closed
          // note that this is a standard amqplib behavior and not something
          // introduced by amqplib-auto-recovery
        }, 5000);
      });

License

MIT License

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.