feat(archied): wire the scheduling ticker engine at boot
schedulingConfig translated [scheduling] config into a domain EngineConfig but nothing ever called it in production: the whole scheduling/cron family (ticker engine, cronstore, crondelivery) was built and tested but reachable from no binary, so an operator setting scheduling.interval etc got a silently inert section. setupScheduling opens a cronstore.Store as the engine's JobSource, builds a crondelivery.Router with a workflow-kind runner backed by the same non-forge gateway.TaskCreator chat's /spawn already uses, and constructs the scheduling.Engine from schedulingConfig. startServices starts it alongside every other subsystem; Stop is registered via addCleanup on the same Background-timeout pattern as the curator runtime. No chat task creator configured leaves the engine unstarted rather than running with no reachable job kind -- the "chat" delivery kind stays unmapped for the same reason (no unified cross-channel outbound sender exists at this composition root yet).
This commit is contained in:
4 files changed
+210
No files matched your search
@@ -35,6 +35,7 @@ import (
|
||||
domainembedding "github.com/samcharles93/archie-core/internal/domain/embedding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/health"
|
||||
domainmemory "github.com/samcharles93/archie-core/internal/domain/memory"
|
||||
"github.com/samcharles93/archie-core/internal/domain/scheduling"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/skillbuild"
|
||||
@@ -195,6 +196,10 @@ type boot struct {
|
||||
guardrails *tools.GuardrailEngine
|
||||
providerRegistry *toolprovider.Registry
|
||||
d *daemon.Daemon
|
||||
// schedulingEngine is the cron/scheduling ticker engine (setupScheduling).
|
||||
// Nil when no chat task creator is configured, in which case startServices
|
||||
// leaves it unstarted rather than running with no reachable job kind.
|
||||
schedulingEngine *scheduling.Engine
|
||||
|
||||
// kindWorkflows/labelWorkflows are the resolved kind/label -> workflow
|
||||
// routing bindings loaded by loadWorkflowRouting. They are handed to the
|
||||
@@ -1590,6 +1595,11 @@ func (b *boot) startServices(ctx context.Context) error {
|
||||
if err := b.curatorRuntime.Start(ctx); err != nil {
|
||||
log.Error("curator runtime startup", "err", err)
|
||||
}
|
||||
if b.schedulingEngine != nil {
|
||||
if err := b.schedulingEngine.Start(ctx); err != nil {
|
||||
log.Error("scheduling engine startup", "err", err)
|
||||
}
|
||||
}
|
||||
for _, startGateway := range b.startGateways {
|
||||
startGateway()
|
||||
}
|
||||
|
||||
@@ -430,6 +430,10 @@ func Run() int { //nolint:cyclop // the composition root's setup sequence is del
|
||||
}
|
||||
b.setupCurators(ctx)
|
||||
|
||||
if err := b.setupScheduling(); err != nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
b.setupGuardrails()
|
||||
if err := b.registerTools(); err != nil {
|
||||
return 1
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/scheduling"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/crondelivery"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/cronevents"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/cronstore"
|
||||
)
|
||||
|
||||
// schedulingConfig translates external input into the domain-owned engine
|
||||
@@ -36,3 +43,86 @@ func schedulingConfig(input configuration.SchedulingInput, bus *events.Bus) (sch
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// setupScheduling builds the cron/scheduling composition site: a job store,
|
||||
// a delivery router over the kind runners this composition root can back
|
||||
// with a real production dependency, and the ticker engine itself. It does
|
||||
// not start the engine -- startServices does that alongside every other
|
||||
// subsystem, and the corresponding Stop is registered here via addCleanup so
|
||||
// shutdown ordering matches every other daemon-lifecycle subsystem.
|
||||
//
|
||||
// Only the "workflow" kind is wired: it rides gateway.TaskCreator, the same
|
||||
// non-forge-backed task creation path chat's /spawn uses, which is the one
|
||||
// production capability here that does not require inventing a channel
|
||||
// courier or a synthetic forge owner/repo. A deployment with no chat task
|
||||
// creator configured (no [chat] identity profiles) leaves the engine
|
||||
// unstarted rather than running with no reachable job kind.
|
||||
func (b *boot) setupScheduling() error {
|
||||
cfg, err := schedulingConfig(b.doc.Scheduling, b.bus)
|
||||
if err != nil {
|
||||
return fmt.Errorf("scheduling: %w", err)
|
||||
}
|
||||
|
||||
if b.chatTasks == nil {
|
||||
b.log.Warn("scheduling: no chat task creator configured; ticker engine not started")
|
||||
return nil
|
||||
}
|
||||
|
||||
storePath := filepath.Join(filepath.Dir(b.cfg.DBPath), "cron", "jobs.json")
|
||||
store, err := cronstore.Open(storePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("scheduling: open job store: %w", err)
|
||||
}
|
||||
b.addCleanup(func() {
|
||||
if err := store.Close(); err != nil {
|
||||
b.log.Error("scheduling store close", "err", err)
|
||||
}
|
||||
})
|
||||
|
||||
workflowRunner, err := crondelivery.NewWorkflowTask(store, spawnTaskSubmitter{creator: b.chatTasks, identity: b.defaultChatIdentity})
|
||||
if err != nil {
|
||||
return fmt.Errorf("scheduling: build workflow runner: %w", err)
|
||||
}
|
||||
router, err := crondelivery.NewRouter(store, map[string]scheduling.Runner{cronstore.KindWorkflow: workflowRunner}, cfg.Events)
|
||||
if err != nil {
|
||||
return fmt.Errorf("scheduling: build router: %w", err)
|
||||
}
|
||||
engine, err := scheduling.NewEngine(store, router, cfg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("scheduling: build engine: %w", err)
|
||||
}
|
||||
|
||||
b.schedulingEngine = engine
|
||||
b.addCleanup(shutdownSchedulingEngine(engine, b.log))
|
||||
return nil
|
||||
}
|
||||
|
||||
// shutdownSchedulingEngine returns a cleanup that stops the ticker engine.
|
||||
// The stop path derives its own timeout budget from Background rather than
|
||||
// the boot context, which is already cancelled by the time shutdown runs --
|
||||
// the same reasoning as shutdownCuratorRuntime.
|
||||
//
|
||||
//nolint:contextcheck
|
||||
func shutdownSchedulingEngine(e *scheduling.Engine, log *slog.Logger) func() {
|
||||
return func() {
|
||||
stopCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := e.Stop(stopCtx); err != nil {
|
||||
log.Error("scheduling engine shutdown", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// spawnTaskSubmitter adapts gateway.TaskCreator to crondelivery.TaskSubmitter.
|
||||
// The job's own id is only a dispatch key to the ticker engine, not an archie
|
||||
// identity, so it is dropped rather than threaded into the spawn request;
|
||||
// identity is fixed at construction to the daemon's default chat identity.
|
||||
type spawnTaskSubmitter struct {
|
||||
creator gateway.TaskCreator
|
||||
identity string
|
||||
}
|
||||
|
||||
func (s spawnTaskSubmitter) Submit(ctx context.Context, _, title, body string) error {
|
||||
_, err := s.creator.CreateTask(ctx, gateway.SpawnRequest{Title: title, Body: body, Identity: s.identity})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
)
|
||||
|
||||
// fakeTaskCreator records CreateTask calls so a test can assert the
|
||||
// scheduling engine's workflow runner reaches a real gateway.TaskCreator
|
||||
// rather than a no-op.
|
||||
type fakeTaskCreator struct {
|
||||
calls []gateway.SpawnRequest
|
||||
}
|
||||
|
||||
func (f *fakeTaskCreator) CreateTask(_ context.Context, req gateway.SpawnRequest) (int64, error) {
|
||||
f.calls = append(f.calls, req)
|
||||
return int64(len(f.calls)), nil
|
||||
}
|
||||
|
||||
func newTestBoot(t *testing.T, tasks gateway.TaskCreator) *boot {
|
||||
t.Helper()
|
||||
bus := events.NewBus()
|
||||
t.Cleanup(bus.Close)
|
||||
return &boot{
|
||||
log: slog.New(slog.NewTextHandler(os.Stderr, nil)),
|
||||
bus: bus,
|
||||
doc: &configuration.Document{},
|
||||
cfg: config.Config{DBPath: filepath.Join(t.TempDir(), "archie.db")},
|
||||
chatTasks: tasks,
|
||||
defaultChatIdentity: "primary",
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetupSchedulingConstructsEngine is the red case for app-archied-2 /
|
||||
// scheduling-1 / config-2: schedulingConfig translated a valid
|
||||
// [scheduling] input but nothing at boot ever constructed the ticker
|
||||
// engine, so the whole scheduling/cron family was reachable from no
|
||||
// binary. setupScheduling is the composition site; this asserts it
|
||||
// actually produces a running-capable engine wired to a real
|
||||
// gateway.TaskCreator, not just a translated config struct.
|
||||
func TestSetupSchedulingConstructsEngine(t *testing.T) {
|
||||
creator := &fakeTaskCreator{}
|
||||
b := newTestBoot(t, creator)
|
||||
|
||||
if err := b.setupScheduling(); err != nil {
|
||||
t.Fatalf("setupScheduling() error = %v", err)
|
||||
}
|
||||
if b.schedulingEngine == nil {
|
||||
t.Fatal("setupScheduling() left b.schedulingEngine nil; the ticker engine was never constructed")
|
||||
}
|
||||
if len(b.cleanups) == 0 {
|
||||
t.Fatal("setupScheduling() registered no cleanup; engine/store would leak at shutdown")
|
||||
}
|
||||
|
||||
if err := b.schedulingEngine.Start(t.Context()); err != nil {
|
||||
t.Fatalf("schedulingEngine.Start() error = %v", err)
|
||||
}
|
||||
b.cleanup()
|
||||
}
|
||||
|
||||
// TestSetupSchedulingWorkflowRunnerReachesTaskCreator proves the "workflow"
|
||||
// kind runner registered on the router is backed by the real
|
||||
// gateway.TaskCreator this composition root already has, not a stub.
|
||||
func TestSetupSchedulingWorkflowRunnerReachesTaskCreator(t *testing.T) {
|
||||
creator := &fakeTaskCreator{}
|
||||
b := newTestBoot(t, creator)
|
||||
|
||||
if err := b.setupScheduling(); err != nil {
|
||||
t.Fatalf("setupScheduling() error = %v", err)
|
||||
}
|
||||
t.Cleanup(b.cleanup)
|
||||
|
||||
submitter := spawnTaskSubmitter{creator: b.chatTasks, identity: b.defaultChatIdentity}
|
||||
if err := submitter.Submit(context.Background(), "job-1", "title", "body"); err != nil {
|
||||
t.Fatalf("Submit() error = %v", err)
|
||||
}
|
||||
if len(creator.calls) != 1 {
|
||||
t.Fatalf("CreateTask called %d times, want 1", len(creator.calls))
|
||||
}
|
||||
if got := creator.calls[0]; got.Title != "title" || got.Body != "body" || got.Identity != "primary" {
|
||||
t.Errorf("CreateTask request = %+v, want Title=title Body=body Identity=primary", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetupSchedulingWithoutTaskCreatorLeavesEngineNil documents the one
|
||||
// deliberate degrade: no chat task creator configured means no runner can
|
||||
// honestly back the "workflow" kind, so the engine is left unstarted rather
|
||||
// than built with no reachable job kind.
|
||||
func TestSetupSchedulingWithoutTaskCreatorLeavesEngineNil(t *testing.T) {
|
||||
b := newTestBoot(t, nil)
|
||||
|
||||
if err := b.setupScheduling(); err != nil {
|
||||
t.Fatalf("setupScheduling() error = %v", err)
|
||||
}
|
||||
if b.schedulingEngine != nil {
|
||||
t.Fatal("setupScheduling() built an engine with no chat task creator configured")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user