sqs-consumer icon indicating copy to clipboard operation
sqs-consumer copied to clipboard

Add the possibility to stop the poller

Open francescobarbera opened this issue 4 years ago • 2 comments

We have a use case where we need to stop the polling process; this is because if a lot of errors occurs, we need to gently stop the process, letting the current message been processed.

Is it useful to add a parameter (a function in this case) that returns a boolean that controls the handle of a new message?

Whit this process, we can check if the poller has to handle the new message or if it has to stop itself.

For example this logic can be added at the start of the poll method.

francescobarbera avatar Aug 19 '20 15:08 francescobarbera

This can be done using app.stop()

adamleemiller avatar Apr 27 '21 08:04 adamleemiller

I think what @francescobarbera means is that there's no await app.stopAndWait() or something like that.

The .stop() method is only setting a boolean to true. It is a sync operation, and it doesn't tell you when all the jobs were handled already.

What I did as a workaround was to use the stopped event, emitted by the poll function.

After all the jobs are processed, the poll function is called again. So, we can actually know that everything finished thanks to that event. And we can make it a promise.

  const shutdown = async () => {
    // If it is not running, we can return right away
    if (!consumer.isRunning) {
      return;
    }

    // Otherwise, we call the 'stop()' function and listen to the 'stopped' event
    consumer.stop();
    return new Promise((done) => {
      consumer.on('stopped', done);
    });
  }

The only problem with this is that it will wait for any ongoing long-polling request to finish, and then for any returned jobs to be fully processed until the stopped event is triggered. In the worst-case scenario, this could mean up to 20 secs + your processing time for the jobs.

So, I'd recommend keeping the waitTimeSeconds as low as possible (less than 5 seconds?), just in case.

leamarty avatar Jun 16 '21 07:06 leamarty

The later comment sounds like a bit of this to me, I'm not sure though: https://github.com/bbc/sqs-consumer/issues/234

nicholasgriffintn avatar Dec 09 '22 12:12 nicholasgriffintn