Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(api): wait for in-flight search retry before shutdown

Hampusd5235af21359
diff --git a/fluxer_api/src/api/app/APILifecycle.ts b/fluxer_api/src/api/app/APILifecycle.ts
index 01b403134..7a883ce22 100644
--- a/fluxer_api/src/api/app/APILifecycle.ts
+++ b/fluxer_api/src/api/app/APILifecycle.ts
@@ -56,6 +56,7 @@ import {getDefaultPostgresClient, initPostgres, shutdownPostgres} from '@pkgs/po
let jsConnectionManager: JetStreamConnectionManager | null = null;
let searchInitRetryTimer: ReturnType<typeof setTimeout> | null = null;
let searchInitStopped = false;
+let searchInitInFlight: Promise<void> | null = null;
const SEARCH_INIT_RETRY_MIN_MS = 5_000;
const SEARCH_INIT_RETRY_MAX_MS = 60_000;
const unregisterMetricsSections: Array<() => void> = [];
@@ -109,6 +110,7 @@ async function initializeSearchAndWarmup(
logger: LoggerInterface,
): Promise<void> {
await initializeSearch(getCacheService());
+ if (searchInitStopped) return;
logger.info('Search initialized');
const warmupLockKey = 'fluxer:search:warmup:admin';
const warmupLockToken = randomUUID();
@@ -145,10 +147,14 @@ function scheduleSearchInitRetry(
const delay = Math.min(SEARCH_INIT_RETRY_MIN_MS * 2 ** attempt, SEARCH_INIT_RETRY_MAX_MS);
searchInitRetryTimer = setTimeout(() => {
searchInitRetryTimer = null;
- initializeSearchAndWarmup(kvClient, logger).catch((error) => {
- logger.error({error, attempt: attempt + 1}, 'Search initialisation failed, retrying in the background');
- scheduleSearchInitRetry(kvClient, logger, attempt + 1);
- });
+ searchInitInFlight = initializeSearchAndWarmup(kvClient, logger)
+ .catch((error) => {
+ logger.error({error, attempt: attempt + 1}, 'Search initialisation failed, retrying in the background');
+ scheduleSearchInitRetry(kvClient, logger, attempt + 1);
+ })
+ .finally(() => {
+ searchInitInFlight = null;
+ });
}, delay);
}
@@ -337,6 +343,7 @@ export function createShutdown(config: APIConfig, logger: LoggerInterface): () =
clearTimeout(searchInitRetryTimer);
searchInitRetryTimer = null;
}
+ await searchInitInFlight;
try {
await shutdownSearch();
logger.info('Search service shut down');