@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 976 lines 32.5 KB
Raw
1 import { Credentials } from "@distilled.cloud/gcp/Credentials";
2 import * as cloudrun from "@distilled.cloud/gcp/run_v2";
3 import { Unowned } from "alchemy/AdoptPolicy";
4 import type * as Bundle from "alchemy/Bundle/Bundle";
5 import { isResolved } from "alchemy/Diff";
6 import { createPhysicalName } from "alchemy/PhysicalName";
7 import { Platform, type Main, type PlatformProps, type PlatformServices } from "alchemy/Platform";
8 import * as Provider from "alchemy/Provider";
9 import { Resource, type ResourceBinding } from "alchemy/Resource";
10 import {
11 createContainerRuntimeContext,
12 type HostRuntimeContext,
13 type ServerHost,
14 } from "alchemy/Runtime/Process";
15 import type { RuntimeContext } from "alchemy/RuntimeContext";
16 import { tagRecord } from "alchemy/Tags";
17 import * as Data from "effect/Data";
18 import * as Effect from "effect/Effect";
19 import * as Schedule from "effect/Schedule";
20 import type { Scope } from "effect/Scope";
21 import * as Stream from "effect/Stream";
22 import { destroyHostImageRepository, makeImageSource } from "../ArtifactRegistry/ImageSource.ts";
23 import { GcpEnvironment } from "../Environment.ts";
24 import { retryActAs, type AppliedIamGrant, type GcpHostBinding } from "../Host.ts";
25 import {
26 alchemyRuntimeEnv,
27 isManagedServiceAccount,
28 makeGcpBootstrap,
29 mergeContainerEnv,
30 releaseHostIdentity,
31 resolveHostIdentity,
32 } from "../HostRuntime.ts";
33 import {
34 createInternalLabels,
35 diffLabels,
36 hasAlchemyLabels,
37 stripInternalLabels,
38 toLabels,
39 } from "../Labels.ts";
40 import {
41 type LongRunningOperation,
42 waitForOperation as waitForLongRunningOperation,
43 } from "../Operation.ts";
44 import type { Providers } from "../Providers.ts";
45
46 const MAX_NAME_LENGTH = 49;
47
48 export type JobEnvVar = {
49 /** Environment variable name. */
50 name: string;
51 /** Literal value. Mutually exclusive with `valueSource`. */
52 value?: string;
53 /** Value sourced from Secret Manager. */
54 valueSource?: {
55 secretKeyRef?: {
56 /** Secret id or `projects/{project}/secrets/{secret}` name. */
57 secret: string;
58 /** Version (`latest`, integer, or alias). */
59 version?: string;
60 };
61 };
62 };
63
64 export type JobContainer = {
65 /** DNS_LABEL container name. */
66 name?: string;
67 /**
68 * Container image in Artifact Registry, Container Registry, or Docker Hub.
69 */
70 image: string;
71 /** Entrypoint. Image ENTRYPOINT is used when omitted. */
72 command?: string[];
73 /** Arguments to the entrypoint. Image CMD is used when omitted. */
74 args?: string[];
75 /** Environment variables. */
76 env?: JobEnvVar[];
77 /** Working directory inside the container. */
78 workingDir?: string;
79 /** CPU/memory limits (`cpu`, `memory`, `nvidia.com/gpu`). */
80 resources?: {
81 limits?: Record<string, string>;
82 cpuIdle?: boolean;
83 startupCpuBoost?: boolean;
84 };
85 /** Volume mounts. Names must match `volumes`. */
86 volumeMounts?: Array<{
87 name: string;
88 mountPath: string;
89 subPath?: string;
90 }>;
91 };
92
93 export type JobVpcAccess = {
94 /** Traffic VPC egress (`ALL_TRAFFIC`, `PRIVATE_RANGES_ONLY`). */
95 egress?: string;
96 /**
97 * Serverless VPC Access connector
98 * (`projects/{project}/locations/{location}/connectors/{connector}`).
99 */
100 connector?: string;
101 /** Direct VPC egress network interfaces. */
102 networkInterfaces?: Array<{
103 network?: string;
104 subnetwork?: string;
105 tags?: string[];
106 }>;
107 };
108
109 export type JobBinaryAuthorization = {
110 /** Use the project's default Binary Authorization policy. */
111 useDefault?: boolean;
112 /** Breakglass justification. Requires `useDefault`. */
113 breakglassJustification?: string;
114 /** Policy path `projects/{project}/platforms/cloudRun/{policy}`. */
115 policy?: string;
116 };
117
118 export type JobProps = PlatformProps & {
119 /**
120 * Job id (the `{job}` segment of
121 * `projects/{project}/locations/{location}/jobs/{job}`). If omitted, a
122 * unique RFC1035 name is generated. 1-49 characters, lowercase letter
123 * first. Immutable — changing it replaces the job.
124 */
125 jobId?: string;
126 /**
127 * Region (`us-central1`, `europe-west1`, …). Immutable — changing it
128 * replaces the job. `US-CENTRAL1` is accepted and normalized to
129 * `us-central1`.
130 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
131 */
132 location?: string;
133 /**
134 * User labels. Alchemy ownership labels are merged in automatically.
135 */
136 labels?: Record<string, string>;
137 /**
138 * User annotations. Cloud Run rejects `run.googleapis.com`,
139 * `cloud.googleapis.com`, `serving.knative.dev`, and
140 * `autoscaling.knative.dev` namespaces.
141 */
142 annotations?: Record<string, string>;
143 /**
144 * Launch stage (`GA`, `BETA`, `ALPHA`). Defaults to GA.
145 */
146 launchStage?: string;
147 /**
148 * Binary Authorization settings.
149 */
150 binaryAuthorization?: JobBinaryAuthorization;
151 /**
152 * Number of tasks the execution should run.
153 * @default 1
154 */
155 taskCount?: number;
156 /**
157 * Maximum number of tasks to run in parallel. `0` uses the maximum.
158 */
159 parallelism?: number;
160 /**
161 * Labels applied to each execution created from this job.
162 */
163 executionLabels?: Record<string, string>;
164 /**
165 * Annotations applied to each execution created from this job.
166 */
167 executionAnnotations?: Record<string, string>;
168 /**
169 * Containers that run as each task. Required unless `main` is set.
170 */
171 containers?: JobContainer[];
172 /**
173 * Module entrypoint for an Effect-native job (typically
174 * `import.meta.url`). Alchemy bundles the program, builds a container,
175 * and runs it to completion. Bindings grant IAM onto the runtime SA.
176 */
177 main?: string;
178 /**
179 * Named export to load from `main`.
180 * @default "default"
181 */
182 handler?: string;
183 /**
184 * Additional environment variables for the Effect-native container.
185 */
186 env?: Record<string, any>;
187 /**
188 * Bundler configuration for `main`.
189 */
190 build?: Bundle.BundleConfig;
191 /**
192 * Max time a task attempt may run (e.g. `"600s"`).
193 * @default "600s"
194 */
195 timeout?: string;
196 /**
197 * Retries per task before the task is marked failed.
198 * @default 3
199 */
200 maxRetries?: number;
201 /**
202 * Service account email used by the task. Defaults to the project's
203 * Compute Engine default.
204 */
205 serviceAccount?: string;
206 /**
207 * Execution environment (`EXECUTION_ENVIRONMENT_GEN1` or
208 * `EXECUTION_ENVIRONMENT_GEN2`).
209 */
210 executionEnvironment?: string;
211 /**
212 * Customer-managed encryption key
213 * (`projects/{project}/locations/{location}/keyRings/{keyRing}/cryptoKeys/{cryptoKey}`).
214 */
215 encryptionKey?: string;
216 /**
217 * VPC Access configuration for the task.
218 */
219 vpcAccess?: JobVpcAccess;
220 };
221
222 export type Job = Resource<
223 "GCP.Run.Job",
224 JobProps,
225 {
226 /** Full resource name `projects/{project}/locations/{location}/jobs/{job}`. */
227 name: string;
228 /** Job id (last path segment). */
229 jobId: string;
230 /** Project id. */
231 project: string;
232 /** Location id (`us-central1`, …). */
233 location: string;
234 /** User labels (Alchemy ownership labels stripped). */
235 labels: Record<string, string>;
236 /** User annotations. */
237 annotations: Record<string, string>;
238 /** Server-assigned UUID. */
239 uid: string | undefined;
240 /** Monotonic generation. */
241 generation: string | undefined;
242 /** Whether Cloud Run is still reconciling the job. */
243 reconciling: boolean;
244 /** RFC3339 creation timestamp. */
245 createTime: string | undefined;
246 /** RFC3339 last-update timestamp. */
247 updateTime: string | undefined;
248 /** Number of executions created for this job. */
249 executionCount: number | undefined;
250 /** Terminal condition state (`CONDITION_SUCCEEDED`, …). */
251 terminalConditionState: string | undefined;
252 /** Latest created execution name, if any. */
253 latestCreatedExecutionName: string | undefined;
254 /** Image of the first container. */
255 image: string | undefined;
256 /** Retries per task. */
257 maxRetries: number | undefined;
258 /** Task timeout. */
259 timeout: string | undefined;
260 /** Desired task count. */
261 taskCount: number | undefined;
262 /** Max parallelism. */
263 parallelism: number | undefined;
264 /** Task service account email. */
265 serviceAccount: string | undefined;
266 /**
267 * Numeric unique ID of the runtime service account: the `sub` of the
268 * Google ID tokens the host fetches. An AWS binding's IAM role trusts
269 * it, so the host reaches AWS without an access key.
270 */
271 serviceAccountUniqueId: string | undefined;
272 /** True when Alchemy minted the per-host runtime service account. */
273 managedServiceAccount: boolean;
274 /** IAM roles bindings granted to the runtime service account. */
275 iamGrants: AppliedIamGrant[];
276 /** Hash of the bundled `main` program and its bootstrap (Effect-native only). */
277 codeHash: string | undefined;
278 },
279 GcpHostBinding,
280 Providers
281 >;
282
283 export type JobRuntimeContext = HostRuntimeContext;
284 export type JobServices = Credentials | GcpEnvironment | ServerHost;
285 /**
286 * Effect-native Job shape: a `run` entry that executes to completion
287 * when the container starts, and/or a `fetch` HTTP handler.
288 */
289 export type JobShape =
290 | void
291 | (Exclude<Main<JobServices>, void> & {
292 run?: Effect.Effect<void, never, JobServices | PlatformServices | RuntimeContext | Scope>;
293 });
294
295 /**
296 * A Cloud Run Job — a container that runs to completion.
297 *
298 * Changing `jobId` or `location` replaces the job.
299 *
300 * ### Creating a Job
301 * **Example:** Generated name
302 * ```typescript
303 * const job = yield* GCP.Run.Job("Migrate", {
304 * containers: [
305 * { image: "us-docker.pkg.dev/cloudrun/container/job:latest" },
306 * ],
307 * });
308 * ```
309 *
310 * **Example:** Explicit id, labels, and retries
311 * ```typescript
312 * const job = yield* GCP.Run.Job("Migrate", {
313 * jobId: "order-migrate",
314 * location: "us-central1",
315 * labels: { env: "prod" },
316 * maxRetries: 1,
317 * timeout: "120s",
318 * containers: [
319 * {
320 * image: "us-docker.pkg.dev/cloudrun/container/job:latest",
321 * env: [{ name: "STAGE", value: "prod" }],
322 * },
323 * ],
324 * });
325 * ```
326 *
327 * ### Effect-native Job with bindings
328 * **Example:** Drain a cache then exit
329 * ```typescript
330 * export class Warm extends GCP.Run.Job<Warm>()(
331 * "Warm",
332 * { main: import.meta.url },
333 * Effect.gen(function* () {
334 * const redis = yield* GCP.Redis.ReadWriteRedis(cache);
335 * return {
336 * run: redis.set("warmed", "1"),
337 * };
338 * }).pipe(Effect.provide(GCP.Redis.ReadWriteRedisHttp)),
339 * ) {}
340 * ```
341 *
342 * ### Running a Job
343 * **Example:** Trigger an execution
344 * ```typescript
345 * const runJob = yield* GCP.Run.RunJob(job);
346 * yield* runJob();
347 * ```
348 *
349 * @resource
350 * @category Run
351 */
352 export const Job: Platform<Job, JobServices, JobShape, JobRuntimeContext> = Platform(
353 "GCP.Run.Job",
354 {
355 createRuntimeContext: createContainerRuntimeContext("GCP.Run.Job") as (
356 id: string,
357 ) => JobRuntimeContext,
358 },
359 );
360
361 export class JobNotResolved extends Data.TaggedError("GCP.Run.JobNotResolved")<{
362 resourceName: string;
363 }> {}
364
365 export class JobNotReady extends Data.TaggedError("GCP.Run.JobNotReady")<{
366 resourceName: string;
367 state: string;
368 message: string;
369 }> {}
370
371 export class JobReconciling extends Data.TaggedError("GCP.Run.JobReconciling")<{
372 resourceName: string;
373 state: string;
374 }> {}
375
376 export class JobStillExists extends Data.TaggedError("GCP.Run.JobStillExists")<{
377 resourceName: string;
378 }> {}
379
380 const lastSegment = (value: string) => {
381 const trimmed = value.replace(/\/+$/, "");
382 const parts = trimmed.split("/");
383 return parts[parts.length - 1] || trimmed;
384 };
385
386 const normalizeLocation = (location: string | undefined, defaultLocation: string) =>
387 lastSegment(location ?? defaultLocation).toLowerCase();
388
389 const rfc1035 = (name: string): string => {
390 let next = name
391 .toLowerCase()
392 .replace(/[^a-z0-9-]/g, "-")
393 .replace(/-+/g, "-")
394 .replace(/^-+|-+$/g, "");
395 if (!/^[a-z]/.test(next)) next = `j${next}`;
396 next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/g, "");
397 return next.length > 0 ? next : "job";
398 };
399
400 const resourceName = (project: string, location: string, jobId: string) =>
401 `projects/${project}/locations/${location}/jobs/${jobId}`;
402
403 const parseName = (name: string, defaultLocation: string) => {
404 const parts = name.split("/").filter((part) => part.length > 0);
405 const jobsAt = parts.lastIndexOf("jobs");
406 const locationsAt = parts.lastIndexOf("locations");
407 const projectsAt = parts.lastIndexOf("projects");
408 return {
409 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
410 location:
411 locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : defaultLocation,
412 jobId: jobsAt >= 0 && parts[jobsAt + 1] ? parts[jobsAt + 1]! : lastSegment(name),
413 };
414 };
415
416 const userLabels = (
417 labels: Record<string, string | undefined> | null | undefined,
418 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
419
420 const userAnnotations = (
421 annotations: Record<string, string | undefined> | null | undefined,
422 ): Record<string, string> => tagRecord(annotations);
423
424 const toId = (id: string, jobId: string | undefined, existing?: string) =>
425 Effect.gen(function* () {
426 if (jobId !== undefined) return jobId;
427 if (existing !== undefined) return existing;
428 return rfc1035(
429 yield* createPhysicalName({
430 id,
431 maxLength: MAX_NAME_LENGTH,
432 lowercase: true,
433 }),
434 );
435 });
436
437 const HOST_TYPE = "GCP.Run.Job";
438
439 const bootstrapFor = (news: JobProps) => makeGcpBootstrap("CloudRunJob", news.handler ?? "default");
440
441 const toAttrs = (
442 job: cloudrun.GoogleCloudRunV2Job,
443 project: string,
444 region: string,
445 extras: {
446 iamGrants?: AppliedIamGrant[];
447 codeHash?: string;
448 serviceAccountUniqueId?: string;
449 } = {},
450 ): Job["Attributes"] => {
451 const name = job.name ?? "";
452 const parsed = parseName(name, region);
453 const task = job.template?.template;
454 const container = task?.containers?.[0];
455 return {
456 name,
457 jobId: parsed.jobId,
458 project: parsed.project || project,
459 location: parsed.location,
460 labels: userLabels(job.labels),
461 annotations: userAnnotations(job.annotations),
462 uid: job.uid,
463 generation: job.generation,
464 reconciling: job.reconciling === true,
465 createTime: job.createTime,
466 updateTime: job.updateTime,
467 executionCount: job.executionCount,
468 terminalConditionState: job.terminalCondition?.state,
469 latestCreatedExecutionName: job.latestCreatedExecution?.name,
470 image: container?.image,
471 maxRetries: task?.maxRetries,
472 timeout: task?.timeout,
473 taskCount: job.template?.taskCount,
474 parallelism: job.template?.parallelism,
475 serviceAccount: task?.serviceAccount,
476 managedServiceAccount: isManagedServiceAccount({
477 project: parsed.project || project,
478 hostType: HOST_TYPE,
479 resourceName: name,
480 serviceAccount: task?.serviceAccount,
481 }),
482 serviceAccountUniqueId: extras.serviceAccountUniqueId,
483 iamGrants: extras.iamGrants ?? [],
484 codeHash: extras.codeHash,
485 };
486 };
487
488 const getByName = (name: string) =>
489 cloudrun
490 .getProjectsLocationsJobs({ name })
491 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
492
493 const toApiContainers = (
494 containers: JobContainer[] | undefined,
495 ): cloudrun.GoogleCloudRunV2ContainerList =>
496 (containers ?? []).map((container) => ({
497 name: container.name,
498 image: container.image,
499 command: container.command,
500 args: container.args,
501 env: container.env?.map((item) => ({
502 name: item.name,
503 value: item.value,
504 valueSource: item.valueSource
505 ? {
506 secretKeyRef: item.valueSource.secretKeyRef
507 ? {
508 secret: item.valueSource.secretKeyRef.secret,
509 version: item.valueSource.secretKeyRef.version,
510 }
511 : undefined,
512 }
513 : undefined,
514 })),
515 workingDir: container.workingDir,
516 resources: container.resources
517 ? {
518 limits: container.resources.limits,
519 cpuIdle: container.resources.cpuIdle,
520 startupCpuBoost: container.resources.startupCpuBoost,
521 }
522 : undefined,
523 volumeMounts: container.volumeMounts,
524 }));
525
526 const stable = (value: unknown): string =>
527 JSON.stringify(value, (_key, current) => {
528 if (current && typeof current === "object" && !Array.isArray(current)) {
529 return Object.fromEntries(
530 Object.entries(current as Record<string, unknown>)
531 .filter(([, item]) => item !== undefined)
532 .sort(([a], [b]) => a.localeCompare(b)),
533 );
534 }
535 return current;
536 });
537
538 const normalizeDuration = (value: string | undefined) => {
539 if (value === undefined) return undefined;
540 const match = /^([0-9]+(?:\.[0-9]+)?)s$/.exec(value.trim());
541 if (!match) return value;
542 return `${Number(match[1])}s`;
543 };
544
545 const comparableEnv = (env: cloudrun.GoogleCloudRunV2EnvVarList | undefined) =>
546 [...(env ?? [])]
547 .map((item) => ({
548 name: item.name ?? "",
549 value: item.value ?? "",
550 secret: item.valueSource?.secretKeyRef?.secret ?? "",
551 version: item.valueSource?.secretKeyRef?.version ?? "",
552 }))
553 .sort((left, right) => left.name.localeCompare(right.name));
554
555 const comparableContainers = (containers: JobContainer[] | undefined) =>
556 (containers ?? []).map((container) => ({
557 name: container.name ?? "",
558 image: container.image,
559 command: container.command ?? [],
560 args: container.args ?? [],
561 env: comparableEnv(container.env),
562 workingDir: container.workingDir ?? "",
563 limits: container.resources?.limits ? tagRecord(container.resources.limits) : {},
564 }));
565
566 const comparableObservedContainers = (
567 containers: cloudrun.GoogleCloudRunV2ContainerList | undefined,
568 desired: JobContainer[] | undefined,
569 ) =>
570 (containers ?? []).map((container, index) => {
571 const user = desired?.[index];
572 return {
573 name: user?.name !== undefined ? (container.name ?? "") : "",
574 image: container.image ?? "",
575 command: container.command ?? [],
576 args: container.args ?? [],
577 env: comparableEnv(container.env),
578 workingDir: container.workingDir ?? "",
579 limits: user?.resources?.limits ? tagRecord(container.resources?.limits) : {},
580 };
581 });
582
583 const desiredTemplate = (
584 news: JobProps,
585 current: cloudrun.GoogleCloudRunV2Job | undefined,
586 ): cloudrun.GoogleCloudRunV2ExecutionTemplate => {
587 const exec = current?.template;
588 const task = exec?.template;
589 return {
590 taskCount: news.taskCount ?? exec?.taskCount,
591 parallelism: news.parallelism ?? exec?.parallelism,
592 labels: news.executionLabels ? toLabels(news.executionLabels) : exec?.labels,
593 annotations: news.executionAnnotations ?? exec?.annotations,
594 template: {
595 containers: toApiContainers(news.containers),
596 timeout: news.timeout ?? task?.timeout,
597 maxRetries: news.maxRetries ?? task?.maxRetries,
598 serviceAccount: news.serviceAccount ?? task?.serviceAccount,
599 executionEnvironment: news.executionEnvironment ?? task?.executionEnvironment,
600 encryptionKey: news.encryptionKey ?? task?.encryptionKey,
601 vpcAccess: news.vpcAccess ?? task?.vpcAccess,
602 volumes: task?.volumes,
603 gpuZonalRedundancyDisabled: task?.gpuZonalRedundancyDisabled,
604 nodeSelector: task?.nodeSelector,
605 },
606 };
607 };
608
609 /** Waits on a Cloud Run long-running operation (revisions roll out in minutes). */
610 const waitForOperation = (
611 operation: cloudrun.GoogleLongrunningOperation,
612 options?: { notFoundOk?: boolean },
613 ) =>
614 waitForLongRunningOperation(
615 operation,
616 (name) => {
617 const get = cloudrun.getProjectsLocationsOperations({ name });
618 return options?.notFoundOk === true
619 ? get.pipe(
620 Effect.catchTag("NotFound", () =>
621 Effect.succeed<LongRunningOperation>({ name, done: true }),
622 ),
623 )
624 : get.pipe(
625 // A just-returned operation can briefly 404 on read.
626 Effect.retry({
627 while: (error) => error._tag === "NotFound",
628 times: 5,
629 schedule: Schedule.exponential("250 millis"),
630 }),
631 );
632 },
633 { budget: "10 minutes" },
634 ).pipe(
635 // google.rpc.Code NOT_FOUND: the resource was already gone.
636 Effect.catchTag("GCP.OperationFailed", (error) =>
637 options?.notFoundOk === true && error.code === 5
638 ? Effect.succeed<LongRunningOperation>(operation)
639 : Effect.fail(error),
640 ),
641 );
642
643 const isPendingJob = (job: cloudrun.GoogleCloudRunV2Job) => {
644 const state = job.terminalCondition?.state ?? "";
645 return (
646 job.reconciling === true ||
647 state === "CONDITION_PENDING" ||
648 state === "CONDITION_RECONCILING" ||
649 state === "" ||
650 state === "STATE_UNSPECIFIED"
651 );
652 };
653
654 const waitUntilReady = (name: string) =>
655 getByName(name).pipe(
656 Effect.filterOrFail(
657 (job): job is cloudrun.GoogleCloudRunV2Job =>
658 job !== undefined && job.deleteTime === undefined,
659 () => new JobNotResolved({ resourceName: name }),
660 ),
661 Effect.filterOrFail(
662 (job) => (job.terminalCondition?.state ?? "") !== "CONDITION_FAILED",
663 (job) =>
664 new JobNotReady({
665 resourceName: name,
666 state: job.terminalCondition?.state ?? "",
667 message: job.terminalCondition?.message ?? "job failed",
668 }),
669 ),
670 Effect.filterOrFail(
671 (job) => !isPendingJob(job),
672 (job) =>
673 new JobReconciling({
674 resourceName: name,
675 state: job.terminalCondition?.state || "reconciling",
676 }),
677 ),
678 Effect.retry({
679 while: (error) =>
680 error._tag === "GCP.Run.JobReconciling" || error._tag === "GCP.Run.JobNotResolved",
681 times: 10,
682 schedule: Schedule.spaced("4 seconds"),
683 }),
684 );
685
686 const waitUntilGone = (name: string) =>
687 getByName(name).pipe(
688 Effect.flatMap((job) =>
689 job === undefined ? Effect.void : Effect.fail(new JobStillExists({ resourceName: name })),
690 ),
691 Effect.retry({
692 while: (error) => error._tag === "GCP.Run.JobStillExists",
693 times: 10,
694 schedule: Schedule.spaced("2 seconds"),
695 }),
696 );
697
698 export const JobProvider = () =>
699 Provider.succeed(Job, {
700 stables: ["name", "jobId", "project", "location", "uid", "createTime"],
701
702 diff: Effect.fn(function* ({ news, olds, output }) {
703 if (!isResolved(news)) return undefined;
704 const env = yield* GcpEnvironment.current;
705
706 const previousId = olds?.jobId ?? output?.jobId;
707 const nextId = news.jobId ?? previousId;
708 const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region);
709 const nextLocation = normalizeLocation(news.location ?? output?.location, env.region);
710
711 const idChanged = previousId !== undefined && nextId !== undefined && nextId !== previousId;
712 const locationChanged = previousLocation !== nextLocation;
713 if (idChanged || locationChanged) {
714 return { action: "replace" as const, deleteFirst: false };
715 }
716 // A code-only change leaves the props untouched; hash the bundled
717 // program (and bootstrap) so it still surfaces as an update.
718 if (output !== undefined && news.main !== undefined) {
719 const images = yield* makeImageSource;
720 const hash = yield* images.hash({
721 source: { main: news.main, handler: news.handler, build: news.build },
722 isExternal: news.isExternal,
723 bootstrap: bootstrapFor(news),
724 });
725 if (hash !== undefined && hash !== output.codeHash) {
726 return { action: "update" as const };
727 }
728 }
729 return undefined;
730 }),
731
732 read: Effect.fn(function* ({ id, olds, output }) {
733 const env = yield* GcpEnvironment.current;
734 const jobId = yield* toId(id, olds?.jobId, output?.jobId);
735 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
736 const name = output?.name ?? resourceName(env.project, location, jobId);
737 const existing = yield* getByName(name);
738 if (existing === undefined || existing.deleteTime !== undefined) {
739 return undefined;
740 }
741 const attrs = toAttrs(existing, env.project, env.region, {
742 iamGrants: output?.iamGrants,
743 codeHash: output?.codeHash,
744 serviceAccountUniqueId: output?.serviceAccountUniqueId,
745 });
746 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
747 }),
748
749 list: () =>
750 Effect.gen(function* () {
751 const env = yield* GcpEnvironment.current;
752 return yield* cloudrun.listProjectsLocationsJobs
753 .pages({
754 parent: `projects/${env.project}/locations/-`,
755 pageSize: 1000,
756 })
757 .pipe(
758 Stream.flatMap((page) => Stream.fromIterable(page.jobs ?? [])),
759 Stream.filter(
760 (job) =>
761 job.deleteTime === undefined &&
762 Object.keys(job.labels ?? {}).some((key) => key.startsWith("alchemy-")),
763 ),
764 Stream.map((job) => toAttrs(job, env.project, env.region)),
765 Stream.runCollect,
766 Effect.map((chunk) => Array.from(chunk)),
767 );
768 }),
769
770 reconcile: Effect.fn(function* ({ id, news, output, bindings, session }) {
771 const env = yield* GcpEnvironment.current;
772 const jobId = yield* toId(id, news.jobId, output?.jobId);
773 const location = normalizeLocation(news.location ?? output?.location, env.region);
774 const name = resourceName(env.project, location, jobId);
775 const desiredLabels = {
776 ...toLabels(news.labels),
777 ...(yield* createInternalLabels(id)),
778 };
779 const desiredAnnotations = userAnnotations(news.annotations);
780 const identity = yield* resolveHostIdentity({
781 project: env.project,
782 hostType: HOST_TYPE,
783 resourceName: name,
784 userServiceAccount: news.serviceAccount,
785 effectNative: news.main !== undefined,
786 bindings: bindings as ResourceBinding<GcpHostBinding>[],
787 output,
788 });
789 const serviceAccount = identity.serviceAccount;
790 let containers = news.containers;
791 let codeHash: string | undefined;
792 if (news.main !== undefined) {
793 const images = yield* makeImageSource;
794 const image = yield* images
795 .resolve({
796 id,
797 source: {
798 main: news.main,
799 handler: news.handler ?? "default",
800 build: news.build,
801 },
802 repositoryName: rfc1035(`${jobId}-src`),
803 location,
804 isExternal: news.isExternal,
805 bootstrap: bootstrapFor(news),
806 session,
807 })
808 .pipe(Effect.onError(() => identity.cleanup));
809 codeHash = image.codeHash;
810 const container = news.containers?.[0];
811 containers = [
812 {
813 ...container,
814 image: image.imageUri,
815 env: mergeContainerEnv(
816 container?.env,
817 identity.env,
818 yield* alchemyRuntimeEnv,
819 news.env,
820 ),
821 },
822 ];
823 } else {
824 const runtimeEnv = { ...identity.env, ...news.env };
825 if (Object.keys(runtimeEnv).length > 0 && containers) {
826 containers = containers.map((container, index) =>
827 index === 0
828 ? {
829 ...container,
830 env: mergeContainerEnv(container.env, runtimeEnv),
831 }
832 : container,
833 );
834 }
835 }
836 const effectiveNews: JobProps = { ...news, containers, serviceAccount };
837
838 let current = yield* getByName(name);
839 if (current?.deleteTime !== undefined) {
840 yield* waitUntilGone(name);
841 current = undefined;
842 }
843
844 if (current === undefined) {
845 const created = yield* cloudrun
846 .createProjectsLocationsJobs({
847 parent: `projects/${env.project}/locations/${location}`,
848 jobId,
849 body: {
850 labels: desiredLabels,
851 annotations:
852 Object.keys(desiredAnnotations).length > 0 ? desiredAnnotations : undefined,
853 launchStage: news.launchStage,
854 binaryAuthorization: news.binaryAuthorization,
855 template: desiredTemplate(effectiveNews, undefined),
856 },
857 })
858 .pipe(
859 retryActAs,
860 Effect.catchTag("Conflict", () => Effect.undefined),
861 Effect.onError(() => identity.cleanup),
862 );
863 if (created !== undefined) {
864 yield* waitForOperation(created);
865 }
866 current = yield* waitUntilReady(name);
867 }
868
869 if (current === undefined) {
870 return yield* new JobNotResolved({ resourceName: name });
871 }
872
873 const observedLabels = tagRecord(current.labels);
874 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
875 const labelsChanged = upsert.length > 0 || removed.length > 0;
876 const annotationsChanged =
877 news.annotations !== undefined &&
878 stable(desiredAnnotations) !== stable(userAnnotations(current.annotations));
879 const containersChanged =
880 stable(comparableContainers(effectiveNews.containers)) !==
881 stable(
882 comparableObservedContainers(
883 current.template?.template?.containers,
884 effectiveNews.containers,
885 ),
886 );
887 const timeoutChanged =
888 news.timeout !== undefined &&
889 normalizeDuration(news.timeout) !== normalizeDuration(current.template?.template?.timeout);
890 const maxRetriesChanged =
891 news.maxRetries !== undefined &&
892 (current.template?.template?.maxRetries ?? 3) !== news.maxRetries;
893 const taskCountChanged =
894 news.taskCount !== undefined && (current.template?.taskCount ?? 1) !== news.taskCount;
895 const parallelismChanged =
896 news.parallelism !== undefined && (current.template?.parallelism ?? 0) !== news.parallelism;
897 const serviceAccountChanged =
898 (current.template?.template?.serviceAccount ?? "") !== serviceAccount;
899 const executionEnvironmentChanged =
900 news.executionEnvironment !== undefined &&
901 (current.template?.template?.executionEnvironment ?? "") !== news.executionEnvironment;
902 const encryptionKeyChanged =
903 news.encryptionKey !== undefined &&
904 (current.template?.template?.encryptionKey ?? "") !== news.encryptionKey;
905 const vpcChanged =
906 news.vpcAccess !== undefined &&
907 stable(news.vpcAccess) !== stable(current.template?.template?.vpcAccess);
908 const launchStageChanged =
909 news.launchStage !== undefined && (current.launchStage ?? "GA") !== news.launchStage;
910 const binaryAuthorizationChanged =
911 news.binaryAuthorization !== undefined &&
912 stable(news.binaryAuthorization) !== stable(current.binaryAuthorization);
913 const executionLabelsChanged =
914 news.executionLabels !== undefined &&
915 stable(toLabels(news.executionLabels)) !== stable(tagRecord(current.template?.labels));
916 const executionAnnotationsChanged =
917 news.executionAnnotations !== undefined &&
918 stable(tagRecord(news.executionAnnotations)) !==
919 stable(tagRecord(current.template?.annotations));
920
921 if (
922 labelsChanged ||
923 annotationsChanged ||
924 containersChanged ||
925 timeoutChanged ||
926 maxRetriesChanged ||
927 taskCountChanged ||
928 parallelismChanged ||
929 serviceAccountChanged ||
930 executionEnvironmentChanged ||
931 encryptionKeyChanged ||
932 vpcChanged ||
933 launchStageChanged ||
934 binaryAuthorizationChanged ||
935 executionLabelsChanged ||
936 executionAnnotationsChanged
937 ) {
938 const operation = yield* cloudrun.patchProjectsLocationsJobs({
939 name,
940 body: {
941 name,
942 labels: desiredLabels,
943 annotations: news.annotations !== undefined ? desiredAnnotations : current.annotations,
944 launchStage: news.launchStage ?? current.launchStage,
945 binaryAuthorization: news.binaryAuthorization ?? current.binaryAuthorization,
946 template: desiredTemplate(effectiveNews, current),
947 },
948 });
949 yield* waitForOperation(operation);
950 current = yield* waitUntilReady(name);
951 }
952
953 if (current === undefined) {
954 return yield* new JobNotResolved({ resourceName: name });
955 }
956
957 return toAttrs(current, env.project, env.region, {
958 iamGrants: identity.grants,
959 codeHash,
960 serviceAccountUniqueId: identity.uniqueId,
961 });
962 }),
963
964 delete: Effect.fn(function* ({ id, output }) {
965 const operation = yield* cloudrun
966 .deleteProjectsLocationsJobs({ name: output.name })
967 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
968 if (operation !== undefined) {
969 yield* waitForOperation(operation, { notFoundOk: true });
970 }
971 yield* waitUntilGone(output.name);
972 // Effect-native hosts build into a per-host repository on reconcile.
973 yield* destroyHostImageRepository(id, output, rfc1035(`${output.jobId}-src`));
974 yield* releaseHostIdentity(output);
975 }),
976 });