Repository navigation
Expand file tree
/
Copy pathdaemonCommands.ts
More file actions
504 lines (478 loc) · 21.1 KB
/
Copy pathdaemonCommands.ts
File metadata and controls
504 lines (478 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as fs from 'node:fs';
import * as path from 'node:path';
import { setTimeout as delayAsync } from 'node:timers/promises';
import {
DaemonClient,
DaemonClientError,
DaemonStartupPendingError,
connectOrStartDaemonAsync,
connectToStartingDaemonAsync,
describeLiveDaemonOwner,
findReclaimedDaemonPid,
inspectDaemonStartupReservation,
isDaemonOwnerStoppedAsync,
requestDaemonShutdownAsync,
resetDaemonArtifactsAsync,
resolveDaemonStartupReservationAsync,
type DaemonOwnerHintPurpose,
type IConnectOrStartDaemonOptions,
type IDaemonStartupReservationInfo
} from '@rushstack/rush-client-core';
import {
DaemonTransportError,
DaemonTransportErrorCode,
isDaemonProcessAlive,
readDaemonLockfile,
type IDaemonLockfile,
type IDaemonOrphanReap,
type IDaemonPaths
} from '@rushstack/rush-daemon-transport';
import type { IDaemonPongMessage, IDaemonRequestAdmissionOptions } from '@rushstack/rush-daemon-protocol';
import { getDaemonConnectionOptionsAsync } from './daemonConnectionOptions';
import { printDaemonLogAsync } from './daemonLogs';
import { executeDaemonGraphCommandAsync } from './daemonGraph';
import { createOrphanReapNoticeHandler, writeStderr } from './daemonReclaimNotice';
import { writeStreamAsync } from './writeStreamAsync';
const FORCE_STOP_WAIT_MS: number = 15000;
/** How long stop waits for a daemon that no longer listens to exit, for example because it is shutting down. */
const STOP_EXIT_WAIT_MS: number = 15000;
/** Stop says that it waits only when the daemon is still there after this long. */
const STOP_EXIT_NOTICE_MS: number = 1000;
const STOP_EXIT_POLL_MS: number = 100;
/**
* What a command that could not reach the daemon was for. `forceStop` is `daemon stop --force`: like a reset that a
* live owner refuses, it says that it killed no process.
*/
type ConnectionPurpose = DaemonOwnerHintPurpose | 'forceStop';
export interface IDaemonCommandOptions {
readonly argv: ReadonlyArray<string>;
readonly environment: Readonly<NodeJS.ProcessEnv>;
readonly rushJsonPath?: string;
readonly rushVersion: string;
readonly admission?: IDaemonRequestAdmissionOptions;
/** The resolved daemon settings. When no daemon runs, status uses them to say what the next command does. */
readonly daemonConfiguration?: { readonly enabled: boolean; readonly autoStart: boolean };
}
export async function executeDaemonCommandAsync(options: IDaemonCommandOptions): Promise<void> {
const command: string | undefined = options.argv[0];
if (options.admission && command !== 'graph') {
throw new Error(
'Daemon admission controls apply to command execution or graph requests, not lifecycle commands.'
);
}
if (command === 'graph') {
await executeDaemonGraphCommandAsync(options);
return;
}
if (
(options.argv.length !== 1 &&
!(
options.argv.length === 2 &&
((command === 'logs' && options.argv[1] === '--follow') ||
(command === 'stop' && options.argv[1] === '--force'))
)) ||
(command !== 'start' &&
command !== 'status' &&
command !== 'stop' &&
command !== 'restart' &&
command !== 'logs')
) {
throw new Error('Usage: rush-client daemon start|status|stop [--force]|restart|logs [--follow]');
}
if (!options.rushJsonPath) throw new Error('Daemon management requires a repository containing rush.json.');
const mayStart: boolean = command === 'start' || command === 'restart';
const connectionOptions: IConnectOrStartDaemonOptions = {
...(await getDaemonConnectionOptionsAsync(
path.dirname(options.rushJsonPath),
options.rushVersion,
options.environment,
mayStart
)),
// A start, or a forced stop, reclaims a daemon that exited uncleanly, and says what it stopped.
onOrphansReaped: createOrphanReapNoticeHandler({ rushx: false, agentRenderer: undefined, writeStderr })
};
if (command === 'logs') {
if (options.argv[1] !== '--follow') {
await printDaemonLogAsync(connectionOptions.paths);
return;
}
const abort: AbortController = new AbortController();
const onSignal = (): void => abort.abort(new Error('Daemon log following cancelled.'));
process.on('SIGINT', onSignal);
process.on('SIGTERM', onSignal);
try {
await printDaemonLogAsync(connectionOptions.paths, { follow: true, abortSignal: abort.signal });
} finally {
process.removeListener('SIGINT', onSignal);
process.removeListener('SIGTERM', onSignal);
}
// Node's process.stdout cannot be destroyed like an ordinary Writable. The log file is closed first.
if (abort.signal.aborted) process.exit(130);
return;
}
// Status observes the selected endpoint, including a compatible daemon from a different client version.
// It never starts a process or trusts a PID file as evidence of readiness.
const client: DaemonClient | undefined =
command === 'start'
? await connectOrStartDaemonAsync(connectionOptions)
: command === 'stop' && options.argv[1] !== '--force'
? await connectForStopAsync(connectionOptions)
: command === 'status'
? await connectForStatusAsync(connectionOptions, options)
: await connectExistingAsync(connectionOptions, true, command === 'stop' ? 'forceStop' : 'use');
if (!client) {
if (command === 'restart') {
// Nothing to shut down: restart behaves like start.
const started: DaemonClient = await connectOrStartDaemonAsync(connectionOptions);
try {
await writeReadyStatusAsync(connectionOptions.paths, await started.status);
} finally {
await started.closeAsync();
}
return;
}
if (command === 'stop' && options.argv[1] !== '--force') {
await waitForUnreachableDaemonExitAsync(connectionOptions.paths);
}
const reset: IForceResetResult | undefined =
options.argv[1] === '--force' ? await forceResetAsync(connectionOptions) : undefined;
await writeStatusAsync({
state: reset && reset.removedPaths.length > 0 ? 'reset' : 'notRunning',
socketPath: connectionOptions.paths.socketPath,
...reset,
...getStartupReservationStatus(connectionOptions.paths)
});
return;
}
try {
if (command === 'stop') {
if (options.argv[1] !== '--force') {
// Once the daemon is gone, nothing proves a remaining reservation stale, so it would refuse every later
// automatic start. A reservation that cannot be resolved is still reported; --force removes it below.
await resolveDaemonStartupReservationAsync(client, connectionOptions.paths).catch(() => false);
}
const { activeRequests } = await client.shutdownAsync();
if (activeRequests) {
await writeStreamAsync(
process.stderr,
Buffer.from(
`rush-client: the daemon was running ${activeRequests} request(s); they were cancelled.\n`
)
);
}
const cancelled: { cancelledRequests?: number } =
activeRequests === undefined ? {} : { cancelledRequests: activeRequests };
if (options.argv[1] === '--force') {
// Wait for the acknowledged daemon to release its listener and record, then clear leftovers
// such as an abandoned startup reservation in the same invocation.
const reset: IForceResetResult = await forceResetAsync(connectionOptions, FORCE_STOP_WAIT_MS);
await writeStatusAsync({
state: 'shutdownAccepted',
socketPath: connectionOptions.paths.socketPath,
...cancelled,
...reset
});
return;
}
await writeStatusAsync({
state: 'shutdownAccepted',
socketPath: connectionOptions.paths.socketPath,
...cancelled,
...getStartupReservationStatus(connectionOptions.paths)
});
return;
}
const readyClient: DaemonClient =
command === 'restart' ? await restartDaemonAsync(client, connectionOptions) : client;
try {
await writeReadyStatusAsync(connectionOptions.paths, await readyClient.status);
} finally {
if (readyClient !== client) await readyClient.closeAsync();
}
} finally {
await client.closeAsync();
}
}
/**
* Connects to the daemon that stop shuts down. When nothing listens yet but a daemon is still starting, stop
* waits for it to become ready and then stops it: reporting notRunning would leave it running afterwards.
*/
async function connectForStopAsync(options: IConnectOrStartDaemonOptions): Promise<DaemonClient | undefined> {
const client: DaemonClient | undefined = await connectExistingAsync(options, true, 'stop');
if (client) return client;
try {
return await connectToStartingDaemonAsync({
...options,
onAwaitStartup: (owner: string, waitMs: number): void => {
process.stderr.write(
`rush-client: The daemon is not ready yet. ${owner}, so stop waits up to ` +
`${Math.round(waitMs / 1000)} s for it to become ready and then stops it.\n`
);
}
});
} catch (error) {
if (!(error instanceof DaemonStartupPendingError)) throw error;
throw new Error(
`${error.message} It was not stopped; run "rush-client daemon stop" again once "rush-client daemon status" reports it ready.`
);
}
}
/** Returns undefined when nothing listens at the endpoint and `allowAbsent` is set; other failures propagate. */
async function connectExistingAsync(
options: IConnectOrStartDaemonOptions,
allowAbsent: boolean,
purpose: ConnectionPurpose
): Promise<DaemonClient | undefined> {
try {
return await DaemonClient.connectAsync({ socketPath: options.paths.socketPath });
} catch (error) {
if (
allowAbsent &&
error instanceof DaemonTransportError &&
error.code === DaemonTransportErrorCode.connectionRefused
) {
return undefined;
}
throw explainConnectionFailure(error, options.paths, purpose);
}
}
/**
* A daemon that no longer listens but still runs is shutting down, lost its socket, or is not running at all
* (a signal stopped it, for example). Stop waits for it to exit, and says what it is doing when it does not:
* reporting notRunning would leave it running. It does not wait for this workspace's daemon while a signal or a
* tracer keeps it stopped, because it cannot exit before something resumes it.
*/
async function waitForUnreachableDaemonExitAsync(paths: IDaemonPaths): Promise<void> {
const deadline: number = Date.now() + STOP_EXIT_WAIT_MS;
let noticed: boolean = false;
let owner: string | undefined = describeLiveDaemonOwner(paths, 'stop');
while (owner !== undefined) {
if (await isDaemonOwnerStoppedAsync(paths, deadline)) {
// The sampling took 1.5 s: say what the process is doing now.
owner = describeLiveDaemonOwner(paths, 'stop');
if (owner === undefined) return;
throw new Error(`Nothing listens at ${paths.socketPath}, but ${owner}`);
}
const remainingMs: number = deadline - Date.now();
if (remainingMs <= 0) throw new Error(`Nothing listens at ${paths.socketPath}, but ${owner}`);
if (!noticed && remainingMs <= STOP_EXIT_WAIT_MS - STOP_EXIT_NOTICE_MS) {
noticed = true;
const description: string = owner.split('\n')[0].replace(/\.$/, '');
await writeStreamAsync(
process.stderr,
Buffer.from(
`rush-client: Nothing listens at ${paths.socketPath}, but ${description}, so stop waits up to ${Math.round(remainingMs / 1000)} s for it to exit.\n`
)
);
}
await delayAsync(Math.min(STOP_EXIT_POLL_MS, remainingMs));
owner = describeLiveDaemonOwner(paths, 'stop');
}
}
/**
* Connects for `daemon status`. When nothing listens and nothing is left at the endpoint, which is the state
* after `daemon stop`, the idle timeout or a signal, it fails with a diagnostic that says so instead of the
* transport's refused connection. It also names a daemon that a client reclaimed after it exited without
* shutting down, until a daemon becomes ready again.
*/
async function connectForStatusAsync(
connectionOptions: IConnectOrStartDaemonOptions,
options: IDaemonCommandOptions
): Promise<DaemonClient> {
const { paths } = connectionOptions;
try {
return await DaemonClient.connectAsync({ socketPath: paths.socketPath });
} catch (error) {
if (
error instanceof DaemonTransportError &&
error.code === DaemonTransportErrorCode.connectionRefused &&
isEndpointAbsent(paths)
) {
throw new DaemonTransportError(
error.code,
`${describeAbsentDaemon(options)}${describeReclaimedDaemon(paths)}`
);
}
throw explainConnectionFailure(error, paths, 'use');
}
}
function explainConnectionFailure(error: unknown, paths: IDaemonPaths, purpose: ConnectionPurpose): unknown {
return explainStartupReservation(
explainLiveOwner(explainExitedDaemon(error, paths), paths, purpose),
paths
);
}
export function addForceStopNoProcessKilledNote(owner: string): string {
const firstLineEnd: number = owner.indexOf('\n');
if (firstLineEnd === -1) {
return `${owner} No process was killed.`;
}
return `${owner.slice(0, firstLineEnd)} No process was killed.${owner.slice(firstLineEnd)}`;
}
/**
* Explains a connection that failed while the process that the ownership record names still runs: what that
* process is doing, for example that a signal stopped it, and what to do about it.
*/
function explainLiveOwner(error: unknown, paths: IDaemonPaths, purpose: ConnectionPurpose): unknown {
if (!isUnansweredConnection(error)) return error;
const hintPurpose: DaemonOwnerHintPurpose = purpose === 'forceStop' ? 'stop' : purpose;
const owner: string | undefined = describeLiveDaemonOwner(paths, hintPurpose);
if (owner === undefined) return error;
// The note ends the first line, which says what that process is doing.
const explained: string = purpose === 'forceStop' ? addForceStopNoProcessKilledNote(owner) : owner;
return new Error(`${error.message} ${explained}`, { cause: error });
}
/** The daemon refused the connection, or accepted it but did not complete hello/ping. */
function isUnansweredConnection(error: unknown): error is Error {
if (error instanceof DaemonTransportError) {
return (
error.code === DaemonTransportErrorCode.connectionRefused ||
error.code === DaemonTransportErrorCode.connectionTimeout
);
}
return error instanceof DaemonClientError && (error.code === 'timeout' || error.code === 'disconnected');
}
/**
* An orderly shutdown removes the ownership record and the socket, and a starting daemon has a startup
* reservation. A refused connection with none of them therefore means that no daemon runs or is starting.
* Windows names a pipe, which leaves no file behind.
*/
function isEndpointAbsent(paths: IDaemonPaths): boolean {
return (
!fs.existsSync(paths.lockfilePath) &&
!inspectDaemonStartupReservation(paths) &&
(process.platform === 'win32' || !fs.existsSync(paths.socketPath))
);
}
function describeAbsentDaemon(options: IDaemonCommandOptions): string {
const repoRoot: string = path.dirname(options.rushJsonPath!);
const absent: string = `No daemon is running for ${repoRoot} (Rush ${options.rushVersion})`;
const configuration: IDaemonCommandOptions['daemonConfiguration'] = options.daemonConfiguration;
if (!configuration) return `${absent}.`;
if (!configuration.enabled) {
return `${absent}; the daemon is not enabled here, so rush-client commands run Rush in-process.`;
}
return configuration.autoStart
? `${absent}; the next rush-client command that uses the daemon starts one.`
: `${absent}, and auto-start is off, so rush-client commands run Rush in-process until "rush-client daemon start" starts one.`;
}
/**
* A client that reclaims a daemon that exited without shutting down removes the ownership record that
* {@link explainExitedDaemon} reads, so the reclaim is logged, and status reads that instead.
*/
function describeReclaimedDaemon(paths: IDaemonPaths): string {
const pid: number | undefined = findReclaimedDaemonPid(paths);
return pid === undefined
? ''
: ` The last daemon, rushd (PID ${pid}), exited without shutting down; "rush-client daemon logs" may show why.`;
}
/**
* Explains a refused connection whose ownership record names a daemon that no longer runs. An orderly
* shutdown removes the record, so that daemon exited without shutting down, for example after a crash.
*/
function explainExitedDaemon(error: unknown, paths: IDaemonPaths): unknown {
if (!(error instanceof DaemonTransportError) || error.code !== DaemonTransportErrorCode.connectionRefused) {
return error;
}
const owner: IDaemonLockfile | undefined = readDaemonLockfile(paths.lockfilePath);
if (
owner?.socketPath !== paths.socketPath ||
!Number.isSafeInteger(owner.pid) ||
owner.pid <= 0 ||
isDaemonProcessAlive(owner.pid)
) {
return error;
}
return new DaemonTransportError(
error.code,
`${error.message} rushd (PID ${owner.pid}) exited without shutting down; "rush-client daemon logs" may show why.`
);
}
/**
* Reports a startup reservation, which refuses another daemon launch until it is resolved. Clients resolve it
* once the daemon it reserved is ready, so the next command that uses, stops or restarts a ready daemon resolves
* a remaining one. Once its helper exited, a command that starts the daemon after `relaunchAfter` takes it over.
* Status only reports it.
*/
function getStartupReservationStatus(paths: IDaemonPaths): {
startupReservation?: IDaemonStartupReservationInfo;
} {
const startupReservation: IDaemonStartupReservationInfo | undefined =
inspectDaemonStartupReservation(paths);
return startupReservation ? { startupReservation } : {};
}
/** Explains that a remaining startup reservation refuses another daemon launch, and what can resolve it. */
function explainStartupReservation(error: unknown, paths: IDaemonPaths): unknown {
const reservation: IDaemonStartupReservationInfo | undefined = inspectDaemonStartupReservation(paths);
if (!reservation || !(error instanceof Error)) return error;
const helper: string = `its startup helper (PID ${reservation.helperPid})`;
let explanation: string;
switch (reservation.helperState) {
case 'running':
explanation = `A daemon is starting: ${helper} is still waiting for it to become ready; retry shortly.`;
break;
case 'exited':
explanation = `The startup reservation at ${reservation.path} remains, but ${helper} exited before the daemon became ready. A command that starts the daemon after ${reservation.relaunchAfter} takes the reservation over and launches the daemon again, provided that nothing listens at ${paths.socketPath} then. "rush-client daemon logs" may show why the daemon did not become ready.`;
break;
default:
explanation = `The startup reservation at ${reservation.path} refuses another daemon launch. Check "rush-client daemon logs"; if no daemon is starting, run "rush-client daemon stop --force" to remove it.`;
}
return new Error(`${error.message} ${explanation}`, { cause: error });
}
async function restartDaemonAsync(
client: DaemonClient,
options: IConnectOrStartDaemonOptions
): Promise<DaemonClient> {
const previousDaemon: Pick<IDaemonLockfile, 'pid' | 'startedAt'> = await requestDaemonShutdownAsync(
client,
options.paths
);
return await connectOrStartDaemonAsync({ ...options, previousDaemon });
}
function writeStatusAsync(status: object): Promise<void> {
return writeStreamAsync(process.stdout, Buffer.from(`${JSON.stringify(status)}\n`));
}
interface IForceResetResult {
readonly removedPaths: ReadonlyArray<string>;
/** Present when the reset first stopped operations that a daemon that exited had left running. */
readonly orphansReaped?: ReadonlyArray<IDaemonOrphanReap>;
}
/** Removes the leftover daemon files; the operations that it stops first are also noted on stderr. */
async function forceResetAsync(
connectionOptions: IConnectOrStartDaemonOptions,
waitTimeoutMs?: number
): Promise<IForceResetResult> {
const orphansReaped: IDaemonOrphanReap[] = [];
const { removedPaths } = await resetDaemonArtifactsAsync(connectionOptions.paths, {
waitTimeoutMs,
onOrphansReaped: (reap: IDaemonOrphanReap): void => {
orphansReaped.push(reap);
connectionOptions.onOrphansReaped?.(reap);
}
});
return orphansReaped.length > 0 ? { removedPaths, orphansReaped } : { removedPaths };
}
/** A daemon whose installation was removed or replaced still answers, but restarts on its next request. */
async function writeReadyStatusAsync(
paths: IDaemonPaths,
status: IDaemonPongMessage['payload']
): Promise<void> {
const change: IDaemonPongMessage['payload']['installationChange'] = status.installationChange;
await writeStatusAsync({
state: change ? 'installationChanged' : 'ready',
socketPath: paths.socketPath,
...status,
...getStartupReservationStatus(paths)
});
if (!change) return;
process.exitCode = 1;
await writeStreamAsync(
process.stderr,
Buffer.from(
`rush-client: The daemon's installation at ${change.folder} was ${change.change}. ` +
'The next command restarts the daemon, or run "rush-client daemon restart".\n'
)
);
}