ab6b2bb632
A schedule that cannot be parsed or claimed is logged and skipped instead of aborting the tick for every schedule. Claimed runs are admitted on their own goroutines, so a slow start (worktree creation) no longer makes the next due schedule look missed; lateness is measured at claim time. An invalid interval no longer divides by zero. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
101 lines
3.2 KiB
Go
101 lines
3.2 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"git.a-wan.net/alan/pipelines/internal/stage"
|
|
"git.a-wan.net/alan/pipelines/internal/summarise"
|
|
)
|
|
|
|
// One schedule whose stored due time cannot be parsed is logged and skipped;
|
|
// every other due schedule still runs on the same tick.
|
|
func TestScheduleTickSkipsAnUnreadableScheduleAndRunsTheRest(t *testing.T) {
|
|
ctx, s, r, pid := scheduleFixture(t)
|
|
now := time.Now()
|
|
broken := saveTestSchedule(t, ctx, s, pid, "broken", t.TempDir(), "1m", now)
|
|
good := saveTestSchedule(t, ctx, s, pid, "good", t.TempDir(), "1m", now)
|
|
db, err := sql.Open("sqlite", s.DatabasePath())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
if _, err := db.ExecContext(ctx, `UPDATE saved_schedules SET next_due='not a time' WHERE id=?`, broken.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
|
return stage.Result{Outcome: "success", Output: stage.Values{"markdown": "# Done"}}, nil
|
|
}
|
|
if err := r.scheduleTick(dueOf(t, good), false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r.scheduleAdmissions.Wait()
|
|
r.wg.Wait()
|
|
runs, err := s.ListRuns(ctx)
|
|
if err != nil || len(runs) != 1 || runs[0].ScheduleID != good.ID {
|
|
t.Fatalf("runs = %+v %v", runs, err)
|
|
}
|
|
}
|
|
|
|
// A slow admission (title generation, worktree creation) no longer holds the
|
|
// scheduler: the tick returns at once and a schedule due a moment later is
|
|
// claimed and started, not recorded as missed.
|
|
func TestSlowScheduledAdmissionDoesNotMissTheNextSchedule(t *testing.T) {
|
|
ctx, s, r, pid := scheduleFixture(t)
|
|
now := time.Now()
|
|
save := func(prompt string) *protocol.SavedSchedule {
|
|
item, err := s.SaveSchedule(ctx, protocol.SavedSchedule{Name: prompt, PipelineID: pid, Version: 1, Enabled: true, Workspace: t.TempDir(), Input: map[string]any{"prompt": prompt}, Rule: protocol.Schedule{Every: "1m"}, NotifyMode: "off"}, now)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return item
|
|
}
|
|
a, b := save("first"), save("second")
|
|
release := make(chan struct{})
|
|
titling := make(chan string, 2)
|
|
r.title = func(_ context.Context, prompt string) summarise.Title {
|
|
titling <- prompt
|
|
if prompt == "first" {
|
|
<-release
|
|
}
|
|
return summarise.Title{Text: prompt}
|
|
}
|
|
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
|
return stage.Result{Outcome: "success", Output: stage.Values{"markdown": "# Done"}}, nil
|
|
}
|
|
at := dueOf(t, a)
|
|
if dueB := dueOf(t, b); dueB.After(at) {
|
|
at = dueB
|
|
}
|
|
// Both are due on this tick; the first one's admission blocks.
|
|
returned := make(chan error, 1)
|
|
go func() { returned <- r.scheduleTick(at, false) }()
|
|
select {
|
|
case err := <-returned:
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("scheduler tick blocked on a slow admission")
|
|
}
|
|
seen := map[string]bool{}
|
|
for len(seen) < 2 {
|
|
select {
|
|
case got := <-titling:
|
|
seen[got] = true
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatalf("second schedule not admitted while the first was slow: %v", seen)
|
|
}
|
|
}
|
|
close(release)
|
|
r.scheduleAdmissions.Wait()
|
|
r.wg.Wait()
|
|
runs, err := s.ListRuns(ctx)
|
|
if err != nil || len(runs) != 2 {
|
|
t.Fatalf("runs = %+v %v", runs, err)
|
|
}
|
|
}
|