1import * as looker from "@distilled.cloud/gcp/looker_v1";
2import * as Effect from "effect/Effect";
3import { waitForOperation as waitForGcpOperation } from "../Operation.ts";
4
5/**
6 * Wait for a Looker operation; instance backups take several minutes.
7 * ALREADY_EXISTS (code 6) counts as success (create race); with
8 * `notFoundOk`, so does NOT_FOUND (code 5, delete race) and an operation
9 * that is already gone.
10 */
11export const waitForOperation = (
12 operation: looker.Operation,
13 options?: { notFoundOk?: boolean },
14) =>
15 waitForGcpOperation(
16 operation,
17 (name) => looker.getProjectsLocationsOperations({ name }),
18 { budget: "30 minutes", interval: "10 seconds" },
19 ).pipe(
20 Effect.catchIf(
21 (error) =>
22 (error._tag === "GCP.OperationFailed" &&
23 (error.code === 6 ||
24 (options?.notFoundOk === true && error.code === 5))) ||
25 (options?.notFoundOk === true && error._tag === "NotFound"),
26 () => Effect.succeed(operation),
27 ),
28 );