ssut / nestjs-sqs

A project to make SQS easier to use within NestJS, with decorator-based handling and seamless NestJS-way integration.
MIT License
214 stars 53 forks source link

feat: add support for consumer stop options #76

Closed cassiolacerda closed 8 months ago

cassiolacerda commented 8 months ago

Context

The bbc/sqs-consumer library gives support for stop options, where it's possible to abort immediately a consumer without await for polling to complete.

Problem

Currently, the ssut/nestjs-sqs doesn't give support to set these options and call stop method without any params. Abort behaviour is an important option in a graceful shutdown strategy and it would be a good improvement for the lib support that too.

Solution

The stop options can be used in a global and / or consumer specific approach:

  1. Used for all consumers: globalStopOptions Enable stopping of all registered consumers immediately after shutdown.
    @Module({
    imports: [
    SqsModule.register({
      consumers: [
        {
          name: 'queue-1',
          queueUrl: "http://localhost:9324/queue/queue-1.fifo",
        },
        {
          name: 'queue-2',
          queueUrl: "http://localhost:9324/queue/queue-2.fifo",
        }
      ],
      producers: [],
      globalStopOptions: {
        abort: true,
      }
    }),
    ],
    })
    class AppModule {}
  2. Used for specific consumer: stopOptions Enable stopping of specific consumers immediately after shutdown.
    @Module({
    imports: [
    SqsModule.register({
      consumers: [
        {
          name: 'queue-1',
          queueUrl: "http://localhost:9324/queue/queue-1.fifo",
          stopOptions: {
            abort: true,
          },
        },
        {
          name: 'queue-2',
          queueUrl: "http://localhost:9324/queue/queue-2.fifo",
        }
      ],
      producers: []
    }),
    ],
    })
    class AppModule {}
  3. Used for all consumers with consumer specific override: globalStopOptions + stopOptions Enable stopping of all registered consumers immediately after shutdown, except consumer queue-1 because its stopOptions override the globalStopOptions.
    @Module({
    imports: [
    SqsModule.register({
      consumers: [
        {
          name: 'queue-1',
          queueUrl: "http://localhost:9324/queue/queue-1.fifo",
          stopOptions: {
            abort: false,
          },
        },
        {
          name: 'queue-2',
          queueUrl: "http://localhost:9324/queue/queue-2.fifo",
        }
      ],
      producers: [],
      globalStopOptions: {
        abort: true,
      }
    }),
    ],
    })
    class AppModule {}

    ⚠️ Important: I was unable to add any tests because I had problems running the tests locally.

Verdini commented 8 months ago

@ssut This feature is very helpful, especially if you have a limited graceful shutdown time, which is common in applications running in Kubernetes clusters. I would be grateful if you consider this Pull Request.

ssut commented 8 months ago

Thanks for your contribution through this PR. I'm preparing for a new release that will include this PR, fixing tests, and bump up some deps. As my availability these days is limited during the weekdays, it'll take a sec.