lifecycleion 0.0.12 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -471,6 +471,7 @@ interface LifecycleCommon extends EventEmitterSurface {
471
471
  attachSignals(): void;
472
472
  detachSignals(): void;
473
473
  getSignalStatus(): LifecycleSignalStatus;
474
+ getShutdownEscalationStatus(): ShutdownEscalationStatus;
474
475
  triggerReload(): Promise<SignalBroadcastResult>;
475
476
  triggerInfo(): Promise<SignalBroadcastResult>;
476
477
  triggerDebug(): Promise<SignalBroadcastResult>;
@@ -681,6 +682,163 @@ interface LifecycleSignalStatus extends ProcessSignalManagerStatus {
681
682
  /** How shutdown was triggered (null if not shut down) */
682
683
  shutdownMethod: ShutdownMethod | null;
683
684
  }
685
+ interface ShutdownEscalationStatusDisabled {
686
+ /** Whether repeated shutdown escalation is configured */
687
+ configured: false;
688
+ /** Whether a shutdown is currently in progress */
689
+ isShuttingDown: boolean;
690
+ /** Escalation cannot be armed when the policy is disabled */
691
+ isArmed: false;
692
+ /** Config fields are unavailable while the policy is disabled */
693
+ forceAfterCount: null;
694
+ withinMS: null;
695
+ armedAfterFailureMS: null;
696
+ armedAfterFailureMSSource: null;
697
+ /** Runtime counters remain empty while the policy is disabled */
698
+ requestCount: 0;
699
+ firstMethod: null;
700
+ latestMethod: null;
701
+ firstRequestAt: null;
702
+ latestRequestAt: null;
703
+ repeatedWindowStartedAt: null;
704
+ armedUntil: null;
705
+ hasTriggeredForceShutdown: false;
706
+ }
707
+ interface ShutdownEscalationStatusEnabled {
708
+ /** Whether repeated shutdown escalation is configured */
709
+ configured: true;
710
+ /** Whether a shutdown is currently in progress */
711
+ isShuttingDown: boolean;
712
+ /** Whether escalation remains armed after an unsuccessful shutdown */
713
+ isArmed: boolean;
714
+ /** Configured force threshold when policy is enabled */
715
+ forceAfterCount: number;
716
+ /** Configured escalation window when policy is enabled */
717
+ withinMS: number;
718
+ /** Effective post-failure armed duration in ms when policy is enabled */
719
+ armedAfterFailureMS: number;
720
+ /** Whether the effective armed duration was explicitly configured or derived from defaults */
721
+ armedAfterFailureMSSource: 'explicit' | 'derived';
722
+ /** Whether manual stopAllComponents() retries continue the armed escalation state */
723
+ countManualRetriesTowardEscalation: boolean;
724
+ /**
725
+ * Number of follow-up shutdown requests counted in the current escalation
726
+ * window (resets when a new window starts). This is a window-local count,
727
+ * not a cumulative total of all requests since shutdown began.
728
+ */
729
+ requestCount: number;
730
+ /** Method that started the current/most recent shutdown escalation cycle */
731
+ firstMethod: ShutdownMethod | null;
732
+ /** Most recent shutdown request method seen by escalation tracking */
733
+ latestMethod: ShutdownMethod | null;
734
+ /** Timestamp of the first shutdown request that seeded this escalation state */
735
+ firstRequestAt: number | null;
736
+ /** Timestamp of the most recent shutdown request counted by escalation tracking */
737
+ latestRequestAt: number | null;
738
+ /** Timestamp when the current post-start escalation window began */
739
+ repeatedWindowStartedAt: number | null;
740
+ /** Timestamp when armed-after-failure escalation will expire */
741
+ armedUntil: number | null;
742
+ /** Whether onForceShutdown() has already been dispatched for this shutdown cycle */
743
+ hasTriggeredForceShutdown: boolean;
744
+ }
745
+ /**
746
+ * Read-only snapshot of repeated shutdown escalation configuration and runtime state.
747
+ */
748
+ type ShutdownEscalationStatus = ShutdownEscalationStatusDisabled | ShutdownEscalationStatusEnabled;
749
+ /**
750
+ * Context passed to repeated shutdown force handlers.
751
+ *
752
+ * The first request starts graceful shutdown. If additional shutdown requests
753
+ * arrive within the configured time window and the threshold is reached, the
754
+ * LifecycleManager invokes `onForceShutdown()` with this snapshot.
755
+ */
756
+ interface ForceShutdownContext {
757
+ /** Number of escalation requests counted after shutdown had already started */
758
+ requestCount: number;
759
+ /** Method that started the current shutdown cycle */
760
+ firstMethod: ShutdownMethod;
761
+ /** Most recent shutdown request method */
762
+ latestMethod: ShutdownMethod;
763
+ /** Unix timestamp in ms for the request that started the current shutdown cycle */
764
+ firstRequestAt: number;
765
+ /** Unix timestamp in ms for the most recent request in the current window */
766
+ latestRequestAt: number;
767
+ /**
768
+ * Whether shutdown is still actively running when the force callback fires.
769
+ *
770
+ * This is `true` when escalation fires during an active shutdown (the normal
771
+ * path: operator pressed the shutdown signal multiple times while
772
+ * `stopAllComponents()` was still running).
773
+ *
774
+ * This is `false` when escalation fires from the post-failure armed window
775
+ * (i.e. a previous shutdown attempt already returned unsuccessfully and the
776
+ * manager kept escalation armed briefly). In that path the force callback
777
+ * fires before the follow-up `stopAllComponents()` call is issued, so no
778
+ * shutdown is actively in progress at the moment the callback runs.
779
+ *
780
+ * Check `wasArmedAfterFailure` to distinguish the two cases.
781
+ */
782
+ isShuttingDown: boolean;
783
+ /**
784
+ * Whether this force-shutdown was triggered from the post-failure armed
785
+ * state rather than during an active shutdown.
786
+ *
787
+ * `true` — a previous shutdown attempt returned unsuccessfully, the manager
788
+ * kept escalation armed, and the force threshold was crossed while
789
+ * that armed window was still open (no shutdown running yet).
790
+ *
791
+ * `false` — the force threshold was crossed while `stopAllComponents()` was
792
+ * actively in progress (the common path).
793
+ *
794
+ * Use this to decide whether your force handler needs to start or re-trigger
795
+ * shutdown, or whether it can assume a shutdown run is already underway.
796
+ */
797
+ wasArmedAfterFailure: boolean;
798
+ }
799
+ /**
800
+ * Optional policy for escalating repeated shutdown requests during an already
801
+ * running shutdown.
802
+ */
803
+ interface RepeatedShutdownRequestPolicy {
804
+ /**
805
+ * Number of shutdown requests required before force escalation runs.
806
+ * Only escalation requests received after shutdown has already started count
807
+ * toward this threshold. The initial request that starts graceful shutdown
808
+ * does not count.
809
+ * @default 3
810
+ */
811
+ forceAfterCount?: number;
812
+ /**
813
+ * Time window in ms for counting follow-up escalation requests received
814
+ * after shutdown has already started.
815
+ * Requests outside the window start a new escalation window.
816
+ * @default 2000
817
+ */
818
+ withinMS?: number;
819
+ /**
820
+ * How long escalation should remain armed after an unsuccessful shutdown
821
+ * returns. When omitted, the manager derives it as `withinMS * forceAfterCount`.
822
+ * Set to `0` to disable post-failure arming entirely — the escalation window
823
+ * will not persist once a shutdown attempt returns, and each new request will
824
+ * start a fresh escalation cycle. (Note: `withinMS = 0` is a separate option
825
+ * that controls the width of the active-shutdown escalation window, not this.)
826
+ */
827
+ armedAfterFailureMS?: number;
828
+ /**
829
+ * If true, manual `stopAllComponents()` retries that happen while the
830
+ * post-failure armed window is still open continue the same escalation state.
831
+ * When false, manual retries always start a fresh escalation cycle.
832
+ * @default false
833
+ */
834
+ countManualRetriesTowardEscalation?: boolean;
835
+ /**
836
+ * Invoked once per shutdown cycle when repeated requests reach the threshold.
837
+ * Typical uses: final logging, telemetry flush, `logger.exit()`, or
838
+ * `process.exit()` by the application.
839
+ */
840
+ onForceShutdown: (context: ForceShutdownContext) => void | Promise<void>;
841
+ }
684
842
  /**
685
843
  * Configuration options for LifecycleManager
686
844
  */
@@ -711,6 +869,11 @@ interface LifecycleManagerOptions {
711
869
  onInfoRequested?: (broadcastInfo: () => Promise<SignalBroadcastResult>) => void | Promise<void>;
712
870
  /** Custom debug signal handler (called instead of default broadcast, receives broadcast function you can optionally call) */
713
871
  onDebugRequested?: (broadcastDebug: () => Promise<SignalBroadcastResult>) => void | Promise<void>;
872
+ /**
873
+ * Optional policy for escalating repeated shutdown requests received while a
874
+ * graceful shutdown is already in progress.
875
+ */
876
+ repeatedShutdownRequestPolicy?: RepeatedShutdownRequestPolicy;
714
877
  }
715
878
 
716
879
  /**
@@ -1008,6 +1171,7 @@ declare class LifecycleManager extends EventEmitterProtected implements Lifecycl
1008
1171
  private readonly attachSignalsBeforeStartup;
1009
1172
  private readonly attachSignalsOnStart;
1010
1173
  private readonly detachSignalsOnStop;
1174
+ private readonly repeatedShutdownRequestPolicy?;
1011
1175
  private components;
1012
1176
  private runningComponents;
1013
1177
  private componentStates;
@@ -1022,6 +1186,8 @@ declare class LifecycleManager extends EventEmitterProtected implements Lifecycl
1022
1186
  private pendingLoggerExitResolve;
1023
1187
  private shutdownMethod;
1024
1188
  private lastShutdownResult;
1189
+ private repeatedShutdownExpiryTimer;
1190
+ private repeatedShutdownRequestState;
1025
1191
  private processSignalManager;
1026
1192
  private readonly onReloadRequested?;
1027
1193
  private readonly onInfoRequested?;
@@ -1214,6 +1380,10 @@ declare class LifecycleManager extends EventEmitterProtected implements Lifecycl
1214
1380
  * Get status information about signal handling.
1215
1381
  */
1216
1382
  getSignalStatus(): LifecycleSignalStatus;
1383
+ /**
1384
+ * Get status information about repeated shutdown escalation configuration and runtime state.
1385
+ */
1386
+ getShutdownEscalationStatus(): ShutdownEscalationStatus;
1217
1387
  /**
1218
1388
  * Enable Logger exit hook integration
1219
1389
  *
@@ -1470,9 +1640,78 @@ declare class LifecycleManager extends EventEmitterProtected implements Lifecycl
1470
1640
  private findAllCircularCycles;
1471
1641
  /**
1472
1642
  * Handle shutdown signal - initiates stopAllComponents().
1473
- * Double signal protection: if already shutting down, log warning and ignore.
1643
+ *
1644
+ * Four cases depending on the current shutdown state:
1645
+ *
1646
+ * 1. **Active shutdown** (`isShuttingDown = true`): escalate through the
1647
+ * repeated-shutdown policy if configured, otherwise log and discard.
1648
+ * Emits `signal:shutdown` with `isAlreadyShuttingDown: true` and returns
1649
+ * without starting another shutdown.
1650
+ *
1651
+ * 2. **Armed post-failure** (previous shutdown finished, armed window still
1652
+ * open): count the request toward the escalation window, emit
1653
+ * `signal:shutdown` with `isAlreadyShuttingDown: false`, then start a
1654
+ * new `stopAllComponents()` run to retry.
1655
+ *
1656
+ * 3. **Armed post-failure expired** (armed window opened but has since
1657
+ * elapsed): expire the stale state, treat the request as a fresh
1658
+ * shutdown (falls through to case 4).
1659
+ *
1660
+ * 4. **Fresh shutdown** (no prior shutdown state): seed escalation tracking
1661
+ * if policy is configured, emit `signal:shutdown` with
1662
+ * `isAlreadyShuttingDown: false`, and start `stopAllComponents()`.
1663
+ *
1664
+ * In all cases `signal:shutdown` is emitted exactly once.
1474
1665
  */
1475
1666
  private handleShutdownRequest;
1667
+ /**
1668
+ * Tracks repeated shutdown requests during an active shutdown and optionally
1669
+ * invokes the configured force shutdown callback when the threshold is reached.
1670
+ *
1671
+ * @returns true when the request was consumed as part of the repeated-shutdown
1672
+ * escalation flow, false when the caller should treat it as a fresh shutdown request
1673
+ */
1674
+ private handleRepeatedShutdownRequest;
1675
+ /**
1676
+ * Clears repeated shutdown request tracking so a new shutdown cycle starts fresh.
1677
+ */
1678
+ private resetRepeatedShutdownRequestState;
1679
+ /**
1680
+ * Clear any pending expiration timer for the post-failure escalation window.
1681
+ */
1682
+ private clearRepeatedShutdownExpiryTimer;
1683
+ /**
1684
+ * Returns whether post-failure escalation remains armed after first
1685
+ * normalizing any stale timer-backed state.
1686
+ *
1687
+ * The method can expire old armed windows as a side effect because the timer
1688
+ * callback may not have run yet on a delayed event loop. Callers use this
1689
+ * when they need the effective runtime truth, not just the last timer write.
1690
+ */
1691
+ private normalizeRepeatedShutdownRequestStateArmedStatus;
1692
+ /**
1693
+ * Transition armed post-failure escalation state into its expired/reset state.
1694
+ */
1695
+ private expireRepeatedShutdownRequestState;
1696
+ /**
1697
+ * Arms or refreshes the post-failure escalation window and its expiration timer.
1698
+ */
1699
+ private refreshRepeatedShutdownArmedWindow;
1700
+ /**
1701
+ * Seeds shutdown escalation tracking for a new shutdown cycle.
1702
+ *
1703
+ * The first shutdown trigger starts graceful shutdown and arms escalation with
1704
+ * an effective post-start count of 0. Later shutdown requests can then count
1705
+ * toward the configured force threshold regardless of whether the shutdown
1706
+ * started from a signal, keyboard shortcut, or direct API call.
1707
+ */
1708
+ private seedRepeatedShutdownRequestState;
1709
+ /**
1710
+ * Preserves a short-lived post-failure escalation window after shutdown
1711
+ * returns unsuccessfully so operators can keep pressing shutdown without
1712
+ * losing the existing force count the moment the graceful attempt finishes.
1713
+ */
1714
+ private armRepeatedShutdownAfterFailure;
1476
1715
  /**
1477
1716
  * Handle reload request - calls custom callback or broadcasts to components.
1478
1717
  *
@@ -1637,6 +1876,28 @@ interface LifecycleManagerEventMap {
1637
1876
  method: ShutdownMethod;
1638
1877
  duringStartup: boolean;
1639
1878
  };
1879
+ /** Failed/timed-out shutdown left escalation armed briefly for follow-up force presses. */
1880
+ 'lifecycle-manager:shutdown-escalation-armed': {
1881
+ firstMethod: ShutdownMethod;
1882
+ requestCount: number;
1883
+ armedUntil: number;
1884
+ };
1885
+ /** Armed escalation window expired and the manager cleared that old escalation state. */
1886
+ 'lifecycle-manager:shutdown-escalation-expired': {
1887
+ firstMethod: ShutdownMethod;
1888
+ latestMethod: ShutdownMethod | null;
1889
+ requestCount: number;
1890
+ armedUntil: number;
1891
+ };
1892
+ /** Repeated shutdown requests crossed the force threshold and onForceShutdown() was invoked. */
1893
+ 'lifecycle-manager:shutdown-escalation-forced': {
1894
+ firstMethod: ShutdownMethod;
1895
+ latestMethod: ShutdownMethod;
1896
+ requestCount: number;
1897
+ firstRequestAt: number;
1898
+ latestRequestAt: number;
1899
+ wasArmedAfterFailure: boolean;
1900
+ };
1640
1901
  'component:starting': {
1641
1902
  name: string;
1642
1903
  };
@@ -1713,6 +1974,7 @@ interface LifecycleManagerEventMap {
1713
1974
  };
1714
1975
  'signal:shutdown': {
1715
1976
  method: ShutdownSignal;
1977
+ isAlreadyShuttingDown: boolean;
1716
1978
  };
1717
1979
  'signal:reload': undefined;
1718
1980
  'signal:info': undefined;
@@ -1827,6 +2089,25 @@ declare class LifecycleManagerEvents {
1827
2089
  method: ShutdownMethod;
1828
2090
  duringStartup: boolean;
1829
2091
  }): void;
