fix(gateway): make process shutdown idempotent

This commit is contained in:
2026-07-31 16:55:57 +00:00
parent 1dd1610cb8
commit 81e58a21bc
5 changed files with 170 additions and 12 deletions
+19 -3
View File
@@ -25,6 +25,7 @@ import { createGatewayOrchestrator } from './orchestrator/orchestratorFactory.js
import { appRouter } from './router.js';
import { RepositoryProfileStatusService } from './lobby/profileStatusService.js';
import { registerAccountIconInternalRoute } from './auth/accountIconInternalRoute.js';
import { installGatewayShutdownController } from './lifecycle/shutdownController.js';
export const createGatewayApiServer = async () => {
const config = resolveGatewayApiConfigFromEnv();
@@ -131,8 +132,23 @@ export const createGatewayApiServer = async () => {
export const runGatewayApiServer = async (): Promise<void> => {
const { app, config } = await createGatewayApiServer();
await app.listen({
host: config.host,
port: config.port,
const shutdown = installGatewayShutdownController({
close: () => app.close(),
onStopping: (reason) => app.log.info({ reason }, 'gateway API stopping'),
onError: (error, reason) => {
app.log.error({ err: error, reason }, 'gateway API shutdown failed');
process.exitCode = 1;
},
});
app.addHook('onClose', async () => shutdown.dispose());
try {
await app.listen({
host: config.host,
port: config.port,
});
} catch (error) {
shutdown.dispose();
await app.close();
throw error;
}
};