|
| 1 | +import type { ServerProvider } from "@t3tools/contracts"; |
| 2 | +import * as Clock from "effect/Clock"; |
| 3 | +import * as Duration from "effect/Duration"; |
| 4 | +import * as Effect from "effect/Effect"; |
| 5 | +import * as Layer from "effect/Layer"; |
| 6 | +import * as Option from "effect/Option"; |
| 7 | +import * as Schedule from "effect/Schedule"; |
| 8 | + |
| 9 | +import * as ServerConfig from "../config.ts"; |
| 10 | +import * as ProviderRegistry from "../provider/ProviderRegistry.ts"; |
| 11 | +import * as ProviderMaintenanceRunner from "../provider/providerMaintenanceRunner.ts"; |
| 12 | +import { forkParked } from "../serverActivation.ts"; |
| 13 | +import * as ServerSettings from "../serverSettings.ts"; |
| 14 | +import * as UpdateWindow from "./UpdateWindow.ts"; |
| 15 | + |
| 16 | +/** A failed install is retried for the same version only after this long. */ |
| 17 | +const RETRY_AFTER = Duration.hours(6); |
| 18 | + |
| 19 | +function isAutoUpdatable(provider: ServerProvider): boolean { |
| 20 | + const advisory = provider.versionAdvisory; |
| 21 | + const updateStatus = provider.updateState?.status; |
| 22 | + return ( |
| 23 | + provider.enabled && |
| 24 | + provider.installed && |
| 25 | + advisory?.status === "behind_latest" && |
| 26 | + advisory.canUpdate && |
| 27 | + advisory.latestVersion !== null && |
| 28 | + updateStatus !== "queued" && |
| 29 | + updateStatus !== "running" |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Installs provider CLI updates in the background when `automaticUpdates` is |
| 35 | + * on, one instance at a time, only while the update window is open. |
| 36 | + */ |
| 37 | +export const layer = Layer.effectDiscard( |
| 38 | + Effect.gen(function* () { |
| 39 | + // A dev server shares the host's CLIs with the real install; leave them to it. |
| 40 | + if ((yield* ServerConfig.ServerConfig).devUrl !== undefined) return; |
| 41 | + const providers = yield* ProviderRegistry.ProviderRegistry; |
| 42 | + const runner = yield* ProviderMaintenanceRunner.ProviderMaintenanceRunner; |
| 43 | + const updateWindow = yield* UpdateWindow.UpdateWindow; |
| 44 | + const serverSettings = yield* ServerSettings.ServerSettingsService; |
| 45 | + const timings = yield* UpdateWindow.updateTimings; |
| 46 | + // Keyed by instance and target version, so a newer release retries at once. |
| 47 | + const attempts = new Map<string, number>(); |
| 48 | + |
| 49 | + const pass = Effect.gen(function* () { |
| 50 | + const settings = yield* serverSettings.getSettings; |
| 51 | + if (!settings.automaticUpdates || !settings.enableProviderUpdateChecks) return; |
| 52 | + const nowMs = yield* Clock.currentTimeMillis; |
| 53 | + const candidates = (yield* providers.getProviders).filter((provider) => { |
| 54 | + if (!isAutoUpdatable(provider)) return false; |
| 55 | + const attemptedAt = attempts.get( |
| 56 | + `${provider.instanceId}@${provider.versionAdvisory?.latestVersion}`, |
| 57 | + ); |
| 58 | + return attemptedAt === undefined || nowMs - attemptedAt >= Duration.toMillis(RETRY_AFTER); |
| 59 | + }); |
| 60 | + for (const provider of candidates) { |
| 61 | + const targetVersion = provider.versionAdvisory?.latestVersion; |
| 62 | + const install = Effect.gen(function* () { |
| 63 | + attempts.set(`${provider.instanceId}@${targetVersion}`, yield* Clock.currentTimeMillis); |
| 64 | + yield* Effect.logInfo("Updating provider in the background", { |
| 65 | + instanceId: provider.instanceId, |
| 66 | + fromVersion: provider.version, |
| 67 | + targetVersion, |
| 68 | + }); |
| 69 | + yield* runner |
| 70 | + .updateProvider({ provider: provider.driver, instanceId: provider.instanceId }) |
| 71 | + .pipe( |
| 72 | + Effect.catchCause((cause) => |
| 73 | + Effect.logWarning("Background provider update failed", { |
| 74 | + instanceId: provider.instanceId, |
| 75 | + cause, |
| 76 | + }), |
| 77 | + ), |
| 78 | + ); |
| 79 | + }); |
| 80 | + // Rechecked per install: the previous one may have run for minutes. |
| 81 | + const installed = yield* updateWindow.runIfOpen({ closesWindows: false }, install); |
| 82 | + if (Option.isNone(installed)) { |
| 83 | + yield* Effect.logDebug("Provider auto-update waiting for the update window"); |
| 84 | + return; |
| 85 | + } |
| 86 | + } |
| 87 | + }).pipe( |
| 88 | + Effect.catchCause((cause) => |
| 89 | + Effect.logWarning("Provider auto-update pass failed", { cause }), |
| 90 | + ), |
| 91 | + Effect.withSpan("updates.ProviderAutoUpdater.pass"), |
| 92 | + ); |
| 93 | + |
| 94 | + yield* forkParked( |
| 95 | + Effect.sleep(timings.firstPassDelay).pipe( |
| 96 | + Effect.andThen(pass.pipe(Effect.repeat(Schedule.spaced(timings.passInterval)))), |
| 97 | + ), |
| 98 | + ); |
| 99 | + }), |
| 100 | +); |
0 commit comments