|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import * as fs from 'node:fs'; |
| 5 | +import * as os from 'node:os'; |
| 6 | +import * as path from 'node:path'; |
| 7 | + |
| 8 | +import type { IReporterEventEnvelope } from '../events/IReporterEventEnvelope'; |
| 9 | +import type { IReporterEventSink } from '../producers/IReporterEventSink'; |
| 10 | +import { ReporterManager } from '../manager/ReporterManager'; |
| 11 | +import { RUSH_REPORTER_BOOTSTRAP_HANDOFF_ENV_VAR } from '../bootstrap/BootstrapProtocol'; |
| 12 | +import { |
| 13 | + readBootstrapHandoffFileAsync, |
| 14 | + deleteBootstrapHandoffFileAsync, |
| 15 | + isBootstrapHandoffFileName |
| 16 | +} from '../bootstrap/BootstrapHandoff'; |
| 17 | + |
| 18 | +/** |
| 19 | + * The default retention window for abandoned handoff files (14 days), in milliseconds. |
| 20 | + * |
| 21 | + * @beta |
| 22 | + */ |
| 23 | +export const DEFAULT_HANDOFF_RETENTION_MS: number = 14 * 24 * 60 * 60 * 1000; |
| 24 | + |
| 25 | +/** |
| 26 | + * Options for constructing a {@link ReporterHost}. |
| 27 | + * |
| 28 | + * @beta |
| 29 | + */ |
| 30 | +export interface IReporterHostOptions { |
| 31 | + /** |
| 32 | + * The manager the host owns. A new {@link ReporterManager} is created when omitted. |
| 33 | + */ |
| 34 | + readonly manager?: ReporterManager; |
| 35 | + |
| 36 | + /** |
| 37 | + * The environment variables consulted for the bootstrap handoff path. Defaults |
| 38 | + * to `process.env`. |
| 39 | + */ |
| 40 | + readonly env?: Record<string, string | undefined>; |
| 41 | + |
| 42 | + /** |
| 43 | + * The directory scanned for abandoned handoff files. Defaults to the OS temp folder. |
| 44 | + */ |
| 45 | + readonly handoffDirectory?: string; |
| 46 | + |
| 47 | + /** |
| 48 | + * The retention window for abandoned handoff files. Defaults to 14 days. |
| 49 | + */ |
| 50 | + readonly retentionMs?: number; |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns the current time in milliseconds. Injectable for testing. |
| 54 | + */ |
| 55 | + readonly nowMs?: () => number; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * The outcome of replaying the bootstrap handoff. |
| 60 | + * |
| 61 | + * @beta |
| 62 | + */ |
| 63 | +export interface IBootstrapReplayResult { |
| 64 | + /** |
| 65 | + * Whether this was a direct invocation with no handoff file to replay. |
| 66 | + */ |
| 67 | + readonly direct: boolean; |
| 68 | + |
| 69 | + /** |
| 70 | + * Whether handoff events were replayed. |
| 71 | + */ |
| 72 | + readonly replayed: boolean; |
| 73 | + |
| 74 | + /** |
| 75 | + * The number of events replayed. |
| 76 | + */ |
| 77 | + readonly eventCount: number; |
| 78 | + |
| 79 | + /** |
| 80 | + * The handoff file path, when one was present. |
| 81 | + */ |
| 82 | + readonly handoffPath?: string; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Hosts the authoritative {@link ReporterManager} in the frontend, before Rush |
| 87 | + * version selection. |
| 88 | + * |
| 89 | + * @remarks |
| 90 | + * The frontend creates the host, registers reporters, replays the bootstrap |
| 91 | + * handoff, and hands the selected `rush-lib` a typed {@link IReporterEventSink}. |
| 92 | + * `rush-lib` receives only the sink, so it can emit events but cannot select |
| 93 | + * reporters or own the session. |
| 94 | + * |
| 95 | + * @beta |
| 96 | + */ |
| 97 | +export class ReporterHost { |
| 98 | + private readonly _manager: ReporterManager; |
| 99 | + private readonly _env: Record<string, string | undefined>; |
| 100 | + private readonly _handoffDirectory: string; |
| 101 | + private readonly _retentionMs: number; |
| 102 | + private readonly _nowMs: () => number; |
| 103 | + |
| 104 | + public constructor(options: IReporterHostOptions = {}) { |
| 105 | + this._manager = options.manager ?? new ReporterManager(); |
| 106 | + this._env = options.env ?? process.env; |
| 107 | + this._handoffDirectory = options.handoffDirectory ?? os.tmpdir(); |
| 108 | + this._retentionMs = options.retentionMs ?? DEFAULT_HANDOFF_RETENTION_MS; |
| 109 | + this._nowMs = options.nowMs ?? (() => Date.now()); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * The manager the host owns, used by the frontend to register reporters. |
| 114 | + */ |
| 115 | + public get manager(): ReporterManager { |
| 116 | + return this._manager; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Returns the typed sink handed to the selected `rush-lib`. |
| 121 | + * |
| 122 | + * @remarks |
| 123 | + * The return type is narrowed to {@link IReporterEventSink} so the engine |
| 124 | + * cannot register reporters, flush, or otherwise own selection. |
| 125 | + */ |
| 126 | + public getSink(): IReporterEventSink { |
| 127 | + return this._manager; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Replays the bootstrap handoff file into the manager and deletes it. |
| 132 | + * |
| 133 | + * @remarks |
| 134 | + * When the private handoff environment variable is absent, this was a direct |
| 135 | + * `rush` invocation and there is nothing to replay. A missing or unreadable |
| 136 | + * handoff file is tolerated: the frontend continues without replay. |
| 137 | + */ |
| 138 | + public async replayBootstrapHandoffAsync(): Promise<IBootstrapReplayResult> { |
| 139 | + const handoffPath: string | undefined = this._env[RUSH_REPORTER_BOOTSTRAP_HANDOFF_ENV_VAR]; |
| 140 | + if (!handoffPath) { |
| 141 | + return { direct: true, replayed: false, eventCount: 0 }; |
| 142 | + } |
| 143 | + |
| 144 | + let events: unknown[]; |
| 145 | + try { |
| 146 | + events = await readBootstrapHandoffFileAsync(handoffPath); |
| 147 | + } catch { |
| 148 | + // The handoff file is missing or corrupt; continue without replay. |
| 149 | + await deleteBootstrapHandoffFileAsync(handoffPath); |
| 150 | + return { direct: false, replayed: false, eventCount: 0, handoffPath }; |
| 151 | + } |
| 152 | + |
| 153 | + for (const event of events) { |
| 154 | + this._manager.ingestForeignEnvelope(event as IReporterEventEnvelope<unknown>); |
| 155 | + } |
| 156 | + await deleteBootstrapHandoffFileAsync(handoffPath); |
| 157 | + return { direct: false, replayed: true, eventCount: events.length, handoffPath }; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Deletes abandoned handoff files older than the retention window. |
| 162 | + * |
| 163 | + * @returns the paths of the deleted files |
| 164 | + */ |
| 165 | + public async cleanAbandonedHandoffFilesAsync(): Promise<string[]> { |
| 166 | + const deleted: string[] = []; |
| 167 | + let fileNames: string[]; |
| 168 | + try { |
| 169 | + fileNames = await fs.promises.readdir(this._handoffDirectory); |
| 170 | + } catch { |
| 171 | + return deleted; |
| 172 | + } |
| 173 | + |
| 174 | + const cutoff: number = this._nowMs() - this._retentionMs; |
| 175 | + for (const fileName of fileNames) { |
| 176 | + if (!isBootstrapHandoffFileName(fileName)) { |
| 177 | + continue; |
| 178 | + } |
| 179 | + const filePath: string = path.join(this._handoffDirectory, fileName); |
| 180 | + try { |
| 181 | + const stats: fs.Stats = await fs.promises.stat(filePath); |
| 182 | + if (stats.mtimeMs < cutoff) { |
| 183 | + await fs.promises.rm(filePath, { force: true }); |
| 184 | + deleted.push(filePath); |
| 185 | + } |
| 186 | + } catch { |
| 187 | + // Ignore files that vanish or cannot be inspected. |
| 188 | + } |
| 189 | + } |
| 190 | + return deleted; |
| 191 | + } |
| 192 | +} |
0 commit comments