2092
+ lifecycleManagerShutdownEscalationArmed(input: {
2093
+ firstMethod: ShutdownMethod;
2094
+ requestCount: number;
2095
+ armedUntil: number;
2096
+ }): void;
2097
+ lifecycleManagerShutdownEscalationExpired(input: {
2098
+ firstMethod: ShutdownMethod;
2099
+ latestMethod: ShutdownMethod | null;
2100
+ requestCount: number;
2101
+ armedUntil: number;
2102
+ }): void;
2103
+ lifecycleManagerShutdownEscalationForced(input: {
2104
+ firstMethod: ShutdownMethod;
2105
+ latestMethod: ShutdownMethod;
2106
+ requestCount: number;
2107
+ firstRequestAt: number;
2108
+ latestRequestAt: number;
2109
+ wasArmedAfterFailure: boolean;
2110
+ }): void;
1830
2111
  componentStarting(name: string): void;
1831
2112
  componentStarted(name: string, status?: ComponentStatus): void;
1832
2113
  componentStartTimeout(name: string, error: Error, info?: {
@@ -1862,7 +2143,7 @@ declare class LifecycleManagerEvents {
1862
2143
  componentShutdownForceCompleted(name: string): void;
1863
2144
  componentShutdownForceTimeout(name: string, timeoutMS: number): void;
1864
2145
  componentStartupRollback(name: string): void;
1865
- signalShutdown(method: ShutdownSignal): void;
2146
+ signalShutdown(method: ShutdownSignal, isAlreadyShuttingDown?: boolean): void;
1866
2147
  signalReload(): void;
1867
2148
  signalInfo(): void;
1868
2149
  signalDebug(): void;
@@ -2048,4 +2329,4 @@ declare const lifecycleManagerErrCodes: {
2048
2329
  readonly StopTimeout: "StopTimeout";
2049
2330
  };
2050
2331
 
2051
- export { BaseComponent, type BaseOperationResult, type BroadcastOptions, type BroadcastResult, type ComponentHealthResult, ComponentNotFoundError, type ComponentOperationFailureCode, type ComponentOperationResult, type ComponentOptions, ComponentRegistrationError, type ComponentSignalResult, type ComponentStallInfo, ComponentStartTimeoutError, ComponentStartupError, type ComponentState, type ComponentStatus, ComponentStopTimeoutError, type ComponentValueResult, DependencyCycleError, type DependencyValidationResult, type GetValueOptions, type HealthCheckResult, type HealthReport, type InsertComponentAtResult, type InsertPosition, InvalidComponentNameError, LifecycleManager, type LifecycleManagerEmit, type LifecycleManagerEventMap, type LifecycleManagerEventName, LifecycleManagerEvents, type LifecycleManagerOptions, type LifecycleManagerStatus, type MessageResult, MissingDependencyError, type RegisterComponentResult, type RegisterOptions, type RegistrationFailureCode, type RestartComponentOptions, type RestartResult, type SendMessageOptions, type ShutdownMethod, type ShutdownResult, type SignalBroadcastResult, type StartComponentOptions, type StartupOptions, type StartupOrderFailureCode, type StartupOrderResult, type StartupResult, StartupTimeoutError, type StopAllOptions, type StopComponentOptions, type SystemState, type UnregisterComponentResult, type UnregisterFailureCode, type UnregisterOptions, type ValueResult, lifecycleManagerErrCodes, lifecycleManagerErrPrefix, lifecycleManagerErrTypes };
2332
+ export { BaseComponent, type BaseOperationResult, type BroadcastOptions, type BroadcastResult, type ComponentHealthResult, ComponentNotFoundError, type ComponentOperationFailureCode, type ComponentOperationResult, type ComponentOptions, ComponentRegistrationError, type ComponentSignalResult, type ComponentStallInfo, ComponentStartTimeoutError, ComponentStartupError, type ComponentState, type ComponentStatus, ComponentStopTimeoutError, type ComponentValueResult, DependencyCycleError, type DependencyValidationResult, type ForceShutdownContext, type GetValueOptions, type HealthCheckResult, type HealthReport, type InsertComponentAtResult, type InsertPosition, InvalidComponentNameError, LifecycleManager, type LifecycleManagerEmit, type LifecycleManagerEventMap, type LifecycleManagerEventName, LifecycleManagerEvents, type LifecycleManagerOptions, type LifecycleManagerStatus, type MessageResult, MissingDependencyError, type RegisterComponentResult, type RegisterOptions, type RegistrationFailureCode, type RepeatedShutdownRequestPolicy, type RestartComponentOptions, type RestartResult, type SendMessageOptions, type ShutdownEscalationStatus, type ShutdownMethod, type ShutdownResult, type SignalBroadcastResult, type StartComponentOptions, type StartupOptions, type StartupOrderFailureCode, type StartupOrderResult, type StartupResult, StartupTimeoutError, type StopAllOptions, type StopComponentOptions, type SystemState, type UnregisterComponentResult, type UnregisterFailureCode, type UnregisterOptions, type ValueResult, lifecycleManagerErrCodes, lifecycleManagerErrPrefix, lifecycleManagerErrTypes };