@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, 04:45 UTC

main
ts 67 lines 2.66 KB
Raw
1 import { createHash } from "node:crypto";
2 import * as GCP from "@alchemy.run/gcp";
3 import * as Effect from "effect/Effect";
4 import * as Result from "effect/Result";
5 import * as Stream from "effect/Stream";
6 import { Jobs, Results, resultPath, type JobMessage, type JobResult } from "./resources.ts";
7
8 const decode = (data: string | undefined) =>
9 Effect.try(() => JSON.parse(Buffer.from(data ?? "", "base64").toString("utf8")) as JobMessage);
10
11 /** The "work": hash and count the payload. */
12 const runJob = (job: JobMessage) =>
13 Effect.sync((): JobResult => ({
14 status: "done",
15 sha256: createHash("sha256").update(job.payload, "utf8").digest("hex"),
16 words: job.payload.split(/\s+/).filter(Boolean).length,
17 chars: job.payload.length,
18 submittedAt: job.submittedAt,
19 completedAt: new Date().toISOString(),
20 }));
21
22 /**
23 * The consumer. A Cloud Run worker pool has no inbound URL — it runs a
24 * long-lived container that pulls from Pub/Sub, which suits queue workers
25 * that should keep running regardless of HTTP traffic.
26 *
27 * `GCP.Run.TopicPullEventSource` creates the pull subscription, pulls
28 * batches in the background, and acks a batch only after the handler
29 * succeeds. Writing the result with `set` makes processing idempotent: a
30 * redelivered job overwrites its document with the same values.
31 */
32 export default class Worker extends GCP.Run.WorkerPool<Worker>()(
33 "Worker",
34 {
35 main: import.meta.url,
36 scaling: { manualInstanceCount: 1 },
37 },
38 Effect.gen(function* () {
39 const results = yield* GCP.Firestore.WriteDatabase(Results);
40
41 yield* GCP.PubSub.consumeTopicMessages(
42 Jobs,
43 { maxMessages: 10, ackDeadlineSeconds: 60 },
44 (messages) =>
45 messages.pipe(
46 Stream.runForEach(({ message }) =>
47 Effect.gen(function* () {
48 const decoded = yield* Effect.result(decode(message.data));
49 // A message that can never parse would be redelivered
50 // forever; drop it (the batch ack removes it) instead.
51 if (Result.isFailure(decoded)) {
52 yield* Effect.logWarning(`dropping malformed message ${message.messageId}`);
53 return;
54 }
55 const job = decoded.success;
56 const result = yield* runJob(job);
57 yield* results.set(resultPath(job.id), { ...result });
58 yield* Effect.log(`job ${job.id}: ${result.words} word(s)`);
59 }),
60 ),
61 // A failure dies the batch, leaving it unacked so Pub/Sub
62 // redelivers it after the ack deadline.
63 Effect.orDie,
64 ),
65 );
66 }).pipe(Effect.provide([GCP.Run.TopicPullEventSource, GCP.Firestore.WriteDatabaseHttp])),
67 ) {}