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 sends the job's payload to its chat. A cancelled ctx sends nothing. 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, spec.Payload.Text); err != nil { return fmt.Errorf("crondelivery: deliver job %q to %s:%s: %w", job.ID, spec.Target.Channel, 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 submits the job as work, titled by Detail or the job id. 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) )