Files
archie/internal/app/statestore/schedules.go
T
sam cc5789eb5f feat(scheduling): schedules and runs in the State Store with grid timing and run history
Schedules leave the schedules control-plane resource for State Store tables
behind a ScheduleService contract. A run is claimed per schedule slot, missed
slots fire once, the next run stays on the schedule's grid, cron is read in the
schedule-settings time zone, and failed runs are recorded and advance. Three
failures in a row raise a schedule.failing alert. A fired run records a
schedule-fired capture. Run history is pruned by age and count.

Removes the chat and spawn-task runners, the dangerous-payload guard and the
old schedules settings page.
2026-10-26 00:49:24 +11:00

63 lines
2.1 KiB
Go

package statestore
import (
"context"
"time"
"github.com/samcharles93/archie-core/internal/domain/scheduling"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanestore"
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
)
// scheduleRunSweepInterval is how often run history retention is applied.
const scheduleRunSweepInterval = time.Hour
// scheduleDeps serves schedules from the task store, with the instance's
// schedule settings read afresh on every call.
func (b *server) scheduleDeps(deps *staterpc.Deps) {
if schedules, ok := b.st.(scheduling.Store); ok {
deps.Schedules = schedules
}
if runs, ok := b.st.(scheduling.RunStore); ok {
deps.ScheduleRuns = runs
}
if resources, ok := b.st.(auditRetentionResources); ok {
deps.ScheduleSettings = func(ctx context.Context) (scheduling.Settings, error) {
resource, err := resources.Resource(ctx, storecontract.DefaultOrgID, controlplanestore.ScheduleSettingsKind)
if err != nil {
return scheduling.Settings{}, err
}
return controlplanestore.ScheduleSettings(resource.Value)
}
}
}
// sweepScheduleRuns applies run history retention until ctx ends. An
// unreadable setting skips the tick rather than pruning by a default.
func (b *server) sweepScheduleRuns(ctx context.Context, deps staterpc.Deps) {
if deps.ScheduleRuns == nil || deps.ScheduleSettings == nil {
return
}
ticker := time.NewTicker(scheduleRunSweepInterval)
defer ticker.Stop()
for {
if settings, err := deps.ScheduleSettings(ctx); err != nil {
if ctx.Err() == nil {
b.log.WarnContext(ctx, "schedule run retention not applied: setting unreadable", "err", err)
}
} else if n, err := deps.ScheduleRuns.PruneRuns(ctx, time.Now().Add(-settings.RunRetention), settings.RunRetentionCount); err != nil {
if ctx.Err() == nil {
b.log.WarnContext(ctx, "prune schedule runs", "err", err)
}
} else if n > 0 {
b.log.InfoContext(ctx, "pruned schedule runs", "rows", n)
}
select {
case <-ctx.Done():
return
case <-ticker.C:
}
}
}