The best way to control Claude Code, Codex and more from any computer or phone
| 1 | --- |
| 2 | title: Cloud Scheduler |
| 3 | description: Run code on a cron schedule with GCP.CloudScheduler.consumeSchedule — the handler lives on your Cloud Run service and only Cloud Scheduler can call it. |
| 4 | --- |
| 5 | |
| 6 | `consumeSchedule` runs a handler on your Cloud Run service on a cron |
| 7 | schedule. Alchemy creates the Cloud Scheduler job and a private path |
| 8 | on the service that only the scheduler can call. |
| 9 | |
| 10 | ## Add a schedule |
| 11 | |
| 12 | ```typescript |
| 13 | // src/Monitor.ts |
| 14 | import * as GCP from "@alchemy.run/gcp"; |
| 15 | import * as Effect from "effect/Effect"; |
| 16 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 17 | |
| 18 | export default class Monitor extends GCP.Function<Monitor>()( |
| 19 | "Monitor", |
| 20 | { main: import.meta.url }, |
| 21 | Effect.gen(function* () { |
| 22 | yield* GCP.CloudScheduler.consumeSchedule( |
| 23 | "Heartbeat", |
| 24 | { schedule: "* * * * *", timeZone: "Etc/UTC" }, |
| 25 | (event) => Effect.log(`tick ${event.scheduleTime}`), |
| 26 | ); |
| 27 | |
| 28 | return { fetch: Effect.succeed(HttpServerResponse.text("ok")) }; |
| 29 | }).pipe(Effect.provide(GCP.Run.ScheduleEventSource)), |
| 30 | ) {} |
| 31 | ``` |
| 32 | |
| 33 | `schedule` is standard cron. `event` carries the job name and the time |
| 34 | the run was scheduled for. |
| 35 | |
| 36 | ## Several schedules on one service |
| 37 | |
| 38 | Give each schedule its own name: |
| 39 | |
| 40 | ```typescript |
| 41 | yield* GCP.CloudScheduler.consumeSchedule( |
| 42 | "Heartbeat", |
| 43 | { schedule: "* * * * *", timeZone: "Etc/UTC" }, |
| 44 | recordHeartbeat, |
| 45 | ); |
| 46 | |
| 47 | yield* GCP.CloudScheduler.consumeSchedule( |
| 48 | "DailyRollup", |
| 49 | { schedule: "0 0 * * *", timeZone: "Etc/UTC" }, |
| 50 | rollUpYesterday, |
| 51 | ); |
| 52 | ``` |
| 53 | |
| 54 | ## Public service, private schedules |
| 55 | |
| 56 | `invokerIamDisabled: true` opens your `fetch` routes to everyone. The |
| 57 | schedule path still rejects any request without a valid token from |
| 58 | Cloud Scheduler. |
| 59 | |
| 60 | ## Use bindings in the handler |
| 61 | |
| 62 | Handlers use the same clients as `fetch`: |
| 63 | |
| 64 | ```typescript |
| 65 | const writer = yield* GCP.BigQuery.WriteTable(table); |
| 66 | |
| 67 | yield* GCP.CloudScheduler.consumeSchedule( |
| 68 | "Heartbeat", |
| 69 | { schedule: "* * * * *", timeZone: "Etc/UTC" }, |
| 70 | (event) => |
| 71 | writer |
| 72 | .insert([{ jobName: event.jobName, receivedAt: new Date() }]) |
| 73 | .pipe(Effect.orDie), |
| 74 | ); |
| 75 | ``` |
| 76 | |
| 77 | ## Start a job on a schedule |
| 78 | |
| 79 | To run a [Cloud Run job](/gcp/compute/jobs) nightly, point a |
| 80 | `CloudScheduler.Job` straight at it. See |
| 81 | [Run it on a schedule](/gcp/compute/jobs#run-it-on-a-schedule). |
| 82 | |
| 83 | ## Example |
| 84 | |
| 85 | [`examples/gcp-cron`](https://github.com/alchemy-run/alchemy/tree/main/examples/gcp-cron) |
| 86 | runs two schedules on one service and records each run in BigQuery. |
| 87 | |
| 88 | ## Where next |
| 89 | |
| 90 | - [Cloud Run jobs](/gcp/compute/jobs) — batch work on a timer. |
| 91 | - [CloudScheduler reference](/providers/gcp/cloudscheduler#job) — |
| 92 | every prop. |