Commit b19ed9f
feat(schedule): add Temporal schedule for periodic scanning (#4)
## Summary
- Adds Temporal Schedule API support so the OrchestratorWorkflow runs
automatically on a configurable cron (default: daily at 06:00 UTC per
the design doc)
- Schedule manager uses a create-or-update pattern to handle server
restarts gracefully (if schedule already exists, updates it if cron
changed)
- Disabled by default via `SCHEDULE_ENABLED=false` for backwards
compatibility
- Wires `SCHEDULE_*` env vars through `docker-compose.yaml` so the
feature can be toggled for local end-to-end testing
## Configuration
| Env Var | Default | Purpose |
|---------|---------|---------|
| `SCHEDULE_ENABLED` | `false` | Opt-in to scheduled scanning |
| `SCHEDULE_CRON` | `0 6 * * *` | Cron expression (daily at 06:00 UTC) |
| `SCHEDULE_ID` | `version-guard-scan` | Stable schedule ID for
idempotent restarts |
| `SCHEDULE_JITTER` | `5m` | Random jitter to prevent thundering herd |
## Changes
**New:**
- `pkg/schedule/schedule.go` — Schedule manager with create-or-update
logic
- `pkg/schedule/schedule_test.go` — 8 unit tests covering all paths
**Modified:**
- `cmd/server/main.go` — Config fields, schedule wiring with graceful
failure
- `pkg/workflow/orchestrator/workflow.go` — ScanID fallback from
workflow execution ID for scheduled runs
- `docker-compose.yaml` — `SCHEDULE_*` env var passthrough
## Bug fixed during review
Initial implementation mutated only `CronExpressions` on the update
path. Temporal parses `CronExpressions` into structured `Calendars`
server-side on create, so subsequent describes return the cron inside
`Calendars` with `CronExpressions` empty. Mutating only
`CronExpressions` left the stale calendar in place, causing the schedule
to fire on both the old and new crons after every restart with a changed
cron.
Fixed by replacing the entire `Spec` on update. Regression test
`TestEnsureSchedule_Update_ReplacesStaleCalendars` simulates the real
Temporal describe response and asserts `Calendars` is cleared.
## Test plan
- [x] `go build ./...` compiles
- [x] `go test ./pkg/schedule/...` — 8/8 tests pass
- [x] `go test ./...` — full suite passes
- [x] `golangci-lint run ./pkg/schedule/...` clean
- [x] Manual end-to-end against `temporal server start-dev`:
- Start 1: `SCHEDULE_ENABLED=true SCHEDULE_CRON="0 6 * * *"` → `temporal
schedule describe` shows one calendar entry for 06:00
- Start 2: restart with `SCHEDULE_CRON="*/30 * * * *"` → server logs
`Schedule updated`, `temporal schedule describe` shows a single calendar
entry matching `*/30` (stale entry correctly cleared)
- `temporal workflow list --query 'WorkflowType =
"OrchestratorWorkflow"'` confirms scheduled runs fire on cron boundaries
### Reproducing locally
```bash
# Terminal 1: start Temporal dev server
temporal server start-dev --namespace version-guard-dev
# Terminal 2: run the server with scheduling enabled
SCHEDULE_ENABLED=true \
SCHEDULE_CRON="*/5 * * * *" \
SCHEDULE_ID="version-guard-local" \
SCHEDULE_JITTER=1m \
TEMPORAL_ENDPOINT=localhost:7233 \
TEMPORAL_NAMESPACE=version-guard-dev \
WIZ_CLIENT_ID_SECRET=<your-id> \
WIZ_CLIENT_SECRET_SECRET=<your-secret> \
WIZ_REPORT_IDS='{"aurora-postgresql":"<report-id>"}' \
CONFIG_PATH=config/resources.yaml \
go run ./cmd/server
# Terminal 3: verify via CLI
temporal schedule list --namespace version-guard-dev
temporal schedule describe --schedule-id version-guard-local --namespace version-guard-dev
temporal workflow list --namespace version-guard-dev \
--query 'WorkflowType = "OrchestratorWorkflow"'
```
Note: `docker-compose up` works too once `SCHEDULE_ENABLED` is exported,
but the bundled Temporal image currently needs a fix on `main` before
compose boots a healthy server — out of scope for this PR.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent 3ee601a commit b19ed9f
5 files changed
Lines changed: 470 additions & 1 deletion
File tree
- cmd/server
- pkg
- schedule
- workflow/orchestrator
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| |||
72 | 73 | | |
73 | 74 | | |
74 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
75 | 82 | | |
76 | 83 | | |
77 | 84 | | |
| |||
132 | 139 | | |
133 | 140 | | |
134 | 141 | | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
135 | 148 | | |
136 | 149 | | |
137 | 150 | | |
| |||
346 | 359 | | |
347 | 360 | | |
348 | 361 | | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
349 | 389 | | |
350 | 390 | | |
351 | 391 | | |
352 | | - | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
353 | 396 | | |
354 | 397 | | |
355 | 398 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
66 | 70 | | |
67 | 71 | | |
68 | 72 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
0 commit comments