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:
1 parent
a6126b353e
commit
4f57cafbdd
3 files changed
+54
-10
No files matched your search
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user