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
+20 -9
View File
@@ -53,7 +53,12 @@ func (s *Server) handleGetTask(w http.ResponseWriter, r *http.Request) {
for _, a := range accs {
views = append(views, accountDTO(a))
}
writeJSON(w, http.StatusOK, map[string]any{"task": s.taskViewFor(r.Context(), task), "accounts": views})
tv, err := s.taskViewFor(r.Context(), task)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
writeJSON(w, http.StatusOK, map[string]any{"task": tv, "accounts": views})
}
// taskView augments the stored task with the server-computed next scheduled run
@@ -63,15 +68,17 @@ type taskView struct {
NextRunAt *string `json:"next_run_at"`
}
func (s *Server) taskViewFor(ctx context.Context, t store.Task) taskView {
var nextRunAt *string
if t.ScheduleIntervalSeconds > 0 && !t.Broken && t.Status != "running" && t.ScheduleAnchor != nil {
last, _ := s.store.LastFinishedRunAt(ctx, t.ID)
n := scheduler.NextRun(time.Duration(t.ScheduleIntervalSeconds)*time.Second, *t.ScheduleAnchor, last)
str := n.UTC().Format(time.RFC3339)
nextRunAt = &str
func (s *Server) taskViewFor(ctx context.Context, t store.Task) (taskView, error) {
if t.ScheduleIntervalSeconds <= 0 || t.Broken || t.Status == "running" || t.ScheduleAnchor == nil {
return taskView{Task: t}, nil
}
return taskView{Task: t, NextRunAt: nextRunAt}
last, err := s.store.LastFinishedRunAt(ctx, t.ID)
if err != nil {
return taskView{}, err
}
n := scheduler.NextRun(time.Duration(t.ScheduleIntervalSeconds)*time.Second, *t.ScheduleAnchor, last)
str := n.UTC().Format(time.RFC3339)
return taskView{Task: t, NextRunAt: &str}, nil
}
// handleSetSchedule enables/changes/disables a task's recurring schedule.
@@ -89,6 +96,10 @@ func (s *Server) handleSetSchedule(w http.ResponseWriter, r *http.Request) {
http.Error(w, "bad json", http.StatusBadRequest)
return
}
if body.IntervalSeconds < 0 {
http.Error(w, "interval_seconds must be >= 0", http.StatusBadRequest)
return
}
if body.IntervalSeconds > 0 {
accs, err := s.store.ListAccountsByTask(r.Context(), taskID)
if err != nil {