|
| 1 | +# AGENTS.md |
| 2 | + |
| 3 | +## Package Overview |
| 4 | + |
| 5 | +`go-coldbrew/workers` is a worker lifecycle library built on [thejerf/suture](https://github.com/thejerf/suture). It manages background goroutines with panic recovery, configurable restart, tracing, and structured shutdown. |
| 6 | + |
| 7 | +## Build & Test |
| 8 | + |
| 9 | +```bash |
| 10 | +make build # go build ./... |
| 11 | +make test # go test -race ./... |
| 12 | +make lint # golangci-lint + govulncheck |
| 13 | +make bench # benchmarks |
| 14 | +make doc # regenerate README.md via gomarkdoc |
| 15 | +``` |
| 16 | + |
| 17 | +## Architecture |
| 18 | + |
| 19 | +Every worker runs inside its own suture supervisor subtree: |
| 20 | + |
| 21 | +``` |
| 22 | +Root Supervisor (created by Run) |
| 23 | +├── Worker-A supervisor |
| 24 | +│ ├── Worker-A run func |
| 25 | +│ ├── Child-A1 supervisor (added via ctx.Add) |
| 26 | +│ │ └── Child-A1 run func |
| 27 | +│ └── Child-A2 supervisor |
| 28 | +│ └── Child-A2 run func |
| 29 | +└── Worker-B supervisor |
| 30 | + └── Worker-B run func |
| 31 | +``` |
| 32 | + |
| 33 | +Key properties: |
| 34 | +- **Scoped lifecycle**: when a parent stops, all children stop |
| 35 | +- **Independent restart**: each worker restarts independently via suture |
| 36 | +- **Panic recovery**: suture catches panics and converts to errors |
| 37 | +- **Backoff**: configurable exponential backoff with jitter on restart |
| 38 | +- **Tracing**: each worker execution gets an OTEL span via `go-coldbrew/tracing` |
| 39 | + |
| 40 | +## Key Types |
| 41 | + |
| 42 | +- `Worker` — struct with builder pattern (`NewWorker().WithRestart().Every()`) |
| 43 | +- `WorkerContext` — extends `context.Context` with `Name()`, `Attempt()`, `Add()`, `Remove()`, `Children()` |
| 44 | +- `Run(ctx, []*Worker) error` — starts all workers, blocks until ctx cancelled |
| 45 | +- `RunWorker(ctx, *Worker)` — runs a single worker |
| 46 | + |
| 47 | +## Helpers |
| 48 | + |
| 49 | +- `EveryInterval(d, fn)` — periodic ticker loop |
| 50 | +- `ChannelWorker[T](ch, fn)` — consume from channel |
| 51 | +- `BatchChannelWorker[T](ch, maxSize, maxDelay, fn)` — batch with size/timer flush |
| 52 | + |
| 53 | +## Rules |
| 54 | + |
| 55 | +- Always run `make doc` after changing exported APIs or docstrings |
| 56 | +- Always run `make test` and `make lint` before committing |
| 57 | +- Don't add `replace` directives to go.mod |
0 commit comments