MCPcopy Create free account
hub / github.com/CreminiAI/skillpack / Lifecycle

Class Lifecycle

src/runtime/lifecycle.ts:20–109  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18
19
20export class Lifecycle implements LifecycleHandler, LifecycleInfo {
21 private readonly server: Server;
22 private readonly exitFn: ExitFn;
23
24 private adapters: PlatformAdapter[] = [];
25 private stopReason: StopReason | null = null;
26
27 constructor(server: Server, exitFn: ExitFn = (code) => process.exit(code)) {
28 this.server = server;
29 this.exitFn = exitFn;
30
31 }
32
33 registerAdapters(adapters: PlatformAdapter[]): void {
34 this.adapters = adapters;
35 }
36
37
38
39 async requestRestart(trigger: LifecycleTrigger): Promise<CommandResult> {
40 return this.requestStop("restart", trigger);
41 }
42
43 async requestShutdown(trigger: LifecycleTrigger): Promise<CommandResult> {
44 return this.requestStop("shutdown", trigger);
45 }
46
47 private async requestStop(
48 reason: StopReason,
49 trigger: LifecycleTrigger,
50 ): Promise<CommandResult> {
51 if (this.stopReason) {
52 const message =
53 this.stopReason === "restart"
54 ? "Restart already in progress."
55 : "Shutdown already in progress.";
56 return { success: true, message };
57 }
58
59 this.stopReason = reason;
60 console.log(`[Lifecycle] ${reason} requested via ${trigger}`);
61
62 setTimeout(() => {
63 void this.gracefulStopAndExit(reason);
64 }, 50);
65
66 return {
67 success: true,
68 message: reason === "restart" ? "Restarting..." : "Shutting down...",
69 };
70 }
71
72 private async gracefulStopAndExit(reason: StopReason): Promise<void> {
73 try {
74 await Promise.race([
75 this.gracefulStop(),
76 new Promise<void>((resolve) => {
77 setTimeout(() => {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected