fix: anchor floor on re-enable; breaker on scheduled panic; propagate next-run err; reject negative interval; close phantom runs on startup

This commit is contained in:
2026-07-03 13:31:58 +07:00
parent e8acab6920
commit 64bcb1d60b
7 changed files with 55 additions and 10 deletions
+7
View File
@@ -6,6 +6,8 @@ import "context"
// process died mid-run (crash, container restart). A fresh process has no
// in-flight goroutines, so any persisted "running" is stale and would otherwise
// wedge the task (the run-guard refuses to start, and accounts can't be edited).
// It also closes any run rows left stuck in "running" (finished_at NULL), which
// would otherwise surface as perpetually "running" in the run-log modal.
// Returns how many task and account rows were reset.
func (s *Store) ResetRunningOnStartup(ctx context.Context) (tasks int64, accounts int64, err error) {
ct, err := s.Pool.Exec(ctx, `UPDATE accounts SET status='idle' WHERE status='running'`)
@@ -18,6 +20,11 @@ func (s *Store) ResetRunningOnStartup(ctx context.Context) (tasks int64, account
return 0, accounts, err
}
tasks = ct.RowsAffected()
_, err = s.Pool.Exec(ctx,
`UPDATE runs SET status='error', finished_at=now() WHERE finished_at IS NULL`)
if err != nil {
return tasks, accounts, err
}
return tasks, accounts, nil
}
+14
View File
@@ -15,6 +15,7 @@ func TestResetRunningOnStartup(t *testing.T) {
// simulate a crash mid-run
_ = s.SetTaskStatus(ctx, taskID, "running")
_ = s.SetAccountStatus(ctx, accID, "running")
runID, _ := s.CreateRun(ctx, taskID, "manual") // phantom run: never finished
tn, an, err := s.ResetRunningOnStartup(ctx)
if err != nil {
@@ -31,6 +32,19 @@ func TestResetRunningOnStartup(t *testing.T) {
if accs[0].Status == "running" {
t.Fatal("account still running after reset")
}
runs, _ := s.ListRunsByTask(ctx, taskID)
var found bool
for _, r := range runs {
if r.ID == runID {
found = true
if r.Status == "running" || r.FinishedAt == nil {
t.Fatalf("phantom run %d still running: status=%s finished_at=%v", runID, r.Status, r.FinishedAt)
}
}
}
if !found {
t.Fatalf("run %d not found", runID)
}
}
func TestClearStuckAccountAndReconcile(t *testing.T) {
+2
View File
@@ -56,6 +56,8 @@ func (s *Store) ListRunsByTask(ctx context.Context, taskID int64) ([]Run, error)
// LastFinishedRunAt returns the most recent finished run's timestamp, or nil if
// the task has never completed a run — the baseline for the next scheduled run.
// The same max(finished_at) rule is also inlined in ListSchedulableTasks's
// subquery — keep the two in sync if this changes.
func (s *Store) LastFinishedRunAt(ctx context.Context, taskID int64) (*time.Time, error) {
var t *time.Time
err := s.Pool.QueryRow(ctx,