| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {CommitInfo, SmartlogCommits} from 'isl/src/types'; |
| b69ab31 | | | 9 | import type {EjecaChildProcess, EjecaError, EjecaReturn} from 'shared/ejeca'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import os from 'node:os'; |
| b69ab31 | | | 12 | import {truncate} from 'shared/utils'; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | export function sleep(timeMs: number): Promise<void> { |
| b69ab31 | | | 15 | return new Promise(res => setTimeout(res, timeMs)); |
| b69ab31 | | | 16 | } |
| b69ab31 | | | 17 | |
| b69ab31 | | | 18 | export function firstOfIterable<T>(iterable: IterableIterator<T>): T | undefined { |
| b69ab31 | | | 19 | return iterable.next().value; |
| b69ab31 | | | 20 | } |
| b69ab31 | | | 21 | |
| b69ab31 | | | 22 | /** |
| b69ab31 | | | 23 | * Limits async function execution parallelism to only one at a time. |
| b69ab31 | | | 24 | * Hence, if a call is already running, it will wait for it to finish, |
| b69ab31 | | | 25 | * then start the next async execution, but if called again while not finished, |
| b69ab31 | | | 26 | * it will return the scheduled execution promise. |
| b69ab31 | | | 27 | * |
| b69ab31 | | | 28 | * Sample Usage: |
| b69ab31 | | | 29 | * ``` |
| b69ab31 | | | 30 | * let i = 1; |
| b69ab31 | | | 31 | * const oneExecAtATime = serializeAsyncCall(() => { |
| b69ab31 | | | 32 | * return new Promise((resolve, reject) => { |
| b69ab31 | | | 33 | * setTimeout(200, () => resolve(i++)); |
| b69ab31 | | | 34 | * }); |
| b69ab31 | | | 35 | * }); |
| b69ab31 | | | 36 | * |
| b69ab31 | | | 37 | * const result1Promise = oneExecAtATime(); // Start an async, and resolve to 1 in 200 ms. |
| b69ab31 | | | 38 | * const result2Promise = oneExecAtATime(); // Schedule the next async, and resolve to 2 in 400 ms. |
| b69ab31 | | | 39 | * const result3Promise = oneExecAtATime(); // Reuse scheduled promise and resolve to 2 in 400 ms. |
| b69ab31 | | | 40 | * ``` |
| b69ab31 | | | 41 | */ |
| b69ab31 | | | 42 | export function serializeAsyncCall<T>(asyncFun: () => Promise<T>): () => Promise<T> { |
| b69ab31 | | | 43 | let scheduledCall: Promise<T> | undefined = undefined; |
| b69ab31 | | | 44 | let pendingCall: Promise<undefined> | undefined = undefined; |
| b69ab31 | | | 45 | const startAsyncCall = () => { |
| b69ab31 | | | 46 | const resultPromise = asyncFun(); |
| b69ab31 | | | 47 | pendingCall = resultPromise.then( |
| b69ab31 | | | 48 | () => (pendingCall = undefined), |
| b69ab31 | | | 49 | () => (pendingCall = undefined), |
| b69ab31 | | | 50 | ); |
| b69ab31 | | | 51 | return resultPromise; |
| b69ab31 | | | 52 | }; |
| b69ab31 | | | 53 | const callNext = () => { |
| b69ab31 | | | 54 | scheduledCall = undefined; |
| b69ab31 | | | 55 | return startAsyncCall(); |
| b69ab31 | | | 56 | }; |
| b69ab31 | | | 57 | const scheduleNextCall = () => { |
| b69ab31 | | | 58 | if (scheduledCall == null) { |
| b69ab31 | | | 59 | if (pendingCall == null) { |
| b69ab31 | | | 60 | throw new Error('pendingCall must not be null!'); |
| b69ab31 | | | 61 | } |
| b69ab31 | | | 62 | scheduledCall = pendingCall.then(callNext, callNext); |
| b69ab31 | | | 63 | } |
| b69ab31 | | | 64 | return scheduledCall; |
| b69ab31 | | | 65 | }; |
| b69ab31 | | | 66 | return () => { |
| b69ab31 | | | 67 | if (pendingCall == null) { |
| b69ab31 | | | 68 | return startAsyncCall(); |
| b69ab31 | | | 69 | } else { |
| b69ab31 | | | 70 | return scheduleNextCall(); |
| b69ab31 | | | 71 | } |
| b69ab31 | | | 72 | }; |
| b69ab31 | | | 73 | } |
| b69ab31 | | | 74 | |
| b69ab31 | | | 75 | /** |
| b69ab31 | | | 76 | * Kill `child` on `AbortSignal`. |
| b69ab31 | | | 77 | * |
| b69ab31 | | | 78 | * This is slightly more robust than execa 6.0 and nodejs' `signal` support: |
| b69ab31 | | | 79 | * if a process was stopped (by `SIGTSTP` or `SIGSTOP`), it can still be killed. |
| b69ab31 | | | 80 | */ |
| b69ab31 | | | 81 | export function handleAbortSignalOnProcess(child: EjecaChildProcess, signal: AbortSignal) { |
| b69ab31 | | | 82 | signal.addEventListener('abort', () => { |
| b69ab31 | | | 83 | if (os.platform() == 'win32') { |
| b69ab31 | | | 84 | // Signals are ignored on Windows. |
| b69ab31 | | | 85 | // execa's default forceKillAfterTimeout behavior does not |
| b69ab31 | | | 86 | // make sense for Windows. Disable it explicitly. |
| b69ab31 | | | 87 | child.kill('SIGKILL', {forceKillAfterTimeout: false}); |
| b69ab31 | | | 88 | } else { |
| b69ab31 | | | 89 | // If the process is stopped (ex. Ctrl+Z, kill -STOP), make it |
| b69ab31 | | | 90 | // continue first so it can respond to signals including SIGKILL. |
| b69ab31 | | | 91 | child.kill('SIGCONT'); |
| b69ab31 | | | 92 | // A good citizen process should exit soon after receiving SIGTERM. |
| b69ab31 | | | 93 | // In case it doesn't, send SIGKILL after 5 seconds. |
| b69ab31 | | | 94 | child.kill('SIGTERM', {forceKillAfterTimeout: 5000}); |
| b69ab31 | | | 95 | } |
| b69ab31 | | | 96 | }); |
| b69ab31 | | | 97 | } |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | /** |
| b69ab31 | | | 100 | * Given a list of commits and a starting commit, |
| b69ab31 | | | 101 | * traverse up the chain of `parents` until we find a public commit |
| b69ab31 | | | 102 | */ |
| b69ab31 | | | 103 | export function findPublicAncestor( |
| b69ab31 | | | 104 | allCommits: SmartlogCommits | undefined, |
| b69ab31 | | | 105 | from: CommitInfo, |
| b69ab31 | | | 106 | ): CommitInfo | undefined { |
| b69ab31 | | | 107 | let publicCommit: CommitInfo | undefined; |
| b69ab31 | | | 108 | if (allCommits != null) { |
| b69ab31 | | | 109 | const map = new Map(allCommits.map(commit => [commit.hash, commit])); |
| b69ab31 | | | 110 | |
| b69ab31 | | | 111 | let current: CommitInfo | undefined = from; |
| b69ab31 | | | 112 | while (current != null) { |
| b69ab31 | | | 113 | if (current.phase === 'public') { |
| b69ab31 | | | 114 | publicCommit = current; |
| b69ab31 | | | 115 | break; |
| b69ab31 | | | 116 | } |
| b69ab31 | | | 117 | if (current.parents[0] == null) { |
| b69ab31 | | | 118 | break; |
| b69ab31 | | | 119 | } |
| b69ab31 | | | 120 | current = map.get(current.parents[0]); |
| b69ab31 | | | 121 | } |
| b69ab31 | | | 122 | } |
| b69ab31 | | | 123 | |
| b69ab31 | | | 124 | return publicCommit; |
| b69ab31 | | | 125 | } |
| b69ab31 | | | 126 | |
| b69ab31 | | | 127 | /** |
| b69ab31 | | | 128 | * Run a command that is expected to produce JSON output. |
| b69ab31 | | | 129 | * Return a JSON object. On error, the JSON object has property "error". |
| b69ab31 | | | 130 | */ |
| b69ab31 | | | 131 | export function parseExecJson<T>( |
| b69ab31 | | | 132 | exec: Promise<EjecaReturn>, |
| b69ab31 | | | 133 | reply: (parsed?: T, error?: string) => void, |
| b69ab31 | | | 134 | ) { |
| b69ab31 | | | 135 | exec |
| b69ab31 | | | 136 | .then(result => { |
| b69ab31 | | | 137 | const stdout = result.stdout; |
| b69ab31 | | | 138 | try { |
| b69ab31 | | | 139 | const parsed = JSON.parse(stdout); |
| b69ab31 | | | 140 | if (parsed.error != null) { |
| b69ab31 | | | 141 | reply(undefined, parsed.error); |
| b69ab31 | | | 142 | } else { |
| b69ab31 | | | 143 | reply(parsed as T); |
| b69ab31 | | | 144 | } |
| b69ab31 | | | 145 | } catch (err) { |
| b69ab31 | | | 146 | const msg = `Cannot parse ${truncate( |
| b69ab31 | | | 147 | result.escapedCommand, |
| b69ab31 | | | 148 | )} output. (error: ${err}, stdout: ${stdout})`; |
| b69ab31 | | | 149 | reply(undefined, msg); |
| b69ab31 | | | 150 | } |
| b69ab31 | | | 151 | }) |
| b69ab31 | | | 152 | .catch(err => { |
| b69ab31 | | | 153 | // Try extracting error from stdout '{error: message}'. |
| b69ab31 | | | 154 | try { |
| b69ab31 | | | 155 | const parsed = JSON.parse(err.stdout); |
| b69ab31 | | | 156 | if (parsed.error != null) { |
| b69ab31 | | | 157 | reply(undefined, parsed.error); |
| b69ab31 | | | 158 | return; |
| b69ab31 | | | 159 | } |
| b69ab31 | | | 160 | } catch {} |
| b69ab31 | | | 161 | // Fallback to general error. |
| b69ab31 | | | 162 | const msg = `Cannot run ${truncate(err.escapedCommand)}. (error: ${err})`; |
| b69ab31 | | | 163 | reply(undefined, msg); |
| b69ab31 | | | 164 | }); |
| b69ab31 | | | 165 | } |
| b69ab31 | | | 166 | |
| b69ab31 | | | 167 | export type EjecaSpawnError = Error & {code: string; path: string}; |
| b69ab31 | | | 168 | |
| b69ab31 | | | 169 | /** |
| b69ab31 | | | 170 | * True if an Ejeca spawned process exits non-zero or is killed. |
| b69ab31 | | | 171 | * @see {EjecaSpawnError} for when a process fails to spawn in the first place (e.g. ENOENT). |
| b69ab31 | | | 172 | */ |
| b69ab31 | | | 173 | export function isEjecaError(s: unknown): s is EjecaError { |
| b69ab31 | | | 174 | return s != null && typeof s === 'object' && 'exitCode' in s; |
| b69ab31 | | | 175 | } |
| b69ab31 | | | 176 | |
| b69ab31 | | | 177 | /** True when Ejeca fails to spawn a process, e.g. ENOENT. |
| b69ab31 | | | 178 | * (as opposed to the command spawning, then exiting non-zero) */ |
| b69ab31 | | | 179 | export function isEjecaSpawnError(s: unknown): s is EjecaSpawnError { |
| b69ab31 | | | 180 | return s != null && typeof s === 'object' && 'code' in s; |
| b69ab31 | | | 181 | } |
| b69ab31 | | | 182 | |
| b69ab31 | | | 183 | export function fromEntries<V>(entries: Array<[string, V]>): { |
| b69ab31 | | | 184 | [key: string]: V; |
| b69ab31 | | | 185 | } { |
| b69ab31 | | | 186 | // Object.fromEntries() is available in Node v12 and later. |
| b69ab31 | | | 187 | if (typeof Object.fromEntries === 'function') { |
| b69ab31 | | | 188 | return Object.fromEntries(entries); |
| b69ab31 | | | 189 | } |
| b69ab31 | | | 190 | |
| b69ab31 | | | 191 | const obj: { |
| b69ab31 | | | 192 | [key: string]: V; |
| b69ab31 | | | 193 | } = {}; |
| b69ab31 | | | 194 | for (const [key, value] of entries) { |
| b69ab31 | | | 195 | obj[key] = value; |
| b69ab31 | | | 196 | } |
| b69ab31 | | | 197 | return obj; |
| b69ab31 | | | 198 | } |