Skip to content

Handle put events retry by default #22

@CorentinDoue

Description

@CorentinDoue

Bus.put(events) returns { FailedEntryCount, Entries } which must be handled manually to avoid losing events.

Most projects that I know that use typebridge forget to handle these return.

I recently encountered a bug related to this and I implemented a retry strategy

export const putEventsWithRetry = async (
  eventBus: Bus,
  events: BusPutEvent[],
  retryCount = 0,
): Promise<void> => {
  const { FailedEntryCount, Entries } = await eventBus.put(events);
  if (FailedEntryCount !== undefined && FailedEntryCount > 0) {
    if (retryCount > 10) {
      throw new Error('Failed to put events in Bus after 10 retries');
    }
    console.warn(`${FailedEntryCount} events failed to be put in Bus:`);
    throwIfNil(Entries, 'Entries should not be undefined if FailedEntryCount is greater than 0');
    const failedEvents = Entries.reduce<BusPutEvent[]>(
      (failedEventsAggregator, { ErrorCode, ErrorMessage }, index) => {
        if (ErrorCode === undefined) {
          return failedEventsAggregator;
        }

        console.warn(`EventBus error: ${ErrorCode} ${ErrorMessage ?? ''}`);
        const failedEvent = events[index];
        throwIfNil(failedEvent, 'Failed event should not be undefined');

        return [...failedEventsAggregator, failedEvent];
      },
      [],
    );
    await putEventsWithRetry(eventBus, failedEvents, retryCount + 1);
  }
};

Can this be the default behavior of Bus.put? and rename the current method put into putWithoutRetrywhcih could be useful to handle retry externaly (for exemple with a step function)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions