commit 2 refactor

This commit is contained in:
Boki 2026-10-20 14:35:45 -04:00
parent 588f666943
commit 32e43c9243
310 changed files with 33318 additions and 31189 deletions

View file

@ -40,7 +40,7 @@ export class Shutdown {
return {
isShuttingDown: this.isShuttingDown,
signal: this.triggerSignal,
callbackCount: this.callbacks.length
callbackCount: this.callbacks.length,
};
}
@ -64,9 +64,9 @@ export class Shutdown {
async shutdown(signal?: string): Promise<void> {
// If already shutting down, return the existing promise
if (this.shutdownPromise) {
this.logger.warn('Shutdown already in progress, ignoring additional request', {
this.logger.warn('Shutdown already in progress, ignoring additional request', {
originalSignal: this.triggerSignal,
newSignal: signal
newSignal: signal,
});
return this.shutdownPromise;
}
@ -77,16 +77,16 @@ export class Shutdown {
this.isShuttingDown = true;
this.triggerSignal = signal || 'manual';
this.logger.info('Starting graceful shutdown', {
this.logger.info('Starting graceful shutdown', {
signal: this.triggerSignal,
timeout: this.shutdownTimeout,
callbackCount: this.callbacks.length
callbackCount: this.callbacks.length,
});
// Create and store the shutdown promise
this.shutdownPromise = this.performShutdown();
try {
await this.shutdownPromise;
this.logger.info('Graceful shutdown completed successfully');
@ -97,23 +97,23 @@ export class Shutdown {
}
private async performShutdown(): Promise<void> {
const timeout = new Promise<never>((_, reject) =>
setTimeout(() => reject(new Error('Shutdown timeout')), this.shutdownTimeout)
);
let timer: ReturnType<typeof setTimeout> | undefined;
const timeout = new Promise<never>((_, reject) => {
timer = setTimeout(() => reject(new Error('Shutdown timeout')), this.shutdownTimeout);
});
try {
await Promise.race([this.executeCallbacks(), timeout]);
} catch (error) {
throw error;
} finally {
clearTimeout(timer);
}
}
private async executeCallbacks(): Promise<void> {
const sorted = [...this.callbacks].sort((a, b) => a.priority - b.priority);
this.logger.debug('Executing shutdown callbacks', {
this.logger.debug('Executing shutdown callbacks', {
count: sorted.length,
callbacks: sorted.map(c => ({ name: c.name || 'unnamed', priority: c.priority }))
callbacks: sorted.map(c => ({ name: c.name || 'unnamed', priority: c.priority })),
});
let completed = 0;
@ -124,9 +124,9 @@ export class Shutdown {
try {
this.logger.debug(`Starting shutdown callback: ${callbackName}`, { priority });
const startTime = Date.now();
await callback();
const duration = Date.now() - startTime;
completed++;
this.logger.debug(`Completed shutdown callback: ${callbackName}`, { duration, priority });
@ -136,10 +136,10 @@ export class Shutdown {
}
}
this.logger.info('Shutdown callbacks execution completed', {
this.logger.info('Shutdown callbacks execution completed', {
total: sorted.length,
completed,
failed
failed,
});
}
@ -155,7 +155,7 @@ export class Shutdown {
// Use process.on instead of process.once to handle multiple signals
process.on(signal, async () => {
this.logger.info(`Received ${signal} signal`);
// If already shutting down, ignore the signal
if (this.isShuttingDown || this.shutdownPromise) {
this.logger.info(`Already shutting down, ignoring ${signal}`);
@ -164,22 +164,22 @@ export class Shutdown {
try {
await this.shutdown(signal);
// Add small delay to ensure logs are flushed
await new Promise(resolve => setTimeout(resolve, 100));
this.logger.info('Exiting process with code 0');
process.exit(0);
} catch (error) {
this.logger.error('Shutdown error, exiting with code 1', { error });
// Add small delay to ensure error logs are flushed
await new Promise(resolve => setTimeout(resolve, 100));
process.exit(1);
}
});
this.logger.debug(`Registered handler for ${signal}`);
});