@procedures/peer 0.3.0 → 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 +27 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/connection/index.d.ts.map +1 -1
- package/dist/connection/index.js +4 -1
- package/dist/connection/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/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/types.d.ts +0 -58
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/connection/index.ts +3 -1
- package/src/input-schemas.ts +4 -27
- package/src/module.ts +2 -7
- package/src/output-schemas.ts +0 -29
- package/src/procedures/index.ts +1 -8
- package/src/types.ts +0 -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,212 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Procedure: server.start
|
|
3
|
-
* Starts the CLI server as a background daemon
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { spawn } from "node:child_process";
|
|
7
|
-
import * as fs from "node:fs";
|
|
8
|
-
import * as path from "node:path";
|
|
9
|
-
import * as os from "node:os";
|
|
10
|
-
import { createProcedure, type Effects, type Procedure } from "@procedures/client";
|
|
11
|
-
import { wrapSchema } from "../schema.js";
|
|
12
|
-
import { ServerStartOutputSchema } from "../output-schemas.js";
|
|
13
|
-
import { serverStartInputSchema } from "../input-schemas.js";
|
|
14
|
-
import { checkServer, readLockfileForPort } from "../lockfile.js";
|
|
15
|
-
import type { ServerStartInput, ServerStartOutput } from "../types.js";
|
|
16
|
-
|
|
17
|
-
const MARK_DIR = path.join(os.homedir(), ".mark");
|
|
18
|
-
const SERVERS_DIR = path.join(MARK_DIR, "servers");
|
|
19
|
-
const LOG_PATH = path.join(MARK_DIR, "server.log");
|
|
20
|
-
const LOG_PREV_PATH = path.join(MARK_DIR, "server.log.1");
|
|
21
|
-
|
|
22
|
-
const serverStartOutputSchema = wrapSchema<ServerStartOutput>(ServerStartOutputSchema);
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Check if process is alive
|
|
27
|
-
*/
|
|
28
|
-
function isProcessAlive(pid: number): boolean {
|
|
29
|
-
try {
|
|
30
|
-
process.kill(pid, 0);
|
|
31
|
-
return true;
|
|
32
|
-
} catch {
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Rotate log file
|
|
39
|
-
*/
|
|
40
|
-
function rotateLog(): void {
|
|
41
|
-
try {
|
|
42
|
-
fs.mkdirSync(MARK_DIR, { recursive: true });
|
|
43
|
-
if (fs.existsSync(LOG_PATH)) {
|
|
44
|
-
fs.renameSync(LOG_PATH, LOG_PREV_PATH);
|
|
45
|
-
}
|
|
46
|
-
} catch {
|
|
47
|
-
// Ignore rotation errors
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Find the CLI entry point
|
|
53
|
-
*/
|
|
54
|
-
function findCliPath(): string {
|
|
55
|
-
// First, try to use the currently running script if it's the CLI
|
|
56
|
-
const currentScript = process.argv[1];
|
|
57
|
-
if (currentScript) {
|
|
58
|
-
// Normalize path for cross-platform check
|
|
59
|
-
const normalized = currentScript.replace(/\\/g, "/");
|
|
60
|
-
// mark runs as dist/bin.js (its bin) or dist/cli.js
|
|
61
|
-
if (normalized.endsWith("/dist/bin.js") || normalized.endsWith("cli.js") || normalized.includes("cli/dist")) {
|
|
62
|
-
// Return absolute path
|
|
63
|
-
return path.resolve(currentScript);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Try to find the mark CLI in node_modules or as a direct path
|
|
68
|
-
const candidates = [
|
|
69
|
-
path.join(process.cwd(), "node_modules", "@procedures", "mark", "dist", "cli.js"),
|
|
70
|
-
path.join(process.cwd(), "node_modules", ".bin", "mark"),
|
|
71
|
-
// Check parent directories for monorepo setups
|
|
72
|
-
path.join(process.cwd(), "..", "cli", "dist", "cli.js"),
|
|
73
|
-
// Direct check in current directory
|
|
74
|
-
path.join(process.cwd(), "dist", "cli.js"),
|
|
75
|
-
];
|
|
76
|
-
|
|
77
|
-
// Check which candidate exists
|
|
78
|
-
for (const candidate of candidates) {
|
|
79
|
-
if (fs.existsSync(candidate)) {
|
|
80
|
-
return candidate;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// Fallback: try mark in PATH (will fail if not installed globally)
|
|
85
|
-
return "mark";
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export const serverStartProcedure: Procedure<
|
|
89
|
-
ServerStartInput,
|
|
90
|
-
ServerStartOutput,
|
|
91
|
-
{ description: string; effects: Effects; args: string[]; shorts: Record<string, string> }
|
|
92
|
-
> = createProcedure()
|
|
93
|
-
.path(["server", "start"])
|
|
94
|
-
.input(serverStartInputSchema)
|
|
95
|
-
.output(serverStartOutputSchema)
|
|
96
|
-
.meta({
|
|
97
|
-
description: "Start CLI server as background daemon",
|
|
98
|
-
// The daemon loads the procedure packages of the workspace. The procedure moves server.log
|
|
99
|
-
// over server.log.1.
|
|
100
|
-
effects: { reads: ["fs", "server"], writes: ["fs", "network", "process", "server"] },
|
|
101
|
-
args: [],
|
|
102
|
-
shorts: { port: "p", host: "h" },
|
|
103
|
-
})
|
|
104
|
-
.handler(async (input: ServerStartInput): Promise<ServerStartOutput> => {
|
|
105
|
-
const port = input.port ?? 3000;
|
|
106
|
-
// Loopback by default; pass host: "0.0.0.0" to expose on the LAN deliberately.
|
|
107
|
-
// See documentation/BUGS-2026-07.md (H19).
|
|
108
|
-
const host = input.host ?? "127.0.0.1";
|
|
109
|
-
const transport = input.transport ?? "http";
|
|
110
|
-
|
|
111
|
-
// Check if server is already running on this port
|
|
112
|
-
const existing = readLockfileForPort(port);
|
|
113
|
-
if (existing && (await checkServer(existing)) === "alive") {
|
|
114
|
-
return {
|
|
115
|
-
success: false,
|
|
116
|
-
message: `Server already running on port ${port} (PID ${existing.pid})`,
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// Rotate log file
|
|
121
|
-
rotateLog();
|
|
122
|
-
|
|
123
|
-
// Ensure directories exist
|
|
124
|
-
fs.mkdirSync(SERVERS_DIR, { recursive: true });
|
|
125
|
-
|
|
126
|
-
// Open log file for writing
|
|
127
|
-
const logFd = fs.openSync(LOG_PATH, "a");
|
|
128
|
-
|
|
129
|
-
// Find CLI path
|
|
130
|
-
const cliPath = findCliPath();
|
|
131
|
-
|
|
132
|
-
// Build arguments
|
|
133
|
-
const args = ["--server", "--port", String(port), "--host", host];
|
|
134
|
-
if (transport !== "http") {
|
|
135
|
-
args.push("--transport", transport);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// Determine how to spawn
|
|
139
|
-
let spawnCmd: string;
|
|
140
|
-
let spawnArgs: string[];
|
|
141
|
-
|
|
142
|
-
if (cliPath === "mark" || cliPath.endsWith(".bin/mark") || cliPath.endsWith(".bin\\mark")) {
|
|
143
|
-
// Use mark command directly
|
|
144
|
-
spawnCmd = cliPath;
|
|
145
|
-
spawnArgs = args;
|
|
146
|
-
} else {
|
|
147
|
-
// Use node to run the JS file
|
|
148
|
-
spawnCmd = process.execPath;
|
|
149
|
-
spawnArgs = [cliPath, ...args];
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// Spawn daemon process
|
|
153
|
-
const child = spawn(spawnCmd, spawnArgs, {
|
|
154
|
-
detached: true,
|
|
155
|
-
stdio: ["ignore", logFd, logFd],
|
|
156
|
-
env: { ...process.env },
|
|
157
|
-
cwd: process.cwd(),
|
|
158
|
-
// No console window on Windows (deep dive CLI-16)
|
|
159
|
-
windowsHide: true,
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
// Unref so parent can exit
|
|
163
|
-
child.unref();
|
|
164
|
-
fs.closeSync(logFd);
|
|
165
|
-
|
|
166
|
-
// Poll for lockfile (server takes ~4-8s to load ecosystem)
|
|
167
|
-
const maxWait = 15000;
|
|
168
|
-
const pollInterval = 500;
|
|
169
|
-
const startTime = Date.now();
|
|
170
|
-
|
|
171
|
-
while (Date.now() - startTime < maxWait) {
|
|
172
|
-
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
173
|
-
|
|
174
|
-
// Check if child died
|
|
175
|
-
if (child.pid && !isProcessAlive(child.pid)) {
|
|
176
|
-
break;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// Check for lockfile
|
|
180
|
-
const lockfile = readLockfileForPort(port);
|
|
181
|
-
if (lockfile && (await checkServer(lockfile)) === "alive") {
|
|
182
|
-
return {
|
|
183
|
-
success: true,
|
|
184
|
-
pid: lockfile.pid,
|
|
185
|
-
port: lockfile.port,
|
|
186
|
-
endpoint: lockfile.endpoint,
|
|
187
|
-
logFile: LOG_PATH,
|
|
188
|
-
message: `Server started (PID ${lockfile.pid})`,
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
// Timed out waiting for lockfile, but child may still be alive
|
|
194
|
-
if (child.pid && isProcessAlive(child.pid)) {
|
|
195
|
-
return {
|
|
196
|
-
success: true,
|
|
197
|
-
pid: child.pid,
|
|
198
|
-
port,
|
|
199
|
-
endpoint: `http://${host === "0.0.0.0" ? "localhost" : host}:${port}/api`,
|
|
200
|
-
logFile: LOG_PATH,
|
|
201
|
-
message: `Server starting (PID ${child.pid}, still loading...)`,
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
return {
|
|
206
|
-
success: false,
|
|
207
|
-
message: `Failed to start server. Check ${LOG_PATH} for details.`,
|
|
208
|
-
};
|
|
209
|
-
})
|
|
210
|
-
.build();
|
|
211
|
-
|
|
212
|
-
export type { ServerStartInput, ServerStartOutput };
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Procedure: server.status
|
|
3
|
-
* Gets the current CLI server status
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import * as path from "node:path";
|
|
7
|
-
import * as os from "node:os";
|
|
8
|
-
import { createProcedure, type Effects, type Procedure } from "@procedures/client";
|
|
9
|
-
import { wrapSchema } from "../schema.js";
|
|
10
|
-
import { ServerStatusOutputSchema } from "../output-schemas.js";
|
|
11
|
-
import { serverStatusInputSchema } from "../input-schemas.js";
|
|
12
|
-
import { checkServer, readAllLockfiles, readLockfileForPort, removeLockfileForPort, type LockfileData } from "../lockfile.js";
|
|
13
|
-
import type { ServerStatusInput, ServerStatusOutput } from "../types.js";
|
|
14
|
-
|
|
15
|
-
const MARK_DIR = path.join(os.homedir(), ".mark");
|
|
16
|
-
const LOG_PATH = path.join(MARK_DIR, "server.log");
|
|
17
|
-
|
|
18
|
-
const serverStatusOutputSchema = wrapSchema<ServerStatusOutput>(ServerStatusOutputSchema);
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Format uptime as human-readable string
|
|
26
|
-
*/
|
|
27
|
-
function formatUptime(startedAt: string): string {
|
|
28
|
-
const start = new Date(startedAt).getTime();
|
|
29
|
-
const now = Date.now();
|
|
30
|
-
const diff = Math.floor((now - start) / 1000);
|
|
31
|
-
|
|
32
|
-
if (diff < 60) {
|
|
33
|
-
return `${diff}s`;
|
|
34
|
-
}
|
|
35
|
-
if (diff < 3600) {
|
|
36
|
-
const mins = Math.floor(diff / 60);
|
|
37
|
-
const secs = diff % 60;
|
|
38
|
-
return `${mins}m ${secs}s`;
|
|
39
|
-
}
|
|
40
|
-
if (diff < 86400) {
|
|
41
|
-
const hours = Math.floor(diff / 3600);
|
|
42
|
-
const mins = Math.floor((diff % 3600) / 60);
|
|
43
|
-
return `${hours}h ${mins}m`;
|
|
44
|
-
}
|
|
45
|
-
const days = Math.floor(diff / 86400);
|
|
46
|
-
const hours = Math.floor((diff % 86400) / 3600);
|
|
47
|
-
return `${days}d ${hours}h`;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export const serverStatusProcedure: Procedure<
|
|
51
|
-
ServerStatusInput,
|
|
52
|
-
ServerStatusOutput,
|
|
53
|
-
{ description: string; effects: Effects; output: string }
|
|
54
|
-
> = createProcedure()
|
|
55
|
-
.path(["server", "status"])
|
|
56
|
-
.input(serverStatusInputSchema)
|
|
57
|
-
.output(serverStatusOutputSchema)
|
|
58
|
-
.meta({
|
|
59
|
-
description: "Get CLI server status",
|
|
60
|
-
// It deletes the lockfile of a server that does not answer
|
|
61
|
-
effects: { reads: ["fs", "server"], writes: ["fs"], idempotent: true },
|
|
62
|
-
args: [],
|
|
63
|
-
shorts: { port: "p" },
|
|
64
|
-
output: "text",
|
|
65
|
-
})
|
|
66
|
-
.handler(async (input: ServerStatusInput): Promise<ServerStatusOutput> => {
|
|
67
|
-
// Get target servers
|
|
68
|
-
let targets: LockfileData[];
|
|
69
|
-
if (input.port !== undefined) {
|
|
70
|
-
const lockfile = readLockfileForPort(input.port);
|
|
71
|
-
targets = lockfile ? [lockfile] : [];
|
|
72
|
-
} else {
|
|
73
|
-
targets = readAllLockfiles();
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (targets.length === 0) {
|
|
77
|
-
return {
|
|
78
|
-
running: false,
|
|
79
|
-
message: input.port
|
|
80
|
-
? `No server found on port ${input.port}`
|
|
81
|
-
: "No servers running",
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// For single server, return detailed info
|
|
86
|
-
if (targets.length === 1) {
|
|
87
|
-
const lockfile = targets[0]!;
|
|
88
|
-
const { pid, port, endpoint, startedAt, transport } = lockfile;
|
|
89
|
-
|
|
90
|
-
if ((await checkServer(lockfile)) !== "alive") {
|
|
91
|
-
removeLockfileForPort(port);
|
|
92
|
-
return {
|
|
93
|
-
running: false,
|
|
94
|
-
message: "Server is not running (cleaned up stale lockfile)",
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const uptime = formatUptime(startedAt);
|
|
99
|
-
return {
|
|
100
|
-
running: true,
|
|
101
|
-
pid,
|
|
102
|
-
port,
|
|
103
|
-
endpoint,
|
|
104
|
-
transport,
|
|
105
|
-
startedAt,
|
|
106
|
-
uptime,
|
|
107
|
-
logFile: LOG_PATH,
|
|
108
|
-
message: `Server running (PID ${pid}, port ${port}, uptime ${uptime})`,
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// Multiple servers - summarize
|
|
113
|
-
const lines: string[] = [];
|
|
114
|
-
let anyRunning = false;
|
|
115
|
-
|
|
116
|
-
for (const lockfile of targets) {
|
|
117
|
-
const { pid, port, endpoint, startedAt } = lockfile;
|
|
118
|
-
if ((await checkServer(lockfile)) !== "alive") {
|
|
119
|
-
removeLockfileForPort(port);
|
|
120
|
-
continue;
|
|
121
|
-
}
|
|
122
|
-
anyRunning = true;
|
|
123
|
-
const uptime = formatUptime(startedAt);
|
|
124
|
-
lines.push(` Port ${port}: PID ${pid}, uptime ${uptime}, ${endpoint}`);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (!anyRunning) {
|
|
128
|
-
return {
|
|
129
|
-
running: false,
|
|
130
|
-
message: "No servers running (cleaned up stale lockfiles)",
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return {
|
|
135
|
-
running: true,
|
|
136
|
-
message: `${lines.length} server(s) running:\n${lines.join("\n")}`,
|
|
137
|
-
};
|
|
138
|
-
})
|
|
139
|
-
.build();
|
|
140
|
-
|
|
141
|
-
export type { ServerStatusInput, ServerStatusOutput };
|
|
@@ -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 };
|