Skip to content

Commit 5ff150a

Browse files
committed
fix: error logs doesnt have crash stacks and reduce graceful exit timeout for availability
1 parent ff64692 commit 5ff150a

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/server.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {fileURLToPath} from 'url';
1919
const __filename = fileURLToPath(import.meta.url);
2020
const __dirname = path.dirname(__filename);
2121

22+
const CLEANUP_GRACE_TIME_5SEC = 5000;
23+
2224
const server = fastify({
2325
logger: true,
2426
trustProxy: true,
@@ -304,7 +306,7 @@ export async function startServer() {
304306
});
305307
server.log.info(`Server started on port ${configs.port}`);
306308
} catch (err) {
307-
server.log.error('Error starting server:', err);
309+
server.log.error(err, 'Error starting server:');
308310
process.exit(1);
309311
}
310312
}
@@ -316,15 +318,15 @@ export async function close() {
316318
server.log.info('Shutting down server...');
317319
try {
318320
const shutdownTimeout = setTimeout(() => {
319-
server.log.warn('Forced shutdown after timeout');
321+
server.log.error('Forced shutdown after timeout');
320322
process.exit(1);
321-
}, 30000);
323+
}, CLEANUP_GRACE_TIME_5SEC);
322324

323325
await server.close();
324326
clearTimeout(shutdownTimeout);
325327
server.log.info('Server shut down successfully');
326328
} catch (err) {
327-
server.log.error('Error during shutdown:', err);
329+
server.log.error(err, 'Error during shutdown:');
328330
process.exit(1);
329331
}
330332
}
@@ -344,12 +346,13 @@ process.on('SIGINT', async () => {
344346

345347
// Handle uncaught exceptions
346348
process.on('uncaughtException', (err) => {
347-
server.log.error('Uncaught Exception:', err);
349+
server.log.error(err, 'Uncaught Exception:');
348350
close().then(() => process.exit(1));
349351
});
350352

351353
// Handle unhandled promise rejections
352354
process.on('unhandledRejection', (reason, promise) => {
353-
server.log.error('Unhandled Rejection at:', promise, 'reason:', reason);
355+
// non error rejections do not have a stack trace and hence cannot be located.
356+
server.log.error(reason instanceof Error ? reason : { reason }, 'Unhandled Rejection at promise', promise);
354357
close().then(() => process.exit(1));
355358
});

0 commit comments

Comments
 (0)