1import * as cloudrun from "@distilled.cloud/gcp/run_v2";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import { bindGcpHost } from "../Host.ts";
5import { grantFor } from "../HttpBinding.ts";
6import { RunJob, type RunJobRequest } from "./RunJob.ts";
7import type { Job } from "./Job.ts";
8
9/**
10 * HTTP implementation of {@link RunJob}.
11 *
12 * @layer
13 * @provides GCP.Run.RunJob
14 */
15export const RunJobHttp = Layer.effect(
16 RunJob,
17 Effect.gen(function* () {
18 const run = yield* cloudrun.runProjectsLocationsJobs;
19 return Effect.fn(function* <T extends Job>(job: T) {
20 const name = yield* job.name;
21 yield* bindGcpHost({
22 tag: "GCP.Run.RunJob",
23 resource: job,
24 iam: [
25 // runWithOverrides: RunJobRequest accepts `body.overrides`.
26 grantFor(
27 { role: "roles/run.jobsExecutorWithOverrides", on: "run.job" },
28 job.name,
29 ),
30 ],
31 });
32 return Effect.fn(`GCP.Run.RunJob(${job.LogicalId})`)(function* (
33 request?: RunJobRequest,
34 ) {
35 return yield* run({
36 ...request,
37 name: yield* name,
38 });
39 });
40 });
41 }),
42);