Files
archie/internal/infrastructure/crondelivery/runner.go
T
sam 82b28cd7f4 feat(crondelivery): add chat, workflow and routing cron runners
The ticker engine had no work to dispatch. Add the delivery half as a new
infrastructure package with three scheduling.Runner implementations:

  - ChatCourier sends a job's payload text to its target chat.
  - WorkflowTask submits a job as a unit of work.
  - Router dispatches on the job's kind and is the single runner the engine
    is given, so the engine stays unaware that more than one delivery exists.

Nothing here talks to a channel, a broker or a forge. The outbound send is
an injected Courier function and work intake is a narrow TaskSubmitter
interface, so which transport carries a message stays the deployment's
decision rather than the cron's, and the package is testable without a
network.

An unrecognised kind is refused at the gate, not failed: it emits
KindJobError with phase "dispatch" in the engine's own data shape and
returns nil, so a misconfigured job is visible without being reported as a
job that tried and broke. A nil sink still refuses, just unobservably.

Every runner returns the context error on a cancelled run and produces no
side effect, so the engine's bounded shutdown drains rather than waiting
out the per-run timeout.

Tests exercise a real cronstore on a temp path, including the epic's
headline daily-status scenario end to end from a persisted due job through
the router to the delivered message.
2026-09-14 09:54:41 +00:00

100 lines
3.1 KiB
Go

package crondelivery
import (
"context"
"errors"
"fmt"
"strings"
"github.com/samcharles93/archie-core/internal/domain/scheduling"
)
// ChatCourier delivers a job's payload text to a chat. It is the runner behind
// the "chat" kind and the epic's headline case: a daily status summary, which
// the blueprints slice will parameterise.
type ChatCourier struct {
specs SpecLookup
courier Courier
}
// NewChatCourier builds the chat runner over a spec lookup and the courier the
// deployment supplies.
func NewChatCourier(specs SpecLookup, courier Courier) (*ChatCourier, error) {
if specs == nil {
return nil, errors.New("crondelivery: spec lookup must not be nil")
}
if courier == nil {
return nil, errors.New("crondelivery: courier must not be nil")
}
return &ChatCourier{specs: specs, courier: courier}, nil
}
// Run hydrates the job's spec and sends its payload text to its target chat.
//
// The pre-check matters as much as the error wrap: a cancelled run must produce
// no side effect, because the engine cancels on shutdown and a message sent
// after that is one nobody asked for. The courier's own ctx handling covers
// cancellation that arrives mid-send.
func (c *ChatCourier) Run(ctx context.Context, job scheduling.Job) error {
if err := ctx.Err(); err != nil {
return err
}
spec, err := hydrate(ctx, c.specs, job)
if err != nil {
return err
}
if err := c.courier(ctx, spec.Target.ChatID, spec.Payload.Text); err != nil {
return fmt.Errorf("crondelivery: deliver job %q to %q: %w", job.ID, spec.Target.ChatID, err)
}
return nil
}
// WorkflowTask submits a job as a unit of work. It is the runner behind the
// "workflow" kind, so a scheduled job reaches the same task lifecycle —
// scheduling, retries, gates, PR opening — that every other intake path feeds.
type WorkflowTask struct {
specs SpecLookup
submitter TaskSubmitter
}
// NewWorkflowTask builds the workflow runner over a spec lookup and the
// submitter the deployment supplies.
func NewWorkflowTask(specs SpecLookup, submitter TaskSubmitter) (*WorkflowTask, error) {
if specs == nil {
return nil, errors.New("crondelivery: spec lookup must not be nil")
}
if submitter == nil {
return nil, errors.New("crondelivery: task submitter must not be nil")
}
return &WorkflowTask{specs: specs, submitter: submitter}, nil
}
// Run hydrates the job's spec and submits it as work.
//
// The title falls back to the job id when Detail is empty: a submitted task is
// how a human finds the job that created it, and an untitled one is
// unfindable.
func (w *WorkflowTask) Run(ctx context.Context, job scheduling.Job) error {
if err := ctx.Err(); err != nil {
return err
}
spec, err := hydrate(ctx, w.specs, job)
if err != nil {
return err
}
title := strings.TrimSpace(spec.Detail)
if title == "" {
title = job.ID
}
if err := w.submitter.Submit(ctx, job.ID, title, spec.Payload.Text); err != nil {
return fmt.Errorf("crondelivery: submit job %q: %w", job.ID, err)
}
return nil
}
// compile-time checks: the two deliveries are the engine's contract.
var (
_ scheduling.Runner = (*ChatCourier)(nil)
_ scheduling.Runner = (*WorkflowTask)(nil)
)