Skip to content

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

TypeScript fluxerfluxerapp

fix(api): start embedded worker lanes once search is ready

Hampusfbace7eec2ce
diff --git a/fluxer_api/src/api/app/APILifecycle.ts b/fluxer_api/src/api/app/APILifecycle.ts
index b9bca9101..0e6931de3 100644
--- a/fluxer_api/src/api/app/APILifecycle.ts
+++ b/fluxer_api/src/api/app/APILifecycle.ts
@@ -58,6 +58,10 @@ let jobQueue: ApiJobQueue | null = null;
let searchInitRetryTimer: ReturnType<typeof setTimeout> | null = null;
let searchInitStopped = false;
let searchInitInFlight: Promise<void> | null = null;
+let markSearchReady: () => void = () => {};
+const searchReady = new Promise<void>((resolve) => {
+ markSearchReady = resolve;
+});
const SEARCH_INIT_RETRY_MIN_MS = 5_000;
const SEARCH_INIT_RETRY_MAX_MS = 60_000;
const unregisterMetricsSections: Array<() => void> = [];
@@ -113,6 +117,10 @@ export function getApiJobQueue(): ApiJobQueue | null {
return jobQueue;
}
+export function whenSearchReady(): Promise<void> {
+ return searchReady;
+}
+
function unsupportedDatabaseBackend(backend: never): never {
throw new Error(`Unsupported database backend during shutdown: ${String(backend)}`);
}
@@ -122,6 +130,7 @@ async function initializeSearchAndWarmup(
logger: LoggerInterface,
): Promise<void> {
await initializeSearch(getCacheService());
+ markSearchReady();
if (searchInitStopped) return;
logger.info('Search initialized');
const warmupLockKey = 'fluxer:search:warmup:admin';
diff --git a/fluxer_api/src/api/worker/EmbeddedWorker.ts b/fluxer_api/src/api/worker/EmbeddedWorker.ts
index 3903ff1ac..76c7bf1bb 100644
--- a/fluxer_api/src/api/worker/EmbeddedWorker.ts
+++ b/fluxer_api/src/api/worker/EmbeddedWorker.ts
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
-import {getApiJobQueue} from '@app/api/app/APILifecycle';
+import {getApiJobQueue, whenSearchReady} from '@app/api/app/APILifecycle';
import {Logger} from '@app/api/Logger';
import {getSnowflakeService} from '@app/api/middleware/ServiceRegistry';
import {clearWorkerDependencies, setWorkerDependencies} from '@app/api/worker/WorkerContext';
@@ -9,6 +9,10 @@ import type {WorkerRuntime} from '@app/api/worker/WorkerMain';
let starting: Promise<WorkerRuntime | null> | null = null;
let stopping: Promise<void> | null = null;
let runtime: WorkerRuntime | null = null;
+let cancelStart: () => void = () => {};
+const startCancelled = new Promise<void>((resolve) => {
+ cancelStart = resolve;
+});
async function start(cores: number): Promise<WorkerRuntime | null> {
const jobs = getApiJobQueue();
@@ -20,6 +24,8 @@ async function start(cores: number): Promise<WorkerRuntime | null> {
import('@app/api/worker/WorkerMain'),
]);
if (stopping) return null;
+ await Promise.race([whenSearchReady(), startCancelled]);
+ if (stopping) return null;
const dependencies = await initializeWorkerDependencies(getSnowflakeService());
if (stopping) return null;
setWorkerDependencies(dependencies);
@@ -35,6 +41,7 @@ export function startEmbeddedWorker(cores: number): Promise<void> {
}
export function stopEmbeddedWorker(): Promise<void> {
+ cancelStart();
stopping ??= (async () => {
if (!starting) return;
const [result] = await Promise.allSettled([starting]);