centralu 0.1.0-beta.7 → 0.1.0-beta.71
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/bin/centralu.mjs +434 -61
- package/bin/node-floor.mjs +42 -0
- package/bin/platform.mjs +380 -0
- package/bin/semver.mjs +34 -1
- package/bin/serve-args.mjs +181 -0
- package/bin/serve-autostart.mjs +490 -0
- package/bin/serve-detach.mjs +377 -0
- package/bin/serve-files.mjs +773 -0
- package/bin/serve-handover.mjs +192 -0
- package/bin/serve-host.mjs +467 -0
- package/bin/serve-install.mjs +218 -0
- package/bin/serve-keys.mjs +126 -0
- package/bin/serve-port.mjs +79 -0
- package/bin/serve-run.mjs +290 -0
- package/bin/serve-switch.mjs +635 -0
- package/bin/serve-util.mjs +144 -0
- package/bin/serve-window.mjs +147 -0
- package/bin/serve-windows.mjs +181 -0
- package/bin/serve.mjs +39 -0
- package/bin/swap.mjs +48 -0
- package/package.json +10 -6
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `centralu serve --detach` and `--stop`: starting serve outside the ssh session that asked, and
|
|
3
|
+
* stopping it in order (docs/plans/remote-hub.md §10.4).
|
|
4
|
+
*/
|
|
5
|
+
import { spawn } from 'node:child_process'
|
|
6
|
+
import { existsSync, openSync, rmSync, writeFileSync, writeSync } from 'node:fs'
|
|
7
|
+
import { userInfo } from 'node:os'
|
|
8
|
+
import { join } from 'node:path'
|
|
9
|
+
import { answerLine, isAlive, say, sayTo, runCaptured, tail, sleep } from './serve-util.mjs'
|
|
10
|
+
import { SERVE_LOG_FILE, serveDataDir, ensureServeState, markStopped, rotateServeLog, markStarting, clearStarting, liveStarts, lockHolder, isWriterWithin, isWriter, readServePid, STARTING_MAX_MS } from './serve-files.mjs'
|
|
11
|
+
import { askPowershell, detachHow, windowsArg, wmiScript, parseWmiAnswer, wmiBlockedMessage, windowsPowershell, windowsTool, wslCommandLine } from './serve-windows.mjs'
|
|
12
|
+
import { CHILD_FLAG } from './serve-args.mjs'
|
|
13
|
+
import { startInSignIn } from './serve-autostart.mjs'
|
|
14
|
+
import { stopServedHost } from './serve-handover.mjs'
|
|
15
|
+
import { connectionLine, serveFrom } from './serve-host.mjs'
|
|
16
|
+
import { describe, runServe, serveFolder } from './serve-run.mjs'
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* What a detached launcher needs from the `--detach` that started it. A process created through WMI
|
|
20
|
+
* does not inherit this environment: it gets the account's default one. Everything else it needs
|
|
21
|
+
* (the token, the port) is in the data folder. Never a secret.
|
|
22
|
+
*/
|
|
23
|
+
export const CARRIED_ENV = ['CC_DATA_DIR', 'CENTRALU_HOST_ENTRY', 'CENTRALU_MANAGED', 'CENTRALU_STARTED_FROM']
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* `start` is how the launcher was started and why (`through WMI: <reason>`), which it writes into its first serve.log
|
|
27
|
+
* line, so a host that ended up outside the Windows sign-in says why (#1223)
|
|
28
|
+
*/
|
|
29
|
+
export function encodeChildSpec({ env, log, start = null }) {
|
|
30
|
+
const carried = {}
|
|
31
|
+
for (const k of CARRIED_ENV) if (typeof env[k] === 'string' && env[k]) carried[k] = env[k]
|
|
32
|
+
return Buffer.from(JSON.stringify({ log, env: carried, ...(start ? { start } : {}) }), 'utf8').toString('base64url')
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** `{ log, env, start? }` from `--detached-child=`, only the carried variables; null when it is not one */
|
|
36
|
+
export function decodeChildSpec(raw) {
|
|
37
|
+
try {
|
|
38
|
+
const v = JSON.parse(Buffer.from(String(raw), 'base64url').toString('utf8'))
|
|
39
|
+
if (typeof v?.log !== 'string' || !v.log) return null
|
|
40
|
+
const env = {}
|
|
41
|
+
for (const k of CARRIED_ENV) if (typeof v.env?.[k] === 'string') env[k] = v.env[k]
|
|
42
|
+
return { log: v.log, env, ...(typeof v.start === 'string' && v.start ? { start: v.start } : {}) }
|
|
43
|
+
} catch {
|
|
44
|
+
return null
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Starts the launcher outside this session (`detachHow`). `{ ok: true, pid, exited?, alive? }` once
|
|
50
|
+
* something was created, `{ ok: false, blocked, message }` when nothing was: `blocked` is the WMI
|
|
51
|
+
* refusal the hub falls back from.
|
|
52
|
+
*/
|
|
53
|
+
export async function startDetached({ how, execPath, args, env, cwd }) {
|
|
54
|
+
if (how === 'setsid') {
|
|
55
|
+
return await new Promise((resolve) => {
|
|
56
|
+
let child
|
|
57
|
+
try {
|
|
58
|
+
child = spawn(execPath, args, { detached: true, stdio: 'ignore', cwd, env, windowsHide: true })
|
|
59
|
+
} catch (e) {
|
|
60
|
+
return resolve({ ok: false, blocked: false, message: `could not start ${execPath}: ${e.message}` })
|
|
61
|
+
}
|
|
62
|
+
child.once('error', (e) => resolve({ ok: false, blocked: false, message: `could not start ${execPath}: ${e.message}` }))
|
|
63
|
+
child.once('spawn', () => {
|
|
64
|
+
const exited = new Promise((r) => child.once('exit', (code, signal) => r(signal ?? code)))
|
|
65
|
+
child.unref()
|
|
66
|
+
resolve({ ok: true, pid: child.pid, exited })
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
const ps = windowsPowershell(how, env)
|
|
71
|
+
if (!ps) return { ok: false, blocked: true, message: wmiBlockedMessage('Windows PowerShell is not reachable from this WSL distro') }
|
|
72
|
+
let commandLine
|
|
73
|
+
try {
|
|
74
|
+
if (how === 'wmi') commandLine = [execPath, ...args].map(windowsArg).join(' ')
|
|
75
|
+
else commandLine = wslCommandLine({ distro: env.WSL_DISTRO_NAME ?? '', user: userInfo().username, cwd, execPath, args })
|
|
76
|
+
} catch (e) {
|
|
77
|
+
return { ok: false, blocked: false, message: e.message }
|
|
78
|
+
}
|
|
79
|
+
const script = wmiScript({ commandLine, cwd: how === 'wmi' ? cwd : null })
|
|
80
|
+
const answer = await askPowershell(ps, script, env, parseWmiAnswer)
|
|
81
|
+
if (!answer.ok) return { ok: false, blocked: true, message: wmiBlockedMessage(answer.message) }
|
|
82
|
+
// A Windows pid can be watched from Windows; from inside WSL it names nothing here
|
|
83
|
+
/*
|
|
84
|
+
* Measured on a Windows 11 laptop (2026-10-08): a `wsl.exe` that WMI creates can hang without
|
|
85
|
+
* running anything in the distro (it starts a second wsl.exe and waits), while `wsl.exe --list`
|
|
86
|
+
* from the same context answers. `--detach` reports that as a start that never ran, and ends what
|
|
87
|
+
* it created rather than leave it waiting: this pid and its children, nothing else.
|
|
88
|
+
*/
|
|
89
|
+
const taskkill = windowsTool(how, env, ['System32', 'taskkill.exe'])
|
|
90
|
+
const stop = taskkill ? () => runCaptured(taskkill, ['/PID', String(answer.pid), '/T', '/F'], env) : null
|
|
91
|
+
return { ok: true, pid: answer.pid, alive: how === 'wmi' ? () => isAlive(answer.pid) : null, stop }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* `centralu serve --detach` (docs/plans/remote-hub.md §10.4, S9): starts `serve` outside this
|
|
96
|
+
* session (`detachHow`) and returns once its host answers a hello, so the caller, typically the
|
|
97
|
+
* hub over ssh, can connect at once.
|
|
98
|
+
*
|
|
99
|
+
* stdout carries one JSON line, read by the hub (`links/tunnel.ts`): the connection line with
|
|
100
|
+
* `hostRunning: true` and `detach: { ok: true, how, already }`, or `{ v: 1, detach: { ok: false,
|
|
101
|
+
* how, reason, message } }`. `reason` is `wmi_blocked` when WMI created nothing or created something
|
|
102
|
+
* that never ran, which the hub answers with a link-bound start; `exited` and `timeout` quote the
|
|
103
|
+
* end of serve.log. Exit codes do not survive Windows' ssh (agent-host/remote/hub-links.md), so the line is
|
|
104
|
+
* what counts; the exit code is 0 or 1 for a person at a prompt.
|
|
105
|
+
*
|
|
106
|
+
* `start` and the two waits are parameters so tests can stand in for WMI.
|
|
107
|
+
*/
|
|
108
|
+
export async function runDetach({
|
|
109
|
+
env,
|
|
110
|
+
home,
|
|
111
|
+
entry,
|
|
112
|
+
version,
|
|
113
|
+
cliPath,
|
|
114
|
+
port = null,
|
|
115
|
+
from = null,
|
|
116
|
+
platform = process.platform,
|
|
117
|
+
execPath = process.execPath,
|
|
118
|
+
cwd = process.cwd(),
|
|
119
|
+
start = startDetached,
|
|
120
|
+
how = detachHow(platform, env),
|
|
121
|
+
signIn = startInSignIn,
|
|
122
|
+
signInMs = 20_000,
|
|
123
|
+
timeoutMs = 30_000,
|
|
124
|
+
noStartMs = 10_000,
|
|
125
|
+
write = (line) => process.stdout.write(line),
|
|
126
|
+
// What the folder's host answers (a test's stand-in for the host coming up at a given moment)
|
|
127
|
+
look = describe,
|
|
128
|
+
}) {
|
|
129
|
+
// Created here, so the token exists before the launcher reads it and `answered` reads it again
|
|
130
|
+
const { dataDir } = serveFolder({ env, home, cliPath, platform })
|
|
131
|
+
const fail = (reason, message) => answerLine(write, 'detach', { ok: false, how, reason, message })
|
|
132
|
+
const answered = async (already, as = how) => {
|
|
133
|
+
const d = await look({ env, dataDir, state: ensureServeState(dataDir, platform), entry, version })
|
|
134
|
+
if (!d?.hostRunning) return false
|
|
135
|
+
write(`${connectionLine({ ...d, detach: { ok: true, how: as, already } })}\n`)
|
|
136
|
+
return true
|
|
137
|
+
}
|
|
138
|
+
if (await answered(true)) return 0
|
|
139
|
+
/*
|
|
140
|
+
* From here a start is in progress (#2150): a `--stop` meanwhile waits for it rather than find nothing running, write
|
|
141
|
+
* the stopped mark and leave the host to come up after it. This process's record is removed on every way out; the
|
|
142
|
+
* launcher it starts writes its own, which covers a launcher still starting when this gave up (`SERVE_STARTING_DIR`)
|
|
143
|
+
*/
|
|
144
|
+
markStarting(dataDir)
|
|
145
|
+
try {
|
|
146
|
+
const log = join(dataDir, SERVE_LOG_FILE)
|
|
147
|
+
/*
|
|
148
|
+
* One start per log: the earlier ones are kept beside it, three of them (`rotateServeLog`, #1143), and its absence is
|
|
149
|
+
* how a start that never ran shows. So a log that cannot be moved aside is removed rather than left to read as a start
|
|
150
|
+
*/
|
|
151
|
+
const freshLog = () => rotateServeLog(log)
|
|
152
|
+
|
|
153
|
+
/*
|
|
154
|
+
* Windows: in the person's sign-in when someone is signed in (#1224), through the scheduled task, and through WMI only
|
|
155
|
+
* when nobody is. WMI's process keeps this ssh session's network logon, where Git cannot use the credentials saved in
|
|
156
|
+
* Windows. A task-started host that does not answer in time is left to the lock: the WMI start below then either
|
|
157
|
+
* holds the folder or is refused by it, never a second host on it. Which way it went, and why, is one line of
|
|
158
|
+
* serve.log (#1223): the task's serve writes no serve.log, so that line is written here; a WMI launcher writes its own
|
|
159
|
+
*/
|
|
160
|
+
let notInSignIn = null
|
|
161
|
+
if (how === 'wmi') {
|
|
162
|
+
const s = await signIn({ dataDir, env, home, platform })
|
|
163
|
+
if (s.started) {
|
|
164
|
+
const line = `started centralu serve in the Windows sign-in (the scheduled task "${s.name}"); waiting for its host`
|
|
165
|
+
say(line)
|
|
166
|
+
freshLog()
|
|
167
|
+
try {
|
|
168
|
+
writeFileSync(log, `[centralu serve] ${line}\n`)
|
|
169
|
+
} catch {
|
|
170
|
+
// the line on stderr is all there is
|
|
171
|
+
}
|
|
172
|
+
const t0 = Date.now()
|
|
173
|
+
while (Date.now() - t0 < signInMs) {
|
|
174
|
+
if (await answered(false, 'task')) return 0
|
|
175
|
+
await sleep(250)
|
|
176
|
+
}
|
|
177
|
+
notInSignIn = `the host the sign-in task "${s.name}" started did not answer within ${Math.round(signInMs / 1000)} s`
|
|
178
|
+
// The WMI launcher's first line says so; the task's line goes, so `serve.log.1` keeps the start before this one
|
|
179
|
+
rmSync(log, { force: true, maxRetries: 5 })
|
|
180
|
+
} else {
|
|
181
|
+
notInSignIn = signInMissed(s)
|
|
182
|
+
}
|
|
183
|
+
say(`${notInSignIn}: starting the host through WMI${s.reason === 'nosession' ? ', outside any sign-in, where Git cannot use the credentials saved in Windows' : ''}`)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
freshLog()
|
|
187
|
+
// Where this was asked for from (#1223): the launcher runs outside this ssh session, so it is told
|
|
188
|
+
const asked = serveFrom(env, from)
|
|
189
|
+
const childEnv = asked ? { ...env, CENTRALU_STARTED_FROM: JSON.stringify(asked) } : env
|
|
190
|
+
const startedHow = `through ${how === 'wmi' ? 'WMI' : how === 'wsl' ? 'a WMI-created wsl.exe' : how}${notInSignIn ? `: ${notInSignIn}` : ''}`
|
|
191
|
+
const args = [cliPath, 'serve', ...(port !== null ? ['--port', String(port)] : []), `${CHILD_FLAG}${encodeChildSpec({ env: childEnv, log, start: startedHow })}`]
|
|
192
|
+
let started
|
|
193
|
+
try {
|
|
194
|
+
started = await start({ how, execPath, args, env, cwd })
|
|
195
|
+
} catch (e) {
|
|
196
|
+
return fail('failed', `could not start centralu serve in the background: ${e.message}`)
|
|
197
|
+
}
|
|
198
|
+
if (!started.ok) return fail(started.blocked ? 'wmi_blocked' : 'failed', started.message)
|
|
199
|
+
say(`started centralu serve in the background (${how}, pid ${started.pid ?? 'unknown'}); waiting for its host`)
|
|
200
|
+
|
|
201
|
+
let exited
|
|
202
|
+
started.exited?.then((c) => (exited = c ?? 'unknown'))
|
|
203
|
+
const t0 = Date.now()
|
|
204
|
+
while (Date.now() - t0 < timeoutMs) {
|
|
205
|
+
if (await answered(false)) return 0
|
|
206
|
+
const why = tail(log)
|
|
207
|
+
if (exited !== undefined) return fail('exited', `centralu serve exited (${exited}) before its host answered${why ? `:\n${why}` : ''}`)
|
|
208
|
+
const gone = started.alive?.() === false
|
|
209
|
+
if (how !== 'setsid' && !existsSync(log) && (gone || Date.now() - t0 > noStartMs)) {
|
|
210
|
+
// WMI said yes, and nothing ran: the process was stopped before Node started, or hangs (`startDetached`)
|
|
211
|
+
if (!gone) await started.stop?.()
|
|
212
|
+
return fail('wmi_blocked', wmiBlockedMessage(gone ? `process ${started.pid} ended before it started` : `nothing started within ${Math.round(noStartMs / 1000)} s`))
|
|
213
|
+
}
|
|
214
|
+
if (gone) return fail('exited', `centralu serve exited before its host answered${why ? `:\n${why}` : ''}`)
|
|
215
|
+
await sleep(250)
|
|
216
|
+
}
|
|
217
|
+
const why = tail(log)
|
|
218
|
+
return fail('timeout', `the host did not answer within ${Math.round(timeoutMs / 1000)} s${why ? `:\n${why}` : ''}`)
|
|
219
|
+
} finally {
|
|
220
|
+
clearStarting(dataDir)
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Why the host was not started in the Windows sign-in, from `startInSignIn`'s answer, for serve.log and stderr */
|
|
225
|
+
export function signInMissed(s) {
|
|
226
|
+
if (s.reason === 'nosession') {
|
|
227
|
+
const n = s.desktops
|
|
228
|
+
const unread = s.unreadable ? `, ${s.unreadable} of them with an owner WMI would not tell` : ''
|
|
229
|
+
const seen =
|
|
230
|
+
typeof n !== 'number'
|
|
231
|
+
? 'no Explorer of this account in a desktop session'
|
|
232
|
+
: n === 0
|
|
233
|
+
? 'no Explorer in any desktop session'
|
|
234
|
+
: `none of the ${n} Explorer${n === 1 ? '' : 's'} in desktop sessions is this account's${unread}`
|
|
235
|
+
return `no Windows sign-in of this user was found (${seen})`
|
|
236
|
+
}
|
|
237
|
+
// It travels on the launcher's command line: bounded
|
|
238
|
+
if (s.reason === 'error') return `the sign-in task failed: ${String(s.message).slice(0, 300)}`
|
|
239
|
+
if (s.reason === 'off') return 'the sign-in task is turned off (CENTRALU_SIGNIN_TASK=off)'
|
|
240
|
+
if (s.reason === 'no_launcher') return 'there is no launcher in the data folder for the sign-in task to run'
|
|
241
|
+
return `the sign-in task was not used (${s.reason})`
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* The launcher `--detach` started: `runServe` writing to serve.log. The carried variables are
|
|
246
|
+
* already in `env` (centralu.mjs merges them before it looks for the host).
|
|
247
|
+
*/
|
|
248
|
+
export async function runDetachedChild({ spec, ...opts }) {
|
|
249
|
+
let fd = null
|
|
250
|
+
try {
|
|
251
|
+
fd = openSync(spec.log, 'a')
|
|
252
|
+
sayTo((text) => {
|
|
253
|
+
try {
|
|
254
|
+
writeSync(fd, text)
|
|
255
|
+
} catch {
|
|
256
|
+
// nowhere left to say it
|
|
257
|
+
}
|
|
258
|
+
})
|
|
259
|
+
} catch {
|
|
260
|
+
// no log: say() keeps stderr, which nobody reads; --detach then reports the start as never run
|
|
261
|
+
}
|
|
262
|
+
// One line saying how it was started and why (#1223), and what runs
|
|
263
|
+
say(`started in the background${spec.start ? ` ${spec.start}` : ''} (pid ${process.pid}, Node ${process.version})`)
|
|
264
|
+
try {
|
|
265
|
+
return await runServe({ ...opts, logFd: fd })
|
|
266
|
+
} catch (e) {
|
|
267
|
+
say(e?.message ?? String(e))
|
|
268
|
+
return 1
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* `centralu serve --stop` (docs/plans/remote-hub.md §10.4, S10): the running host stopped in order, over its own
|
|
274
|
+
* socket, and waited for (`stopServedHost` in serve-handover.mjs, which the hand-over into the sign-in shares, #1224).
|
|
275
|
+
*
|
|
276
|
+
* stdout: one line, `{ v: 1, stop: { ok, wasRunning, how?, message? } }`.
|
|
277
|
+
*
|
|
278
|
+
* A stop that went through is remembered in the data folder (`markStopped`, #1143), so a linked hub does not start the
|
|
279
|
+
* host again by itself; the next host that starts here removes the mark. Not for a hub's own stop, which starts the
|
|
280
|
+
* host again itself (an update, a rollback): the hub runs it with `CENTRALU_STOP_BY=hub` (links/tunnel.ts `stopCommand`).
|
|
281
|
+
* The window's "Quit completely" on this host runs it with `CENTRALU_STOP_BY=quit` (#1466), so the mark says the person quit
|
|
282
|
+
* Centralu there, as the app's own host's does, not that they ran `serve --stop`.
|
|
283
|
+
* With `CENTRALU_STOP_FOR=update` too (#1618), the host is asked to list the turns it ends, which the next host continues
|
|
284
|
+
*/
|
|
285
|
+
export async function runStop({
|
|
286
|
+
env,
|
|
287
|
+
home,
|
|
288
|
+
entry,
|
|
289
|
+
platform = process.platform,
|
|
290
|
+
budgetMs = STOP_BUDGET_MS,
|
|
291
|
+
graceMs = 10_000,
|
|
292
|
+
kill = (pid, sig) => process.kill(pid, sig),
|
|
293
|
+
write = (line) => process.stdout.write(line),
|
|
294
|
+
writer = isWriter,
|
|
295
|
+
}) {
|
|
296
|
+
const dataDir = serveDataDir(env, home)
|
|
297
|
+
const deadline = Date.now() + budgetMs
|
|
298
|
+
const mark = () => {
|
|
299
|
+
if (env.CENTRALU_STOP_BY !== 'hub') markStopped(dataDir, env.CENTRALU_STOP_BY === 'quit' ? 'quit' : 'stop')
|
|
300
|
+
}
|
|
301
|
+
/*
|
|
302
|
+
* A start in progress (#2150, `SERVE_STARTING_DIR`): its host is not up yet, so the folder reads as not running, and
|
|
303
|
+
* stopped here it would come up after the stop, under a stopped mark. Waited for while it has not taken the folder: a
|
|
304
|
+
* host that has held host.lock for `LOCK_GRACE_MS` with its start still named is stopped as any running host is,
|
|
305
|
+
* answering or not (one that hung before it listened, #1903, is ended by `stopServedHost`). The wait leaves the stop its share of the budget; when that is reached, a start
|
|
306
|
+
* whose launcher is known (serve.pid) is ended, the launcher and the host it started, and any other is said to be
|
|
307
|
+
* still starting, with nothing stopped
|
|
308
|
+
*/
|
|
309
|
+
const known = new Map()
|
|
310
|
+
// Since when a live host holds host.lock while a start is still named: a host that is up ends its start within a moment
|
|
311
|
+
let heldSince = null
|
|
312
|
+
const tookTheFolder = () => {
|
|
313
|
+
const h = lockHolder(dataDir)
|
|
314
|
+
const key = h ? `lock:${h.pid}:${h.started}:${h.at}` : null
|
|
315
|
+
// A host started in another logon (its creation unreadable) took the folder too: counted for as long as a start takes
|
|
316
|
+
if (key && !known.has(key)) known.set(key, isWriterWithin(h, STARTING_MAX_MS, writer))
|
|
317
|
+
if (!key || !known.get(key) || !isAlive(h.pid)) return (heldSince = null), false
|
|
318
|
+
heldSince ??= Date.now()
|
|
319
|
+
return Date.now() - heldSince > LOCK_GRACE_MS
|
|
320
|
+
}
|
|
321
|
+
let waiting = false
|
|
322
|
+
for (let starts = liveStarts(dataDir, known, { writer }); starts.length > 0 && !tookTheFolder(); starts = liveStarts(dataDir, known, { writer })) {
|
|
323
|
+
if (!waiting) say(`centralu serve is starting a host for ${dataDir}; waiting for it, to stop it`)
|
|
324
|
+
waiting = true
|
|
325
|
+
if (Date.now() < deadline - STOP_RESERVE_MS) {
|
|
326
|
+
await sleep(100)
|
|
327
|
+
continue
|
|
328
|
+
}
|
|
329
|
+
const launcher = readServePid(dataDir)
|
|
330
|
+
const ending = launcher && starts.some((r) => r.pid === launcher.pid) ? [launcher.hostPid, launcher.pid].filter((p) => Number.isInteger(p) && isAlive(p)) : []
|
|
331
|
+
if (ending.length === 0) {
|
|
332
|
+
return answerLine(write, 'stop', { ok: false, wasRunning: true, starting: true, message: `centralu serve is still starting a host for ${dataDir}; nothing was stopped` })
|
|
333
|
+
}
|
|
334
|
+
for (const pid of ending) {
|
|
335
|
+
try {
|
|
336
|
+
kill(pid, platform === 'win32' ? 'SIGKILL' : 'SIGTERM')
|
|
337
|
+
} catch {
|
|
338
|
+
// gone in the meantime
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
const t1 = Date.now()
|
|
342
|
+
while (ending.some((p) => isAlive(p)) && Date.now() - t1 < 5_000) await sleep(100)
|
|
343
|
+
for (const pid of ending.filter((p) => isAlive(p))) {
|
|
344
|
+
try {
|
|
345
|
+
kill(pid, 'SIGKILL')
|
|
346
|
+
} catch {
|
|
347
|
+
// gone in the meantime
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
const gone = !ending.some((p) => isAlive(p))
|
|
351
|
+
if (gone) mark()
|
|
352
|
+
return answerLine(write, 'stop', gone
|
|
353
|
+
? { ok: true, wasRunning: true, how: 'ended', message: `ended a start that was still in progress (pid ${ending.join(', ')})` }
|
|
354
|
+
: { ok: false, wasRunning: true, starting: true, message: `centralu serve is still starting a host for ${dataDir}, and pid ${ending.join(', ')} did not end` })
|
|
355
|
+
}
|
|
356
|
+
// What is left of the budget: the host is waited for, after its request to stop, for all of it but the grace and the ends that follow
|
|
357
|
+
const left = deadline - Date.now()
|
|
358
|
+
const timeoutMs = Math.max(3_000, left - graceMs - 10_000)
|
|
359
|
+
const stopped = await stopServedHost({ dataDir, entry, platform, forUpdate: env.CENTRALU_STOP_FOR === 'update', timeoutMs, graceMs, kill })
|
|
360
|
+
if (stopped.ok) mark()
|
|
361
|
+
return answerLine(write, 'stop', stopped)
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* The longest a whole `serve --stop` takes (#2150 review): the window's "Quit completely" gives it 60 s (keeper
|
|
366
|
+
* `serve_window.rs` `STOP_TIMEOUT`), and so a start waited for and the stop after it fit in this together, with room.
|
|
367
|
+
* Worst case: the wait up to `STOP_BUDGET_MS - STOP_RESERVE_MS`, then the host asked and waited for (at least 3 s), its
|
|
368
|
+
* grace after a signal (10 s; Windows 5) and the ends that follow (5 s and 5 s)
|
|
369
|
+
*/
|
|
370
|
+
export const STOP_BUDGET_MS = 45_000
|
|
371
|
+
/**
|
|
372
|
+
* How long a host may hold host.lock while its start is still named before `--stop` stops it rather than wait: a host that is
|
|
373
|
+
* up ends its start at its ready line, and `--detach` answers within a look (250 ms) of that; one past this hung
|
|
374
|
+
*/
|
|
375
|
+
export const LOCK_GRACE_MS = 10_000
|
|
376
|
+
/** What the wait for a start leaves of the budget for the stop itself */
|
|
377
|
+
export const STOP_RESERVE_MS = 23_000
|