|
| 1 | +export type BootstrapDownloadTarget = "desktop" | "mobile"; |
| 2 | + |
| 3 | +export interface BootstrapDownloadEnv { |
| 4 | + QVAC_E2E_DOWNLOAD_CONCURRENCY?: string; |
| 5 | +} |
| 6 | + |
| 7 | +export interface BootstrapDownloadItem { |
| 8 | + id: string; |
| 9 | + name: string; |
| 10 | + ownerLabel: string; |
| 11 | + run: () => Promise<void>; |
| 12 | +} |
| 13 | + |
| 14 | +export interface BootstrapDownloadOptions { |
| 15 | + concurrency: number; |
| 16 | + retryConcurrency: number; |
| 17 | + log?: (message: string) => void; |
| 18 | +} |
| 19 | + |
| 20 | +export interface BootstrapDownloadResult { |
| 21 | + maxConcurrent: number; |
| 22 | +} |
| 23 | + |
| 24 | +interface FailedDownload { |
| 25 | + item: BootstrapDownloadItem; |
| 26 | + reason: unknown; |
| 27 | +} |
| 28 | + |
| 29 | +interface QueueState { |
| 30 | + nextIndex: number; |
| 31 | +} |
| 32 | + |
| 33 | +const DEFAULT_DOWNLOAD_CONCURRENCY: Record<BootstrapDownloadTarget, number> = { |
| 34 | + desktop: 6, |
| 35 | + mobile: 4, |
| 36 | +}; |
| 37 | + |
| 38 | +function positiveIntegerOrNull(value: string | undefined): number | null { |
| 39 | + if (!value) return null; |
| 40 | + const parsed = Number(value); |
| 41 | + if (!Number.isInteger(parsed) || parsed < 1) return null; |
| 42 | + return parsed; |
| 43 | +} |
| 44 | + |
| 45 | +function normalizeConcurrency(value: number, fallback: number) { |
| 46 | + if (!Number.isInteger(value) || value < 1) return fallback; |
| 47 | + return value; |
| 48 | +} |
| 49 | + |
| 50 | +export function resolveBootstrapDownloadConcurrency( |
| 51 | + env: BootstrapDownloadEnv = {}, |
| 52 | + target: BootstrapDownloadTarget = "desktop", |
| 53 | +) { |
| 54 | + return ( |
| 55 | + positiveIntegerOrNull(env.QVAC_E2E_DOWNLOAD_CONCURRENCY) ?? |
| 56 | + DEFAULT_DOWNLOAD_CONCURRENCY[target] |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +export function resolveBootstrapRetryConcurrency(concurrency: number) { |
| 61 | + return Math.min(2, Math.max(1, concurrency)); |
| 62 | +} |
| 63 | + |
| 64 | +async function runQueueWorker<T>( |
| 65 | + items: readonly T[], |
| 66 | + results: PromiseSettledResult<void>[], |
| 67 | + state: QueueState, |
| 68 | + worker: (item: T) => Promise<void>, |
| 69 | +) { |
| 70 | + while (state.nextIndex < items.length) { |
| 71 | + const index = state.nextIndex; |
| 72 | + state.nextIndex++; |
| 73 | + try { |
| 74 | + await worker(items[index]); |
| 75 | + results[index] = { status: "fulfilled", value: undefined }; |
| 76 | + } catch (reason) { |
| 77 | + results[index] = { status: "rejected", reason }; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +async function mapSettledWithConcurrency<T>( |
| 83 | + items: readonly T[], |
| 84 | + concurrency: number, |
| 85 | + worker: (item: T) => Promise<void>, |
| 86 | +) { |
| 87 | + const results = new Array<PromiseSettledResult<void>>(items.length); |
| 88 | + const state: QueueState = { nextIndex: 0 }; |
| 89 | + |
| 90 | + const workerCount = Math.min(concurrency, items.length); |
| 91 | + await Promise.all( |
| 92 | + Array.from({ length: workerCount }, () => |
| 93 | + runQueueWorker(items, results, state, worker), |
| 94 | + ), |
| 95 | + ); |
| 96 | + return results; |
| 97 | +} |
| 98 | + |
| 99 | +function collectFailures( |
| 100 | + items: readonly BootstrapDownloadItem[], |
| 101 | + results: readonly PromiseSettledResult<void>[], |
| 102 | +) { |
| 103 | + const failed: FailedDownload[] = []; |
| 104 | + for (let i = 0; i < results.length; i++) { |
| 105 | + const result = results[i]; |
| 106 | + if (result.status === "rejected") { |
| 107 | + failed.push({ item: items[i], reason: result.reason }); |
| 108 | + } |
| 109 | + } |
| 110 | + return failed; |
| 111 | +} |
| 112 | + |
| 113 | +function formatReason(reason: unknown) { |
| 114 | + if (reason instanceof Error) return reason.message; |
| 115 | + return String(reason); |
| 116 | +} |
| 117 | + |
| 118 | +export async function runBootstrapDownloads( |
| 119 | + items: readonly BootstrapDownloadItem[], |
| 120 | + options: BootstrapDownloadOptions, |
| 121 | +) { |
| 122 | + const concurrency = normalizeConcurrency( |
| 123 | + options.concurrency, |
| 124 | + DEFAULT_DOWNLOAD_CONCURRENCY.desktop, |
| 125 | + ); |
| 126 | + const retryConcurrency = normalizeConcurrency( |
| 127 | + options.retryConcurrency, |
| 128 | + resolveBootstrapRetryConcurrency(concurrency), |
| 129 | + ); |
| 130 | + const log = options.log; |
| 131 | + let active = 0; |
| 132 | + let maxConcurrent = 0; |
| 133 | + let leftToCheck = items.length; |
| 134 | + let parallelDetected = false; |
| 135 | + |
| 136 | + async function runItem(item: BootstrapDownloadItem, retry: boolean) { |
| 137 | + const prefix = retry ? "🔁 retry" : "📥"; |
| 138 | + log?.(`${prefix} ${item.name} (used by: ${item.ownerLabel})...`); |
| 139 | + active++; |
| 140 | + maxConcurrent = Math.max(maxConcurrent, active); |
| 141 | + if (!parallelDetected && active >= 2) { |
| 142 | + parallelDetected = true; |
| 143 | + log?.(`🔀 Parallel downloads confirmed (active: ${active})`); |
| 144 | + } |
| 145 | + try { |
| 146 | + await item.run(); |
| 147 | + leftToCheck--; |
| 148 | + log?.(`✅ ${item.name} cached - still processing: ${leftToCheck}`); |
| 149 | + } finally { |
| 150 | + active--; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + const firstPassResults = await mapSettledWithConcurrency( |
| 155 | + items, |
| 156 | + concurrency, |
| 157 | + (item) => runItem(item, false), |
| 158 | + ); |
| 159 | + const firstPassFailed = collectFailures(items, firstPassResults); |
| 160 | + if (firstPassFailed.length === 0) return { maxConcurrent }; |
| 161 | + |
| 162 | + for (const failure of firstPassFailed) { |
| 163 | + log?.( |
| 164 | + `❌ download failed: ${failure.item.name}: ${formatReason(failure.reason)}`, |
| 165 | + ); |
| 166 | + } |
| 167 | + |
| 168 | + log?.( |
| 169 | + `🔁 Retrying ${firstPassFailed.length} failed download(s) with concurrency ${retryConcurrency}`, |
| 170 | + ); |
| 171 | + |
| 172 | + const retryItems = firstPassFailed.map((failure) => failure.item); |
| 173 | + const retryResults = await mapSettledWithConcurrency( |
| 174 | + retryItems, |
| 175 | + retryConcurrency, |
| 176 | + (item) => runItem(item, true), |
| 177 | + ); |
| 178 | + const finalFailed = collectFailures(retryItems, retryResults); |
| 179 | + if (finalFailed.length > 0) { |
| 180 | + for (const failure of finalFailed) { |
| 181 | + log?.( |
| 182 | + `❌ retry failed: ${failure.item.name}: ${formatReason(failure.reason)}`, |
| 183 | + ); |
| 184 | + } |
| 185 | + throw new Error( |
| 186 | + `${finalFailed.length}/${items.length} downloads failed after retry pass`, |
| 187 | + ); |
| 188 | + } |
| 189 | + |
| 190 | + return { maxConcurrent }; |
| 191 | +} |
0 commit comments