feat: swap to async exit hook

This commit is contained in:
Eli Bosley
2025-01-29 11:47:30 -05:00
parent 0c6f44da35
commit c7d4e39287
2 changed files with 13 additions and 29 deletions

View File

@@ -9,7 +9,7 @@ import https from 'https';
import type { RawServerDefault } from 'fastify'; import type { RawServerDefault } from 'fastify';
import CacheableLookup from 'cacheable-lookup'; import CacheableLookup from 'cacheable-lookup';
import exitHook from 'exit-hook'; import { asyncExitHook, gracefulExit } from 'exit-hook';
import { WebSocket } from 'ws'; import { WebSocket } from 'ws';
import { logger } from '@app/core/log'; import { logger } from '@app/core/log';
@@ -91,17 +91,19 @@ try {
// Start webserver // Start webserver
server = await bootstrapNestServer(); server = await bootstrapNestServer();
// On process exit stop HTTP server asyncExitHook(
exitHook(async (signal) => { async (signal) => {
console.log('exithook', signal); console.log('exithook', signal);
await server?.close?.(); await server?.close?.();
// If port is unix socket, delete socket before exiting // If port is unix socket, delete socket before exiting
unlinkUnixPort(); unlinkUnixPort();
shutdownApiEvent(); shutdownApiEvent();
process.exit(0); gracefulExit();
}); },
{ wait: 9999 }
);
// Start a loop to run the app // Start a loop to run the app
await new Promise(() => {}); await new Promise(() => {});
} catch (error: unknown) { } catch (error: unknown) {
@@ -115,5 +117,5 @@ try {
} }
shutdownApiEvent(); shutdownApiEvent();
// Kill application // Kill application
process.exit(1); gracefulExit(1);
} }

View File

@@ -1,18 +0,0 @@
import { beforeAll, expect, test } from 'vitest';
import { store } from '@app/store';
import { loadConfigFile } from '@app/store/modules/config';
import { anonymiseOrigins } from '@app/unraid-api/cli/report.command';
beforeAll(async () => {
// Load cfg into store
await store.dispatch(loadConfigFile());
});
test('anonymise origins removes .sock origins', async () => {
expect(anonymiseOrigins(['/var/run/test.sock'])).toEqual([]);
});
test('anonymise origins hides WAN port', async () => {
expect(anonymiseOrigins(['https://domain.tld:8443'])).toEqual(['https://domain.tld:WANPORT']);
});