The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import * as Kubernetes from "@alchemy.run/kubernetes"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { EntriesDatabase, entryPath, GuestbookCluster, GuestbookNamespace } from "./infra.ts"; |
| 5 | |
| 6 | export const seedEntries = [ |
| 7 | { id: "ada", message: "First computers, now clusters." }, |
| 8 | { |
| 9 | id: "grace", |
| 10 | message: "A cluster in port is safe, but that is not what clusters are for.", |
| 11 | }, |
| 12 | { id: "linus", message: "Talk is cheap. Show me the manifest." }, |
| 13 | ]; |
| 14 | |
| 15 | /** |
| 16 | * A one-shot `Kubernetes.Job` in the INLINE EFFECT form: props + an init Effect |
| 17 | * whose impl returns `{ run }` — a one-shot entry that executes to |
| 18 | * completion inside the pod, after which the process exits (the Kubernetes |
| 19 | * analog of `GCP.Run.Job`). |
| 20 | * |
| 21 | * The Effect program is bundled into a generated image |
| 22 | * (`main: import.meta.url` names this module as the entrypoint). The |
| 23 | * `GCP.Firestore.WriteDatabase` binding injects the database name into the |
| 24 | * pod and grants `roles/datastore.user` (project-level, conditioned to the |
| 25 | * database) to the Job's Kubernetes ServiceAccount principal. |
| 26 | * |
| 27 | * Applying the batch/v1 Job runs it: the seed executes once on deploy. |
| 28 | * Adding `schedule: "0 3 * * *"` to the props would synthesize a CronJob |
| 29 | * instead. |
| 30 | */ |
| 31 | export default Kubernetes.Job( |
| 32 | "SeedJob", |
| 33 | // Props are themselves an Effect so they can reference shared resources. |
| 34 | Effect.gen(function* () { |
| 35 | const cluster = yield* GuestbookCluster; |
| 36 | const ns = yield* GuestbookNamespace; |
| 37 | return { |
| 38 | cluster, |
| 39 | main: import.meta.url, |
| 40 | // Deploy after the namespace exists (see src/infra.ts). |
| 41 | namespace: ns.name, |
| 42 | // A fresh deploy's Firestore grant (a project binding under an IAM |
| 43 | // Condition) can take a few minutes to reach Firestore; Kubernetes |
| 44 | // retries the pod with exponential backoff (10s, 20s, 40s, …) until |
| 45 | // then. |
| 46 | backoffLimit: 6, |
| 47 | resources: { |
| 48 | requests: { cpu: "250m", memory: "512Mi" }, |
| 49 | limits: { cpu: "250m", memory: "512Mi" }, |
| 50 | }, |
| 51 | }; |
| 52 | }), |
| 53 | Effect.gen(function* () { |
| 54 | const db = yield* GCP.Firestore.WriteDatabase(EntriesDatabase); |
| 55 | |
| 56 | return { |
| 57 | // One-shot entry: seed the database, log, exit 0. |
| 58 | run: Effect.gen(function* () { |
| 59 | yield* Effect.forEach( |
| 60 | seedEntries, |
| 61 | (entry) => |
| 62 | db.set(entryPath(entry.id), { |
| 63 | author: entry.id, |
| 64 | message: entry.message, |
| 65 | }), |
| 66 | { discard: true }, |
| 67 | ); |
| 68 | yield* Effect.log(`seeded ${seedEntries.length} guestbook entries`); |
| 69 | }).pipe(Effect.orDie), |
| 70 | }; |
| 71 | }).pipe(Effect.provide(GCP.Firestore.WriteDatabaseHttp)), |
| 72 | ); |