Git Product home page Git Product logo

Comments (2)

nspragg avatar nspragg commented on June 3, 2024

The Consumer is exported, so you can extend Consumer and override method(s).

For example:

const Consumer = require('sqs-consumer');

class MySubclass extends Consumer {
    constructor(options) {
        super(options)
    }
    _processMessage(message, cb) {
        console.log('execute custom logic....');
        super._processMessage(message, cb); // delegate to parent 
    }
}

const consumer = new MySubclass(opts);
consumer.start();

However, in this case, overriding the _processMessage method is not recommended (or supported for this usage) as it's intended as a private method. This is indicated by the '_' prefix.

from sqs-consumer.

warrenca avatar warrenca commented on June 3, 2024

Thanks @nspragg, this is a very nice solution. What I did is ugly, I can't find a better way, it's embarrassing. Although it's working. Can you also suggest a better way to run multiple consumers. As you can see, I stored all the instances of consumer in an array, then run everything at the bottom.

var app = [];
Consumer.prototype.setExtraProperties = function(extraProperties) {
    this.extraProperties = extraProperties;
}

var Consumer1 = new Consumer({
  queueUrl: `https://sqs.ap-southeast-1.amazonaws.com/QNAME1`,
  handleMessage: handleMessage,
  sqs: new AWS.SQS(),
  batchSize: 10,
});

Consumer1.setExtraProperties({version:1});
Consumer1._processMessageBound = _processMessage.bind(Consumer1);
app.push(Consumer1);

var Consumer2 = new Consumer({
  queueUrl: `https://sqs.ap-southeast-1.amazonaws.com/QNAME2`,
  handleMessage: handleMessage,
  sqs: new AWS.SQS(),
  batchSize: 10,
});

Consumer2.setExtraProperties({version:2});
Consumer2._processMessageBound = _processMessage.bind(Consumer2);
app.push(Consumer2);

if (app.length > 0) {
  let consumer;
  for (let i in app) {
    consumer = app[i];
    consumer.on('error', err => {
      console.error(`error on app:  ${err.message}`);
      console.log('The app will restart due to error');
      setTimeout( () => {
        process.exit(1);
      }, 3000);
    });

    consumer.on('processing_error', err => {
      console.error(`processing_error on app:  ${err.message}`);
      console.log('The app will restart due to processing_error');
      setTimeout( () => {
        process.exit(1);
      }, 3000);
    });

    consumer.start();
  }
}

function _processMessage (message, cb) {
  var consumer = this;

  this.emit('message_received', message);
  async.series([
    function handleMessage(done) {
      consumer.handleMessage(message, done, consumer.extraProperties);
    },
    function deleteMessage(done) {
      consumer._deleteMessage(message, done);
    }
  ], function (err) {
    if (err) {
      if (err.name === SQSError.name) {
        consumer.emit('error', err, message);
      } else {
        consumer.emit('processing_error', err, message);
      }
    } else {
      consumer.emit('message_processed', message);
    }
    cb();
  });
}

from sqs-consumer.

Related Issues (20)

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.