feat(archied): run daemon maintenance as built-in schedules

This commit is contained in:
sam committed 2026-10-26 00:51:53 +11:00
1 parent cc5789eb5f
commit 87ac7b693c
3 files changed
+36 -13

No files matched your search

+1 -2
View File
@@ -200,11 +200,10 @@ func Run() int { //nolint:cyclop,funlen // the composition root's setup sequence
if err := b.registerNATSRPC(); err != nil {
return 1
}
b.buildDaemon()
if err := b.setupScheduling(); err != nil {
return 1
}
b.buildDaemon()
b.setupDaemonRPC(ctx)
b.playbooks.Keep(ctx, playbookRefreshInterval)
b.workflowDefinitions.Keep(ctx, playbookRefreshInterval)
+27 -4
View File
@@ -55,6 +55,7 @@ type scheduleRuns interface {
ClaimDueRuns(ctx context.Context, now time.Time, stale time.Duration) ([]scheduling.Claim, error)
FinishScheduleRun(ctx context.Context, runID int64, result scheduling.RunResult, errText string) (int, error)
SetScheduleRunCapture(ctx context.Context, runID int64, captureID string) error
EnsureBuiltinSchedules(ctx context.Context, builtins []scheduling.Builtin) error
}
// setupScheduling builds the scheduler: claimed runs of a built-in schedule
@@ -69,7 +70,14 @@ func (b *boot) setupScheduling() error {
if err != nil {
return fmt.Errorf("scheduling: %w", err)
}
src := scheduleSource{runs: runs, stale: 2 * cfg.JobTimeout, log: b.log}
jobs := b.d.BuiltinJobs()
builtins := make(map[string]scheduling.Runner, len(jobs))
seeds := make([]scheduling.Builtin, 0, len(jobs))
for _, job := range jobs {
builtins[job.ID] = builtinJob(job.Run)
seeds = append(seeds, scheduling.Builtin{ID: job.ID, Name: job.Name, Interval: job.DefaultInterval})
}
src := &scheduleSource{runs: runs, stale: 2 * cfg.JobTimeout, log: b.log, builtins: seeds}
if alerts, ok := b.stateStore.(alert.Store); ok {
src.alerts = alert.Reporter{Raiser: alerts, Log: b.log, By: "archied"}
}
@@ -79,7 +87,7 @@ func (b *boot) setupScheduling() error {
Captures: captures, Retention: b.cfg.Capture.Retention.Std(), MaxEvents: b.cfg.Capture.MaxEvents,
}}
}
engine, err := scheduling.NewEngine(src, scheduleRunner{Fire: fire}, cfg)
engine, err := scheduling.NewEngine(src, scheduleRunner{Builtins: builtins, Fire: fire}, cfg)
if err != nil {
return fmt.Errorf("scheduling: build engine: %w", err)
}
@@ -95,9 +103,19 @@ type scheduleSource struct {
stale time.Duration
alerts alert.Reporter
log *slog.Logger
// builtins are seeded before the first claim, and again on a later tick
// if the State Store was unavailable.
builtins []scheduling.Builtin
seeded bool
}
func (s scheduleSource) Due(ctx context.Context, now time.Time) ([]scheduling.Job, error) {
func (s *scheduleSource) Due(ctx context.Context, now time.Time) ([]scheduling.Job, error) {
if !s.seeded {
if err := s.runs.EnsureBuiltinSchedules(ctx, s.builtins); err != nil {
return nil, fmt.Errorf("seed built-in schedules: %w", err)
}
s.seeded = true
}
claims, err := s.runs.ClaimDueRuns(ctx, now, s.stale)
if err != nil {
return nil, err
@@ -117,7 +135,7 @@ func (s scheduleSource) Due(ctx context.Context, now time.Time) ([]scheduling.Jo
return jobs, nil
}
func (s scheduleSource) Finish(ctx context.Context, job scheduling.Job, result scheduling.RunResult, runErr error) error {
func (s *scheduleSource) Finish(ctx context.Context, job scheduling.Job, result scheduling.RunResult, runErr error) error {
errText := ""
if runErr != nil {
errText = runErr.Error()
@@ -140,6 +158,11 @@ func (s scheduleSource) Finish(ctx context.Context, job scheduling.Job, result s
return nil
}
// builtinJob runs a daemon maintenance job as a schedule's run.
type builtinJob func(ctx context.Context) error
func (run builtinJob) Run(ctx context.Context, _ scheduling.Job) error { return run(ctx) }
// scheduleRunner sends a built-in schedule's run to its job and any other
// run to Fire.
type scheduleRunner struct {