|
| 1 | +import { gte, valid } from 'semver'; |
| 2 | + |
| 3 | +import { getUpdateInstallLogFile } from '#/utils/paths'; |
| 4 | + |
| 5 | +import { |
| 6 | + activateHomebrewUpdate, |
| 7 | + PreparedHomebrewUpdateInvalidError, |
| 8 | +} from './homebrew'; |
| 9 | +import { formatErrorMessage } from './format-error'; |
| 10 | +import { tryAcquireUpdateInstallLock, type UpdateInstallLockHandle } from './install-lock'; |
| 11 | +import { readUpdateInstallState, writeUpdateInstallState } from './install-state'; |
| 12 | +import { detectInstallSource } from './source'; |
| 13 | +import type { InstallSource, UpdateInstallState, UpdatePreparedHomebrew } from './types'; |
| 14 | + |
| 15 | +const ACTIVATION_FAILURE_LIMIT = 2; |
| 16 | + |
| 17 | +export interface ActivatePendingUpdateDeps { |
| 18 | + readonly readState: () => Promise<UpdateInstallState>; |
| 19 | + readonly writeState: (state: UpdateInstallState) => Promise<void>; |
| 20 | + readonly acquireLock: ( |
| 21 | + request: { readonly version: string }, |
| 22 | + ) => Promise<UpdateInstallLockHandle | null>; |
| 23 | + readonly activateHomebrew: ( |
| 24 | + prepared: UpdatePreparedHomebrew, |
| 25 | + ) => Promise<{ readonly version: string; readonly executable: string }>; |
| 26 | + readonly detectSource: () => Promise<InstallSource>; |
| 27 | + readonly now: () => Date; |
| 28 | + readonly pid: number; |
| 29 | +} |
| 30 | + |
| 31 | +export interface ActivatePendingUpdateOptions { |
| 32 | + readonly enabled: boolean; |
| 33 | + readonly automaticEnabled: boolean; |
| 34 | + readonly deps?: Partial<ActivatePendingUpdateDeps>; |
| 35 | +} |
| 36 | + |
| 37 | +function resolveDeps(overrides: Partial<ActivatePendingUpdateDeps> = {}): ActivatePendingUpdateDeps { |
| 38 | + return { |
| 39 | + readState: overrides.readState ?? (() => readUpdateInstallState()), |
| 40 | + writeState: overrides.writeState ?? ((state) => writeUpdateInstallState(state)), |
| 41 | + acquireLock: overrides.acquireLock ?? ((request) => tryAcquireUpdateInstallLock(request)), |
| 42 | + activateHomebrew: |
| 43 | + overrides.activateHomebrew ?? |
| 44 | + ((prepared) => activateHomebrewUpdate(prepared, { logFile: getUpdateInstallLogFile() })), |
| 45 | + detectSource: overrides.detectSource ?? (() => detectInstallSource()), |
| 46 | + now: overrides.now ?? (() => new Date()), |
| 47 | + pid: overrides.pid ?? process.pid, |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +function activationAttempts(state: UpdateInstallState, version: string): number { |
| 52 | + const failure = state.lastFailure; |
| 53 | + return failure?.version === version && failure.operation === 'activate' ? failure.attempts : 0; |
| 54 | +} |
| 55 | + |
| 56 | +function isRunningPreparedVersion(currentVersion: string, preparedVersion: string): boolean { |
| 57 | + return ( |
| 58 | + valid(currentVersion) !== null && |
| 59 | + valid(preparedVersion) !== null && |
| 60 | + gte(currentVersion, preparedVersion) |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +export async function activatePendingUpdate( |
| 65 | + currentVersion: string, |
| 66 | + options: ActivatePendingUpdateOptions, |
| 67 | +) { |
| 68 | + if (!options.enabled) return { status: 'none' as const }; |
| 69 | + const deps = resolveDeps(options.deps); |
| 70 | + let state = await deps.readState(); |
| 71 | + const pending = state.pending; |
| 72 | + if (pending === null) return { status: 'none' as const }; |
| 73 | + if (pending.requestedBy === 'automatic' && !options.automaticEnabled) { |
| 74 | + return { status: 'none' as const }; |
| 75 | + } |
| 76 | + |
| 77 | + if (await deps.detectSource() !== pending.source) { |
| 78 | + await deps.writeState({ ...state, active: null, pending: null }); |
| 79 | + return { status: 'invalidated' as const, version: pending.version }; |
| 80 | + } |
| 81 | + |
| 82 | + if (isRunningPreparedVersion(currentVersion, pending.version)) { |
| 83 | + const installedAt = deps.now().toISOString(); |
| 84 | + await deps.writeState({ |
| 85 | + active: null, |
| 86 | + pending: null, |
| 87 | + lastFailure: null, |
| 88 | + lastSuccess: { |
| 89 | + version: currentVersion, |
| 90 | + installedAt, |
| 91 | + notifiedAt: null, |
| 92 | + }, |
| 93 | + }); |
| 94 | + return { status: 'finalized' as const, version: currentVersion }; |
| 95 | + } |
| 96 | + |
| 97 | + if (activationAttempts(state, pending.version) >= ACTIVATION_FAILURE_LIMIT) { |
| 98 | + // Terminal: drop the pending record (keeping lastFailure for preflight) |
| 99 | + // so later launches stop retrying and reporting an in-progress update. |
| 100 | + await deps.writeState({ ...state, pending: null }); |
| 101 | + return { |
| 102 | + status: 'failed' as const, |
| 103 | + version: pending.version, |
| 104 | + message: `Automatic activation failed ${String(ACTIVATION_FAILURE_LIMIT)} times`, |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + const lock = await deps.acquireLock({ version: pending.version }); |
| 109 | + if (lock === null) return { status: 'in-progress' as const, version: pending.version }; |
| 110 | + |
| 111 | + try { |
| 112 | + state = await deps.readState(); |
| 113 | + if (state.pending?.jobId !== pending.jobId) return { status: 'none' as const }; |
| 114 | + const startedAt = deps.now().toISOString(); |
| 115 | + const activatingState: UpdateInstallState = { |
| 116 | + ...state, |
| 117 | + active: { |
| 118 | + version: pending.version, |
| 119 | + source: pending.source, |
| 120 | + operation: 'activate', |
| 121 | + jobId: pending.jobId, |
| 122 | + startedAt, |
| 123 | + pid: deps.pid, |
| 124 | + }, |
| 125 | + }; |
| 126 | + await deps.writeState(activatingState); |
| 127 | + |
| 128 | + try { |
| 129 | + const activated = await deps.activateHomebrew(pending); |
| 130 | + await deps.writeState({ |
| 131 | + ...activatingState, |
| 132 | + active: null, |
| 133 | + lastFailure: null, |
| 134 | + }); |
| 135 | + return { |
| 136 | + status: 'activated' as const, |
| 137 | + version: activated.version, |
| 138 | + executable: activated.executable, |
| 139 | + }; |
| 140 | + } catch (error) { |
| 141 | + const message = formatErrorMessage(error); |
| 142 | + if (error instanceof PreparedHomebrewUpdateInvalidError) { |
| 143 | + // Carry the cumulative prepare-failure count so repeated invalid |
| 144 | + // artifacts can reach the auto-install failure threshold. |
| 145 | + const priorFailure = activatingState.lastFailure; |
| 146 | + const prepareAttempts = |
| 147 | + priorFailure?.version === pending.version && priorFailure.operation === 'prepare' |
| 148 | + ? priorFailure.attempts + 1 |
| 149 | + : 1; |
| 150 | + await deps.writeState({ |
| 151 | + ...activatingState, |
| 152 | + active: null, |
| 153 | + pending: null, |
| 154 | + lastFailure: { |
| 155 | + version: pending.version, |
| 156 | + failedAt: deps.now().toISOString(), |
| 157 | + attempts: prepareAttempts, |
| 158 | + operation: 'prepare', |
| 159 | + message, |
| 160 | + }, |
| 161 | + }); |
| 162 | + return { status: 'invalidated' as const, version: pending.version }; |
| 163 | + } |
| 164 | + const attempts = activationAttempts(activatingState, pending.version) + 1; |
| 165 | + await deps.writeState({ |
| 166 | + ...activatingState, |
| 167 | + active: null, |
| 168 | + lastFailure: { |
| 169 | + version: pending.version, |
| 170 | + failedAt: deps.now().toISOString(), |
| 171 | + attempts, |
| 172 | + operation: 'activate', |
| 173 | + message, |
| 174 | + }, |
| 175 | + }); |
| 176 | + return { status: 'failed' as const, version: pending.version, message }; |
| 177 | + } |
| 178 | + } finally { |
| 179 | + await lock.release().catch(() => {}); |
| 180 | + } |
| 181 | +} |
0 commit comments