1import * as ml from "@distilled.cloud/gcp/ml_v1";
2import * as Effect from "effect/Effect";
3import { waitForOperation as waitForGcpOperation } from "../Operation.ts";
4
5const getOperation = (name: string) =>
6 name.includes("/locations/")
7 ? ml.getProjectsLocationsOperations({ name })
8 : ml.getProjectsOperations({ name });
9
10/**
11 * Wait for an AI Platform (ML Engine) operation. Version deploys take
12 * several minutes. An operation that finished with `ALREADY_EXISTS` (6) is
13 * a lost create race; `notFoundOk` also accepts `NOT_FOUND` (5).
14 */
15export const waitForOperation = (
16 operation: ml.GoogleLongrunning__Operation,
17 options?: { notFoundOk?: boolean },
18) =>
19 waitForGcpOperation(
20 operation,
21 (name) =>
22 getOperation(name).pipe(
23 Effect.catchTag("NotFound", (error) =>
24 options?.notFoundOk === true
25 ? Effect.succeed<ml.GoogleLongrunning__Operation>({
26 name,
27 done: true,
28 })
29 : Effect.fail(error),
30 ),
31 ),
32 { budget: "20 minutes" },
33 ).pipe(
34 Effect.catchIf(
35 (error) =>
36 error._tag === "GCP.OperationFailed" &&
37 (error.code === 6 ||
38 (options?.notFoundOk === true && error.code === 5)),
39 () => Effect.void,
40 ),
41 Effect.asVoid,
42 );