79 lines
1.9 KiB
TypeScript
79 lines
1.9 KiB
TypeScript
/**
|
|
* @stock-bot/shutdown - Shutdown management library
|
|
*
|
|
* Main exports for the shutdown library
|
|
*/
|
|
|
|
// Core shutdown classes and types
|
|
export { Shutdown } from './shutdown.js';
|
|
export type { ShutdownCallback, ShutdownOptions, ShutdownResult } from './types.js';
|
|
|
|
import { Shutdown } from './shutdown.js';
|
|
import type { ShutdownResult } from './types.js';
|
|
|
|
// Global singleton instance
|
|
let globalInstance: Shutdown | null = null;
|
|
|
|
/**
|
|
* Get the global shutdown instance (creates one if it doesn't exist)
|
|
*/
|
|
function getGlobalInstance(): Shutdown {
|
|
if (!globalInstance) {
|
|
globalInstance = Shutdown.getInstance();
|
|
}
|
|
return globalInstance;
|
|
}
|
|
|
|
/**
|
|
* Convenience functions for global shutdown management
|
|
*/
|
|
|
|
/**
|
|
* Register a cleanup callback that will be executed during shutdown
|
|
*/
|
|
export function onShutdown(callback: () => Promise<void> | void): void {
|
|
getGlobalInstance().onShutdown(callback);
|
|
}
|
|
|
|
/**
|
|
* Set the shutdown timeout in milliseconds
|
|
*/
|
|
export function setShutdownTimeout(timeout: number): void {
|
|
getGlobalInstance().setTimeout(timeout);
|
|
}
|
|
|
|
/**
|
|
* Check if shutdown is currently in progress
|
|
*/
|
|
export function isShuttingDown(): boolean {
|
|
return globalInstance?.isShutdownInProgress() || false;
|
|
}
|
|
|
|
/**
|
|
* Get the number of registered shutdown callbacks
|
|
*/
|
|
export function getShutdownCallbackCount(): number {
|
|
return globalInstance?.getCallbackCount() || 0;
|
|
}
|
|
|
|
/**
|
|
* Manually initiate graceful shutdown
|
|
*/
|
|
export function initiateShutdown(signal?: string): Promise<ShutdownResult> {
|
|
return getGlobalInstance().shutdown(signal);
|
|
}
|
|
|
|
/**
|
|
* Manually initiate graceful shutdown and exit the process
|
|
*/
|
|
export function shutdownAndExit(signal?: string, exitCode = 0): Promise<never> {
|
|
return getGlobalInstance().shutdownAndExit(signal, exitCode);
|
|
}
|
|
|
|
/**
|
|
* Reset the global instance (mainly for testing)
|
|
*/
|
|
export function resetShutdown(): void {
|
|
globalInstance = null;
|
|
Shutdown.reset();
|
|
}
|