fix(schedule): keep monitors created while reconciliation reads the database

Reconciliation removed every monitor missing from its database read,
including ones created in the meantime; they stayed unscheduled until
the next run a minute later. Upserts now record when they happened and
reconciliation only prunes entries older than its read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 08:48:10 +01:00
1 parent a6126b353e
commit 4f57cafbdd
3 files changed
+54 -10

No files matched your search

+23 -7
View File
@@ -33,7 +33,10 @@ const (
const (
keyRegions = "sched:regions"
keyIntervals = "sched:intervals"
jobsPrefix = "sched:jobs:"
// keyTouched records when each monitor was last upserted, so a
// reconciliation based on an older database read leaves it alone.
keyTouched = "sched:touched"
jobsPrefix = "sched:jobs:"
// jobsTTL drops queues of instances that vanished together with every
// API replica that could have recovered them.
jobsTTL = 10 * time.Minute
@@ -74,6 +77,7 @@ func (s *Scheduler) Upsert(ctx context.Context, m Monitor, previousRegions []str
id := m.ID.String()
_, err := s.rdb.TxPipelined(ctx, func(p redis.Pipeliner) error {
p.HSet(ctx, keyIntervals, id, int64(m.Interval/time.Millisecond))
p.HSet(ctx, keyTouched, id, now)
keep := map[string]bool{}
for _, r := range m.Regions {
keep[r] = true
@@ -95,6 +99,7 @@ func (s *Scheduler) Upsert(ctx context.Context, m Monitor, previousRegions []str
func (s *Scheduler) Remove(ctx context.Context, id uuid.UUID, regions []string) error {
_, err := s.rdb.TxPipelined(ctx, func(p redis.Pipeliner) error {
p.HDel(ctx, keyIntervals, id.String())
p.HDel(ctx, keyTouched, id.String())
for _, r := range regions {
p.ZRem(ctx, keyDue(r), id.String())
}
@@ -103,10 +108,12 @@ func (s *Scheduler) Remove(ctx context.Context, id uuid.UUID, regions []string)
return err
}
// Reconcile makes Redis match want, the full set of enabled monitors. It
// repairs drift after a Redis failover or a missed update and is safe to run
// from several replicas.
func (s *Scheduler) Reconcile(ctx context.Context, want []Monitor) error {
// Reconcile makes Redis match want, the full set of enabled monitors as read
// from the database at asOf. It repairs drift after a Redis failover or a
// missed update and is safe to run from several replicas. Monitors upserted
// after asOf are kept even if want lacks them: they were created while the
// database was being read.
func (s *Scheduler) Reconcile(ctx context.Context, want []Monitor, asOf time.Time) error {
byRegion := map[string]map[string]bool{}
for _, m := range want {
if err := s.Upsert(ctx, m, nil); err != nil {
@@ -123,13 +130,22 @@ func (s *Scheduler) Reconcile(ctx context.Context, want []Monitor) error {
for _, m := range want {
wanted[m.ID.String()] = true
}
touched, err := s.rdb.HGetAll(ctx, keyTouched).Result()
if err != nil {
return err
}
recent := func(id string) bool {
ms, err := strconv.ParseInt(touched[id], 10, 64)
return err == nil && ms >= asOf.UnixMilli()
}
ids, err := s.rdb.HKeys(ctx, keyIntervals).Result()
if err != nil {
return err
}
for _, id := range ids {
if !wanted[id] {
if !wanted[id] && !recent(id) {
s.rdb.HDel(ctx, keyIntervals, id)
s.rdb.HDel(ctx, keyTouched, id)
}
}
regions, err := s.rdb.SMembers(ctx, keyRegions).Result()
@@ -142,7 +158,7 @@ func (s *Scheduler) Reconcile(ctx context.Context, want []Monitor) error {
return err
}
for _, id := range members {
if !byRegion[r][id] {
if !byRegion[r][id] && !recent(id) {
s.rdb.ZRem(ctx, keyDue(r), id)
}
}
+22 -1
View File
@@ -169,7 +169,7 @@ func TestRemoveAndReconcile(t *testing.T) {
_ = s.Upsert(ctx, keep, nil)
_ = s.Upsert(ctx, gone, nil)
if err := s.Reconcile(ctx, []Monitor{keep}); err != nil {
if err := s.Reconcile(ctx, []Monitor{keep}, s.now().Add(time.Millisecond)); err != nil {
t.Fatal(err)
}
mustDispatch(t, s)
@@ -203,3 +203,24 @@ func TestTakeReturnsWhenCancelled(t *testing.T) {
t.Errorf("Take() returned %v after cancellation, want within about a second", took)
}
}
// TestReconcileKeepsMonitorsCreatedDuringRead covers a monitor created after
// reconciliation read the database but before it pruned Redis.
func TestReconcileKeepsMonitorsCreatedDuringRead(t *testing.T) {
s, now := newTest(t)
ctx := context.Background()
in := uuid.New()
_ = s.Heartbeat(ctx, "eu", in)
asOf := *now
*now = now.Add(50 * time.Millisecond)
fresh := Monitor{ID: uuid.New(), Interval: time.Minute, Regions: []string{"eu"}}
_ = s.Upsert(ctx, fresh, nil)
if err := s.Reconcile(ctx, nil, asOf); err != nil {
t.Fatal(err)
}
mustDispatch(t, s)
if got := takeAll(t, s, in); len(got) != 1 || got[0].MonitorID != fresh.ID {
t.Errorf("jobs after Reconcile = %+v, want the freshly created monitor", got)
}
}