@procedures/peer 0.2.3 → 0.4.0
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.
- package/CHANGELOG.md +75 -0
- package/README.md +3 -3
- package/dist/.tsbuildinfo +1 -1
- package/dist/connection/index.d.ts +3 -2
- package/dist/connection/index.d.ts.map +1 -1
- package/dist/connection/index.js +6 -2
- package/dist/connection/index.js.map +1 -1
- package/dist/discovery/index.d.ts +4 -2
- package/dist/discovery/index.d.ts.map +1 -1
- package/dist/discovery/index.js +22 -34
- package/dist/discovery/index.js.map +1 -1
- package/dist/input-schemas.d.ts +5 -7
- package/dist/input-schemas.d.ts.map +1 -1
- package/dist/input-schemas.js +4 -15
- package/dist/input-schemas.js.map +1 -1
- package/dist/module.d.ts +2 -1
- package/dist/module.d.ts.map +1 -1
- package/dist/module.js +2 -7
- package/dist/module.js.map +1 -1
- package/dist/output-schemas.d.ts +1 -4
- package/dist/output-schemas.d.ts.map +1 -1
- package/dist/output-schemas.js +0 -23
- package/dist/output-schemas.js.map +1 -1
- package/dist/peer/index.d.ts +16 -3
- package/dist/peer/index.d.ts.map +1 -1
- package/dist/peer/index.js +45 -6
- package/dist/peer/index.js.map +1 -1
- package/dist/procedures/index.d.ts +0 -6
- package/dist/procedures/index.d.ts.map +1 -1
- package/dist/procedures/index.js +1 -3
- package/dist/procedures/index.js.map +1 -1
- package/dist/procedures/manifest.generate.d.ts.map +1 -1
- package/dist/procedures/manifest.generate.js +10 -33
- package/dist/procedures/manifest.generate.js.map +1 -1
- package/dist/procedures/server.call.d.ts.map +1 -1
- package/dist/procedures/server.call.js +8 -3
- package/dist/procedures/server.call.js.map +1 -1
- package/dist/procedures/server.connect.d.ts.map +1 -1
- package/dist/procedures/server.connect.js +3 -1
- package/dist/procedures/server.connect.js.map +1 -1
- package/dist/types.d.ts +2 -58
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/connection/index.ts +8 -5
- package/src/discovery/index.ts +26 -35
- package/src/input-schemas.ts +4 -27
- package/src/module.ts +2 -7
- package/src/output-schemas.ts +0 -29
- package/src/peer/index.ts +70 -7
- package/src/procedures/index.ts +1 -8
- package/src/procedures/manifest.generate.ts +10 -33
- package/src/procedures/server.call.ts +8 -3
- package/src/procedures/server.connect.ts +3 -1
- package/src/types.ts +2 -76
- package/dist/procedures/server.start.d.ts +0 -14
- package/dist/procedures/server.start.d.ts.map +0 -1
- package/dist/procedures/server.start.js +0 -182
- package/dist/procedures/server.start.js.map +0 -1
- package/dist/procedures/server.status.d.ts +0 -13
- package/dist/procedures/server.status.d.ts.map +0 -1
- package/dist/procedures/server.status.js +0 -118
- package/dist/procedures/server.status.js.map +0 -1
- package/dist/procedures/server.stop.d.ts +0 -12
- package/dist/procedures/server.stop.d.ts.map +0 -1
- package/dist/procedures/server.stop.js +0 -98
- package/dist/procedures/server.stop.js.map +0 -1
- package/src/procedures/server.start.ts +0 -212
- package/src/procedures/server.status.ts +0 -141
- package/src/procedures/server.stop.ts +0 -123
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Procedure: server.stop
|
|
3
|
-
* Stops the running CLI server daemon
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { createProcedure, type Effects, type Procedure } from "@procedures/client";
|
|
7
|
-
import { wrapSchema } from "../schema.js";
|
|
8
|
-
import { ServerStopOutputSchema } from "../output-schemas.js";
|
|
9
|
-
import { serverStopInputSchema } from "../input-schemas.js";
|
|
10
|
-
import { checkServer, readAllLockfiles, readLockfileForPort, removeLockfileForPort, type LockfileData } from "../lockfile.js";
|
|
11
|
-
import type { ServerStopInput, ServerStopOutput } from "../types.js";
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const serverStopOutputSchema = wrapSchema<ServerStopOutput>(ServerStopOutputSchema);
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Kill process by PID
|
|
22
|
-
*/
|
|
23
|
-
function killProcess(pid: number, signal: NodeJS.Signals = "SIGTERM"): boolean {
|
|
24
|
-
try {
|
|
25
|
-
process.kill(pid, signal);
|
|
26
|
-
return true;
|
|
27
|
-
} catch {
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export const serverStopProcedure: Procedure<
|
|
33
|
-
ServerStopInput,
|
|
34
|
-
ServerStopOutput,
|
|
35
|
-
{ description: string; effects: Effects }
|
|
36
|
-
> = createProcedure()
|
|
37
|
-
.path(["server", "stop"])
|
|
38
|
-
.input(serverStopInputSchema)
|
|
39
|
-
.output(serverStopOutputSchema)
|
|
40
|
-
.meta({
|
|
41
|
-
description: "Stop running CLI server",
|
|
42
|
-
// It stops the servers of the lockfiles and deletes their lockfiles
|
|
43
|
-
effects: { reads: ["fs", "server"], writes: ["fs", "server"], idempotent: true },
|
|
44
|
-
args: [],
|
|
45
|
-
shorts: { port: "p" },
|
|
46
|
-
})
|
|
47
|
-
.handler(async (input: ServerStopInput): Promise<ServerStopOutput> => {
|
|
48
|
-
const force = input.force ?? false;
|
|
49
|
-
const signal: NodeJS.Signals = force ? "SIGKILL" : "SIGTERM";
|
|
50
|
-
|
|
51
|
-
// Get target servers
|
|
52
|
-
let targets: LockfileData[];
|
|
53
|
-
if (input.port !== undefined) {
|
|
54
|
-
const lockfile = readLockfileForPort(input.port);
|
|
55
|
-
targets = lockfile ? [lockfile] : [];
|
|
56
|
-
} else {
|
|
57
|
-
targets = readAllLockfiles();
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (targets.length === 0) {
|
|
61
|
-
return {
|
|
62
|
-
success: false,
|
|
63
|
-
message: input.port
|
|
64
|
-
? `No server found on port ${input.port}`
|
|
65
|
-
: "No servers running",
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const results: string[] = [];
|
|
70
|
-
let allSuccess = true;
|
|
71
|
-
|
|
72
|
-
for (const lockfile of targets) {
|
|
73
|
-
const { pid, port } = lockfile;
|
|
74
|
-
|
|
75
|
-
// Signal the PID only when the server answers as the peer of this lockfile: Windows reuses
|
|
76
|
-
// PIDs, and before, a stale lockfile made this kill an unrelated process (deep dive CLI-4)
|
|
77
|
-
const state = await checkServer(lockfile);
|
|
78
|
-
if (state !== "alive") {
|
|
79
|
-
removeLockfileForPort(port);
|
|
80
|
-
results.push(
|
|
81
|
-
state === "dead"
|
|
82
|
-
? `Port ${port}: cleaned up stale lockfile`
|
|
83
|
-
: `Port ${port}: another process answers on this port; nothing stopped (stale lockfile removed)`
|
|
84
|
-
);
|
|
85
|
-
continue;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const sent = killProcess(pid, signal);
|
|
89
|
-
if (!sent) {
|
|
90
|
-
allSuccess = false;
|
|
91
|
-
results.push(`Port ${port}: failed to send ${signal} to PID ${pid}`);
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// Wait for process to die
|
|
96
|
-
const maxWait = force ? 500 : 5000;
|
|
97
|
-
const startTime = Date.now();
|
|
98
|
-
let stopped = false;
|
|
99
|
-
|
|
100
|
-
while (Date.now() - startTime < maxWait) {
|
|
101
|
-
if ((await checkServer(lockfile, 300)) === "dead") {
|
|
102
|
-
removeLockfileForPort(port);
|
|
103
|
-
results.push(`Port ${port}: stopped (PID ${pid})`);
|
|
104
|
-
stopped = true;
|
|
105
|
-
break;
|
|
106
|
-
}
|
|
107
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (!stopped) {
|
|
111
|
-
allSuccess = false;
|
|
112
|
-
results.push(`Port ${port}: PID ${pid} did not stop${force ? "" : " (try --force)"}`);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return {
|
|
117
|
-
success: allSuccess,
|
|
118
|
-
message: results.join("\n"),
|
|
119
|
-
};
|
|
120
|
-
})
|
|
121
|
-
.build();
|
|
122
|
-
|
|
123
|
-
export type { ServerStopInput, ServerStopOutput };
|