events-node: try to recreate subscription if it is cleaned up during polling

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-05-25 16:22:32 +02:00
parent 32371edb4b
commit f13bd3e987
@@ -182,11 +182,12 @@ class PluginEventsService implements EventsService {
throw await ResponseError.fromResponse(res);
}
this.#startPolling(subscriptionId, options.onEvent);
this.#startPolling(subscriptionId, options.topics, options.onEvent);
}
#startPolling(
subscriptionId: string,
topics: string[],
onEvent: EventsServiceSubscribeOptions['onEvent'],
) {
let backoffMs = POLL_BACKOFF_START_MS;
@@ -208,6 +209,21 @@ class PluginEventsService implements EventsService {
);
if (!res.ok) {
if (res.status === 404) {
this.logger.info(
`Polling event subscription resulted in a 404, recreating subscription`,
);
const putRes = await this.client.putSubscription(
{
path: { subscriptionId },
body: { topics },
},
{ token },
);
if (!putRes.ok) {
throw await ResponseError.fromResponse(res);
}
}
throw await ResponseError.fromResponse(res);
}
backoffMs = POLL_BACKOFF_START_MS;