Ensure Graceful Shutdown for SQS Consumer During Deployment

I am polling my sqs consumer periodically in my app and processing messages , How can I make sure that during server shutdown ( basically while deploying new changes ) all my ongoing messages are processed before shutting down , I dont want them to be in intermediate state , as my processing involves multiple steps. Here is my processing logic , i want this effect to complete execution during interruption before server stops

export const EventsProcessor = Layer.scopedDiscard(
  Effect.gen(function* () {
    const eventService = yield* EventService;
    const messageHandles = yield* eventservice.processevents();
    if (messageHandles) {
      yield* Effect.forEach(messageHandles, (receiptHandle) =>
        Effect.gen(function* () {
          yield* eventService
            .deleteEvents(receiptHandle)
            .pipe(
              Effect.catchTag("DeleteMessageError", (e) =>
                Effect.logError(e.message, e.cause)
              )
            );
        })
      );
    }
  }).pipe(Effect.repeat(Schedule.spaced(Duration.seconds(1))))
);
Was this page helpful?