fixed priority shutdown

This commit is contained in:
Boki 2025-06-21 09:08:40 -04:00
parent 6d5d746f68
commit 5929612e36
6 changed files with 113 additions and 46 deletions

View file

@ -58,7 +58,7 @@ function createDestination(
// Console: In-process pretty stream for dev (fast shutdown)
if (config.logConsole && config.environment !== 'production') {
const prettyStream = pretty({
sync: false, // IMPORTANT: Make async to prevent blocking the event loop
sync: true, // IMPORTANT: Make async to prevent blocking the event loop
colorize: true,
translateTime: 'yyyy-mm-dd HH:MM:ss.l',
messageFormat: '[{service}{childName}] {msg}',

View file

@ -9,7 +9,7 @@ import type { ShutdownResult } from './types';
// Core shutdown classes and types
export { Shutdown } from './shutdown';
export type { ShutdownCallback, ShutdownOptions, ShutdownResult } from './types';
export type { ShutdownCallback, ShutdownOptions, ShutdownResult, PrioritizedShutdownCallback } from './types';
// Global singleton instance
let globalInstance: Shutdown | null = null;
@ -31,8 +31,29 @@ function getGlobalInstance(): Shutdown {
/**
* Register a cleanup callback that will be executed during shutdown
*/
export function onShutdown(callback: () => Promise<void> | void): void {
getGlobalInstance().onShutdown(callback);
export function onShutdown(callback: () => Promise<void> | void, priority?: number, name?: string): void {
getGlobalInstance().onShutdown(callback, priority, name);
}
/**
* Register a high priority shutdown callback (for queues, critical services)
*/
export function onShutdownHigh(callback: () => Promise<void> | void, name?: string): void {
getGlobalInstance().onShutdownHigh(callback, name);
}
/**
* Register a medium priority shutdown callback (for databases, connections)
*/
export function onShutdownMedium(callback: () => Promise<void> | void, name?: string): void {
getGlobalInstance().onShutdownMedium(callback, name);
}
/**
* Register a low priority shutdown callback (for loggers, cleanup)
*/
export function onShutdownLow(callback: () => Promise<void> | void, name?: string): void {
getGlobalInstance().onShutdownLow(callback, name);
}
/**

View file

@ -8,13 +8,13 @@
* - Platform-specific signal support (Windows/Unix)
*/
import type { ShutdownCallback, ShutdownOptions, ShutdownResult } from './types';
import type { ShutdownCallback, ShutdownOptions, ShutdownResult, PrioritizedShutdownCallback } from './types';
export class Shutdown {
private static instance: Shutdown | null = null;
private isShuttingDown = false;
private shutdownTimeout = 30000; // 30 seconds default
private callbacks: ShutdownCallback[] = [];
private callbacks: PrioritizedShutdownCallback[] = [];
private signalHandlersRegistered = false;
constructor(options: ShutdownOptions = {}) {
@ -43,13 +43,34 @@ export class Shutdown {
}
/**
* Register a cleanup callback
* Register a cleanup callback with priority (lower numbers = higher priority)
*/
onShutdown(callback: ShutdownCallback): void {
onShutdown(callback: ShutdownCallback, priority: number = 50, name?: string): void {
if (this.isShuttingDown) {
return;
}
this.callbacks.push(callback);
this.callbacks.push({ callback, priority, name });
}
/**
* Register a high priority shutdown callback (for queues, critical services)
*/
onShutdownHigh(callback: ShutdownCallback, name?: string): void {
this.onShutdown(callback, 10, name);
}
/**
* Register a medium priority shutdown callback (for databases, connections)
*/
onShutdownMedium(callback: ShutdownCallback, name?: string): void {
this.onShutdown(callback, 50, name);
}
/**
* Register a low priority shutdown callback (for loggers, cleanup)
*/
onShutdownLow(callback: ShutdownCallback, name?: string): void {
this.onShutdown(callback, 90, name);
}
/**
@ -140,21 +161,34 @@ export class Shutdown {
}
/**
* Execute all registered callbacks
* Execute all registered callbacks in priority order
*/
private async executeCallbacks(): Promise<{ executed: number; failed: number }> {
if (this.callbacks.length === 0) {
return { executed: 0, failed: 0 };
}
const results = await Promise.allSettled(
this.callbacks.map(async callback => {
await callback();
})
);
// Sort callbacks by priority (lower numbers = higher priority = execute first)
const sortedCallbacks = [...this.callbacks].sort((a, b) => a.priority - b.priority);
const failed = results.filter(result => result.status === 'rejected').length;
const executed = results.length;
let executed = 0;
let failed = 0;
// Execute callbacks in order by priority
for (const { callback, name, priority } of sortedCallbacks) {
try {
await callback();
executed++;
if (name) {
console.log(`✓ Shutdown completed: ${name} (priority: ${priority})`);
}
} catch (error) {
failed++;
if (name) {
console.error(`✗ Shutdown failed: ${name} (priority: ${priority})`, error);
}
}
}
return { executed, failed };
}

View file

@ -7,6 +7,15 @@
*/
export type ShutdownCallback = () => Promise<void> | void;
/**
* Shutdown callback with priority information
*/
export interface PrioritizedShutdownCallback {
callback: ShutdownCallback;
priority: number;
name?: string;
}
/**
* Options for configuring shutdown behavior
*/