/** * Safe Restart Service * * Manages graceful shutdown and restart of Crewly, * preserving state and enabling seamless recovery. * * @module services/orchestrator/safe-restart */ import { ResumeInstructions } from '../../types/orchestrator-state.types.js'; /** * Restart configuration */ export interface RestartConfig { /** Reason for restart */ reason: string; /** Delay before restart in ms */ delayMs?: number; /** Skip state save (emergency restart) */ skipStateSave?: boolean; /** Command to run after restart */ postRestartCommand?: string; /** Notify via Slack */ notifySlack?: boolean; } /** * Restart status */ export interface RestartStatus { /** Whether a restart is in progress */ isRestarting: boolean; /** When restart was scheduled */ restartScheduledAt?: string; /** Why restart was scheduled */ restartReason?: string; /** Countdown until restart */ countdown?: number; } /** * SafeRestartService class * * Handles graceful shutdown, state preservation, and process restart. * Integrates with state persistence and Slack for notifications. * * @example * ```typescript * const service = getSafeRestartService(); * * // Handle startup and resume * const instructions = await service.handleStartup(); * if (instructions) { * // Resume tasks and conversations * } * * // Schedule restart for self-improvement * service.scheduleRestart({ * reason: 'Applying code changes', * delayMs: 5000, * notifySlack: true, * }); * ``` */ export declare class SafeRestartService { private isRestartingFlag; private restartTimer; private scheduledRestartReason; private scheduledRestartTime; private shutdownCallbacks; private startupCallbacks; private signalHandlersRegistered; private logger; /** * Create a new SafeRestartService * * Note: Does not automatically register signal handlers. * Call registerProcessHandlers() explicitly to enable signal handling. */ constructor(); /** * Register process signal handlers * * Sets up handlers for SIGTERM, SIGINT, and error events. * Should be called during application initialization. */ registerProcessHandlers(): void; /** * Register a callback to run before shutdown * * @param callback - Async function to run during shutdown */ onShutdown(callback: () => Promise): void; /** * Register a callback to run after startup * * @param callback - Async function to run with resume instructions */ onStartup(callback: (instructions: ResumeInstructions) => Promise): void; /** * Handle application startup * * Initializes state persistence, loads previous state, and runs * startup callbacks with resume instructions. * * @returns Resume instructions if restarting, null for fresh start */ handleStartup(): Promise; /** * Initiate graceful shutdown * * Runs all shutdown callbacks and saves state before shutdown. * * @param reason - Reason for shutdown */ gracefulShutdown(reason: string): Promise; /** * Emergency shutdown (minimal cleanup) * * Attempts to save state with minimal processing for error scenarios. * * @param reason - Reason for emergency shutdown */ emergencyShutdown(reason: string): Promise; /** * Schedule a restart * * Schedules a restart after the specified delay. * * @param config - Restart configuration */ scheduleRestart(config: RestartConfig): void; /** * Cancel scheduled restart * * @returns True if a restart was cancelled, false if none scheduled */ cancelScheduledRestart(): boolean; /** * Execute restart * * @param config - Restart configuration */ private executeRestart; /** * Notify about scheduled restart * * @param reason - Restart reason * @param delayMs - Delay in milliseconds */ private notifyRestartScheduled; /** * Get restart status * * @returns Current restart status */ getStatus(): RestartStatus; /** * Check if this is a restart (vs fresh start) * * @returns True if this process was started after a restart */ isRestart(): boolean; /** * Get restart reason (if restarted) * * @returns Restart reason or undefined for fresh start */ getRestartReason(): string | undefined; /** * Check for and execute post-restart command * * Reads the restart marker file and executes any post-restart command. */ checkPostRestartCommand(): Promise; /** * Clear shutdown callbacks (for testing) */ clearShutdownCallbacks(): void; /** * Clear startup callbacks (for testing) */ clearStartupCallbacks(): void; } /** * Get singleton instance * * @returns SafeRestartService instance */ export declare function getSafeRestartService(): SafeRestartService; /** * Reset instance (for testing) */ export declare function resetSafeRestartService(): void; //# sourceMappingURL=safe-restart.service.d.ts.map