|
| 1 | +import type { Client } from "node-appwrite"; |
| 2 | +import pLimit from "p-limit"; |
| 3 | +import { type AppwriteFunction } from "appwrite-utils"; |
| 4 | +import { MessageFormatter } from "appwrite-utils-helpers"; |
| 5 | +import { |
| 6 | + finalizeFunctionDeployment, |
| 7 | + prepareFunctionDeployment, |
| 8 | + uploadFunctionDeployment, |
| 9 | +} from "./deployments.js"; |
| 10 | +import type { WaitForDeploymentOptions } from "./methods.js"; |
| 11 | + |
| 12 | +export interface BatchDeployItem { |
| 13 | + functionName: string; |
| 14 | + functionConfig: AppwriteFunction; |
| 15 | + functionPath?: string; |
| 16 | + configDirPath?: string; |
| 17 | +} |
| 18 | + |
| 19 | +export interface BatchDeployResult { |
| 20 | + functionName: string; |
| 21 | + functionId: string; |
| 22 | + status: "ready" | "failed"; |
| 23 | + deploymentId?: string; |
| 24 | + error?: Error; |
| 25 | + durationMs: number; |
| 26 | +} |
| 27 | + |
| 28 | +export interface BatchDeployOptions { |
| 29 | + buildConcurrency?: number; |
| 30 | + pollOptions?: WaitForDeploymentOptions; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Pipelined multi-function deploy: |
| 35 | + * - Uploads sequentially (clean cli-progress bar UX, no createDeployment |
| 36 | + * rate-limit churn). |
| 37 | + * - As each upload completes, its wait+activate task is enqueued on a |
| 38 | + * pLimit(N) worker pool and starts running in parallel with the next |
| 39 | + * upload. |
| 40 | + * - At the end, all pending wait+activate tasks are awaited together via |
| 41 | + * Promise.allSettled so one bad build does not abort the rest. |
| 42 | + * |
| 43 | + * Returns a per-function result array suitable for both human-readable |
| 44 | + * summary logging and machine-readable MCP responses. |
| 45 | + */ |
| 46 | +export const deployFunctionsBatch = async ( |
| 47 | + client: Client, |
| 48 | + items: BatchDeployItem[], |
| 49 | + options: BatchDeployOptions = {} |
| 50 | +): Promise<BatchDeployResult[]> => { |
| 51 | + if (items.length === 0) { |
| 52 | + return []; |
| 53 | + } |
| 54 | + |
| 55 | + const buildConcurrency = options.buildConcurrency ?? 5; |
| 56 | + const limit = pLimit(buildConcurrency); |
| 57 | + const overallStart = Date.now(); |
| 58 | + |
| 59 | + type Pending = { |
| 60 | + functionName: string; |
| 61 | + functionId: string; |
| 62 | + startedAt: number; |
| 63 | + promise: Promise<BatchDeployResult>; |
| 64 | + }; |
| 65 | + |
| 66 | + const pending: Pending[] = []; |
| 67 | + // Upload failures we surface up-front; finalize failures come from settled. |
| 68 | + const earlyFailures: BatchDeployResult[] = []; |
| 69 | + |
| 70 | + MessageFormatter.info( |
| 71 | + `Deploying ${items.length} function${items.length === 1 ? "" : "s"} (build concurrency: ${buildConcurrency})...`, |
| 72 | + { prefix: "BatchDeploy" } |
| 73 | + ); |
| 74 | + |
| 75 | + for (const item of items) { |
| 76 | + const startedAt = Date.now(); |
| 77 | + let prepared: Awaited<ReturnType<typeof prepareFunctionDeployment>>; |
| 78 | + let deploymentId: string; |
| 79 | + |
| 80 | + try { |
| 81 | + MessageFormatter.progress( |
| 82 | + `[${item.functionName}] Preparing and uploading...`, |
| 83 | + { prefix: "BatchDeploy" } |
| 84 | + ); |
| 85 | + prepared = await prepareFunctionDeployment( |
| 86 | + client, |
| 87 | + item.functionName, |
| 88 | + item.functionConfig, |
| 89 | + item.functionPath, |
| 90 | + item.configDirPath |
| 91 | + ); |
| 92 | + const uploaded = await uploadFunctionDeployment( |
| 93 | + client, |
| 94 | + prepared.functionId, |
| 95 | + prepared.deployPath, |
| 96 | + // Pass activate=true so Appwrite can auto-activate as soon as the |
| 97 | + // build finishes; finalizeFunctionDeployment also calls activate |
| 98 | + // explicitly afterwards to guarantee the final state. |
| 99 | + true, |
| 100 | + prepared.entrypoint, |
| 101 | + prepared.commands, |
| 102 | + prepared.ignored |
| 103 | + ); |
| 104 | + deploymentId = uploaded.$id; |
| 105 | + MessageFormatter.success( |
| 106 | + `[${item.functionName}] Upload complete (deployment ${deploymentId}); build queued.`, |
| 107 | + { prefix: "BatchDeploy" } |
| 108 | + ); |
| 109 | + } catch (error) { |
| 110 | + const err = error instanceof Error ? error : new Error(String(error)); |
| 111 | + MessageFormatter.error( |
| 112 | + `[${item.functionName}] Upload failed`, |
| 113 | + err, |
| 114 | + { prefix: "BatchDeploy" } |
| 115 | + ); |
| 116 | + earlyFailures.push({ |
| 117 | + functionName: item.functionName, |
| 118 | + functionId: item.functionConfig.$id, |
| 119 | + status: "failed", |
| 120 | + error: err, |
| 121 | + durationMs: Date.now() - startedAt, |
| 122 | + }); |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + const functionName = item.functionName; |
| 127 | + const functionId = prepared.functionId; |
| 128 | + const taskStartedAt = startedAt; |
| 129 | + |
| 130 | + const promise = limit(async (): Promise<BatchDeployResult> => { |
| 131 | + try { |
| 132 | + const ready = await finalizeFunctionDeployment( |
| 133 | + client, |
| 134 | + functionId, |
| 135 | + deploymentId, |
| 136 | + options.pollOptions |
| 137 | + ); |
| 138 | + return { |
| 139 | + functionName, |
| 140 | + functionId, |
| 141 | + status: "ready", |
| 142 | + deploymentId: ready.$id, |
| 143 | + durationMs: Date.now() - taskStartedAt, |
| 144 | + }; |
| 145 | + } catch (error) { |
| 146 | + const err = error instanceof Error ? error : new Error(String(error)); |
| 147 | + MessageFormatter.error( |
| 148 | + `[${functionName}] Build/activation failed`, |
| 149 | + err, |
| 150 | + { prefix: "BatchDeploy" } |
| 151 | + ); |
| 152 | + return { |
| 153 | + functionName, |
| 154 | + functionId, |
| 155 | + status: "failed", |
| 156 | + deploymentId, |
| 157 | + error: err, |
| 158 | + durationMs: Date.now() - taskStartedAt, |
| 159 | + }; |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + pending.push({ functionName, functionId, startedAt: taskStartedAt, promise }); |
| 164 | + } |
| 165 | + |
| 166 | + // All uploads done; await the finalize tasks. Use allSettled even though |
| 167 | + // the inner promises swallow errors, so we never bubble an unexpected |
| 168 | + // rejection. |
| 169 | + const settled = await Promise.allSettled(pending.map((p) => p.promise)); |
| 170 | + |
| 171 | + const finalizeResults: BatchDeployResult[] = settled.map((s, idx) => { |
| 172 | + const slot = pending[idx]; |
| 173 | + if (s.status === "fulfilled") { |
| 174 | + return s.value; |
| 175 | + } |
| 176 | + const err = s.reason instanceof Error ? s.reason : new Error(String(s.reason)); |
| 177 | + return { |
| 178 | + functionName: slot.functionName, |
| 179 | + functionId: slot.functionId, |
| 180 | + status: "failed", |
| 181 | + error: err, |
| 182 | + durationMs: Date.now() - slot.startedAt, |
| 183 | + }; |
| 184 | + }); |
| 185 | + |
| 186 | + const results: BatchDeployResult[] = [...earlyFailures, ...finalizeResults]; |
| 187 | + |
| 188 | + const readyCount = results.filter((r) => r.status === "ready").length; |
| 189 | + const failedCount = results.length - readyCount; |
| 190 | + const totalMs = Date.now() - overallStart; |
| 191 | + |
| 192 | + MessageFormatter.info( |
| 193 | + `Batch deploy summary: ${readyCount} ready, ${failedCount} failed in ${(totalMs / 1000).toFixed(1)}s`, |
| 194 | + { prefix: "BatchDeploy" } |
| 195 | + ); |
| 196 | + |
| 197 | + for (const r of results) { |
| 198 | + if (r.status === "ready") { |
| 199 | + MessageFormatter.success( |
| 200 | + ` ✔ ${r.functionName} (${(r.durationMs / 1000).toFixed(1)}s) → ${r.deploymentId}`, |
| 201 | + { prefix: "BatchDeploy" } |
| 202 | + ); |
| 203 | + } else { |
| 204 | + const msg = r.error?.message ?? "Unknown error"; |
| 205 | + const tail = msg.length > 500 ? msg.slice(-500) : msg; |
| 206 | + MessageFormatter.error( |
| 207 | + ` ✘ ${r.functionName} (${(r.durationMs / 1000).toFixed(1)}s) — ${tail}`, |
| 208 | + undefined, |
| 209 | + { prefix: "BatchDeploy" } |
| 210 | + ); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + return results; |
| 215 | +}; |
0 commit comments