feat(archied): run daemon maintenance as built-in schedules
This commit is contained in:
1 parent
b701f60a99
commit
fff689312d
3 files changed
+36
-13
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -448,6 +448,12 @@ func (d *Daemon) pollIssuesWithConfig(ctx context.Context, fg forge.Forge, cfg c
|
||||
func (d *Daemon) RunOnce(ctx context.Context) {
|
||||
defer d.WaitForTasks()
|
||||
d.watchCallWaits(ctx)
|
||||
// One pass has no scheduler, so it runs the maintenance jobs itself.
|
||||
for _, job := range d.BuiltinJobs() {
|
||||
if err := job.Run(ctx); err != nil {
|
||||
d.Log.WarnContext(ctx, "built-in job failed", "job", job.ID, "err", err)
|
||||
}
|
||||
}
|
||||
d.Cycle(ctx)
|
||||
if d.Tasks == nil {
|
||||
return
|
||||
@@ -465,15 +471,10 @@ func (d *Daemon) RunOnce(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// Cycle is one poll pass: run every built-in job, then turn captured events
|
||||
// and review reactions into tasks. Draining queued tasks is the caller's job
|
||||
// Cycle is one poll pass: turn captured events and review reactions into
|
||||
// tasks; maintenance runs as built-in schedules. Draining queued tasks is the caller's job
|
||||
// (drainLoop in Run, drainNATS in RunOnce) so there is one drainer.
|
||||
func (d *Daemon) Cycle(ctx context.Context) {
|
||||
for _, job := range d.BuiltinJobs() {
|
||||
if err := job.Run(ctx); err != nil {
|
||||
d.Log.WarnContext(ctx, "built-in job failed", "job", job.ID, "err", err)
|
||||
}
|
||||
}
|
||||
d.dispatchBindings(ctx)
|
||||
d.drainReactions(ctx)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user