5.7 KiB
02-runner — serial job runner in internal/batch
Add a new package internal/batch: the job lifecycle and the serial state machine. It knows how
to create a job, drain its targets one at a time, write each target's outcome, bump the lease,
handle cancellation, and enforce the caps and idempotency. It knows nothing about HTTP, the
guard, or the real fetch pipeline: the per-target work is an injected function, and persistence is
the *dcache.DB from task 01.
Context
internal/pdfpool is the wrong shape (in-memory, coalescing, forgets results). The runner is the
opposite: store-backed, persistent, one job = many distinct targets, results written to the store
as they complete. It is deliberately serial.
Public surface (exact names — the given test compiles against them)
package batch
type Options struct {
Mode string `json:"mode,omitempty"`
Question string `json:"question,omitempty"`
Pages string `json:"pages,omitempty"`
Grep string `json:"grep,omitempty"`
JQ string `json:"jq,omitempty"`
Context bool `json:"context,omitempty"`
Recover bool `json:"recover,omitempty"`
}
type Target struct {
URL string `json:"url"`
Options Options `json:"options,omitempty"`
}
// Result is a completed target's outcome. The runner JSON-encodes it into the
// store's target.result column and decodes it back in Status.
type Result struct {
Content string `json:"content"`
Meta map[string]any `json:"meta,omitempty"`
}
type FetchFunc func(ctx context.Context, t Target) (Result, error)
// Status views returned by Status (mapped from the store rows).
type Job struct {
ID string
Status string
Error string
TargetsTotal int
TargetsDone int
}
type TargetStatus struct {
Idx int
URL string
Status string
Result *Result
Error string
Hint string
}
type Config struct {
TTL time.Duration // 0 → 24h
TargetsPerJob int // 0 → 32
JobsPerCaller int // 0 → 1
JobsTotal int // 0 → 8
}
var (
ErrTooManyTargets = errors.New("too many targets")
ErrCallerBusy = errors.New("busy")
ErrBusy = errors.New("jobs busy")
)
func New(db *dcache.DB, fetch FetchFunc, cfg Config, log *slog.Logger) *Runner
func (r *Runner) Submit(ctx context.Context, caller, callerClass string, targets []Target) (id string, reused bool, err error)
func (r *Runner) Cancel(id string) bool
func (r *Runner) Status(id string) (Job, []TargetStatus, bool)
The runner depends on *dcache.DB (task 01) for persistence; the FetchFunc is the only injected
behavior. Zero Config fields fall back to the defaults above.
Rules
- Idempotency first.
Submitderivesidem_key = SHA256(caller + "\x00" + canonical(targets))wherecanonicalisjson.Marshalof the decoded targets (so field order and whitespace don't matter). If a job whose lease has not expired (BatchJobByIDem) exists for the same(caller, idem_key)— any status — return it withreused=trueand do not create a new job or enforce the caps. Idempotency is lease-scoped, so it is "reuse within TTL, fresh after". - Caps second. targets/job >
TargetsPerJob→ErrTooManyTargets; a live (queued/running) job already exists for the caller →ErrCallerBusy; live jobs total >=JobsTotal→ErrBusy. "Live" is by status, not lease — read fromLiveBatchJobs/LiveBatchJobsTotal. The typed errors map to422/429in the API layer. - Submit transitions to
runningsynchronously (before the drain goroutine starts) so a caller-busy check against a just-submitted job is deterministic. The job is createdqueuedthen immediatelyrunning;queuedis only ever persisted as the crash-window state thatMarkInFlightFailedreaps. - Serial drain. The worker iterates targets in
idxorder, one at a time, and does not start targeti+1untiliis written to the store. Each target'sFetchFuncresult or error is written immediately (JSON-encoded); a target failure does not stop the job. - Lease on completion. Each target write bumps the job lease (task 01); the first and last target therefore share one expiry.
- Cancellation.
Cancelflips the job tocancelledand signals the worker; the worker finishes the in-flight target, stores it, and stops before the next. Acancelledjob's completed targets stay readable until the lease lapses.Cancelreturns false for an unknown id or a job already in a terminal state. - Terminal statuses. When the last target is written: all
done→done; allfailed→failed; a mix →partial. Set exactly once. - No restart concept in the runner. It is created fresh each process start;
main(task 03) callsdcache.MarkInFlightFailed("restart", now)before constructing the runner. Completed targets on a restart-killed job stay readable; the job is never resumed. - No panic on input. Library code never panics: bad ids, missing targets, a
FetchFuncpanic is recovered into a target error and the job continues. The drain runs undercontext.WithoutCancelof a background context; only the per-job cancellation stops it.
Given
Copy _files/batch/runner_test.go → internal/batch/runner_test.go. It uses an in-memory dcache
plus a FetchFunc that records call order and can fail or panic. Asserts serial order (peak
in-flight 1), failure isolation (partial), idempotent resubmit (reused, no double-drain), the
three caps, cancel (in-flight stored, later targets pending), and panic recovery.
Gate
make gate green; go test ./internal/batch/ green under -race.