Skip to content

Commit 6f9aeb9

Browse files
roberttwangclaude
andcommitted
Align first scheduled run with Tencent#1437 to avoid a merge conflict
Tencent#1437 fixes the same first-run bug in jobDue. Use its implementation (first timer run anchored at CreatedAt for both Cron and interval jobs) so either PR can merge first without conflicting in internal/schedule/service.go and service_test.go. Keep the Cron first-occurrence regression test. Co-Authored-By: Claude Code <noreply@anthropic.com>
1 parent 62dd267 commit 6f9aeb9

3 files changed

Lines changed: 24 additions & 31 deletions

File tree

‎internal/schedule/schedule_cases_test.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ func TestJobDueBranches(t *testing.T) {
1616
}{
1717
{domain.ScheduleJob{}, false},
1818
{domain.ScheduleJob{Enabled: true, LastStatus: "running"}, false},
19-
{domain.ScheduleJob{Enabled: true}, true},
19+
{domain.ScheduleJob{Enabled: true}, false},
20+
{domain.ScheduleJob{Enabled: true, CreatedAt: now.Add(-time.Hour + time.Millisecond), IntervalSec: 3600}, false},
21+
{domain.ScheduleJob{Enabled: true, CreatedAt: now.Add(-time.Hour), IntervalSec: 3600}, true},
22+
{domain.ScheduleJob{Enabled: true, CreatedAt: now.Add(-2 * time.Hour), LastRunAt: now.Add(-time.Minute), IntervalSec: 3600}, false},
23+
{domain.ScheduleJob{Enabled: true, CreatedAt: now.Add(-time.Minute), Cron: "0 9 * * 1"}, false},
24+
{domain.ScheduleJob{Enabled: true, CreatedAt: now.Add(-time.Minute), Cron: "0 20 * * *"}, true},
2025
{domain.ScheduleJob{Enabled: true, LastRunAt: now.Add(-31 * time.Second), IntervalSec: 1}, true},
2126
{domain.ScheduleJob{Enabled: true, LastRunAt: now.Add(-29 * time.Second), IntervalSec: 1}, false},
2227
{domain.ScheduleJob{Enabled: true, Cron: "bad", LastRunAt: now.Add(-time.Hour)}, false},

‎internal/schedule/service.go‎

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -195,30 +195,23 @@ func jobDue(job domain.ScheduleJob, now time.Time) bool {
195195
if job.LastStatus == "running" {
196196
return false
197197
}
198+
base := job.LastRunAt
199+
if base.IsZero() {
200+
// The first timer run follows the configured schedule, just like later runs.
201+
base = job.CreatedAt
202+
}
203+
if base.IsZero() {
204+
return false
205+
}
198206
if cron := strings.TrimSpace(job.Cron); cron != "" {
199-
after := job.LastRunAt
200-
if after.IsZero() {
201-
// A new or reconfigured job that has never run starts its clock
202-
// when saved. The explicit Run now action bypasses jobDue.
203-
after = job.UpdatedAt
204-
if after.IsZero() {
205-
after = job.CreatedAt
206-
}
207-
if after.IsZero() {
208-
return false
209-
}
210-
}
211-
next, err := cronexpr.Next(cron, after)
207+
next, err := cronexpr.Next(cron, base)
212208
return err == nil && !next.After(now)
213209
}
214210
interval := time.Duration(job.IntervalSec) * time.Second
215211
if interval < 30*time.Second {
216212
interval = 30 * time.Second
217213
}
218-
if job.LastRunAt.IsZero() {
219-
return true
220-
}
221-
return now.Sub(job.LastRunAt) >= interval
214+
return now.Sub(base) >= interval
222215
}
223216
func (s *Service) RunJob(id string) (domain.ScheduleJob, error) {
224217
return s.runJob(id, "timer")

‎internal/schedule/service_test.go‎

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,7 @@ func TestNewCronJobWaitsForScheduledTime(t *testing.T) {
321321
// 10:32 UTC is 18:32 in the scheduler's time zone.
322322
savedAt := time.Date(2026, 11, 26, 10, 32, 43, 0, time.UTC)
323323
dueAt := time.Date(2026, 11, 26, 10, 34, 0, 0, time.UTC)
324-
job := domain.ScheduleJob{
325-
Enabled: true, Cron: "34 18 26 11 *",
326-
CreatedAt: savedAt.Add(-time.Hour), UpdatedAt: savedAt,
327-
}
324+
job := domain.ScheduleJob{Enabled: true, Cron: "34 18 26 11 *", CreatedAt: savedAt}
328325
for _, now := range []time.Time{savedAt, dueAt.Add(-time.Second)} {
329326
if jobDue(job, now) {
330327
t.Fatalf("new job started early at %v", now)
@@ -333,10 +330,6 @@ func TestNewCronJobWaitsForScheduledTime(t *testing.T) {
333330
if !jobDue(job, dueAt) {
334331
t.Fatal("new job did not become due at its first occurrence")
335332
}
336-
job.UpdatedAt = time.Time{}
337-
if !jobDue(job, dueAt) {
338-
t.Fatal("older saved job did not use its creation time")
339-
}
340333
job.CreatedAt = time.Time{}
341334
if jobDue(job, dueAt) {
342335
t.Fatal("job without a persisted scheduling anchor should not fire")
@@ -777,8 +770,8 @@ func TestCronDropsOccurrencesDuringRun(t *testing.T) {
777770
start := time.Date(2026, 10, 12, 2, 0, 0, 0, time.UTC)
778771
finish := start.Add(150 * time.Minute)
779772
job := domain.ScheduleJob{ID: "job", Enabled: true, Cron: "0 * * * *", LastStatus: status, LastRunAt: start}
780-
other := domain.ScheduleJob{ID: "other", Enabled: true, Cron: "0 * * * *", LastRunAt: start}
781-
for _, v := range []domain.ScheduleJob{job, other} {
773+
neverRun := domain.ScheduleJob{ID: "other", Enabled: true, Cron: "0 * * * *"}
774+
for _, v := range []domain.ScheduleJob{job, neverRun} {
782775
if err := st.PutScheduleJob(v); err != nil {
783776
t.Fatal(err)
784777
}
@@ -793,8 +786,9 @@ func TestCronDropsOccurrencesDuringRun(t *testing.T) {
793786
if _, err := svc.claimJob(job.ID, start.Add(180*time.Minute)); err != nil {
794787
t.Fatalf("next cron must run: err=%v", err)
795788
}
796-
// One job's execution does not delay another due job.
797-
if _, err := svc.claimJob(other.ID, finish); err != nil {
789+
// Another job's run does not delay a job that never ran.
790+
other, _ := st.ScheduleJob(neverRun.ID)
791+
if _, err := svc.claimJob(neverRun.ID, other.CreatedAt.Add(time.Hour)); err != nil {
798792
t.Fatalf("other job must not wait for this one: err=%v", err)
799793
}
800794
got, _ := st.ScheduleJob(job.ID)
@@ -841,7 +835,8 @@ func TestTimerRunsAsyncAndSkipsOverlap(t *testing.T) {
841835
return "ok", nil
842836
})
843837
returned := make(chan struct{})
844-
go func() { svc.runDueJobsAt(time.Now().UTC().Add(time.Minute)); close(returned) }()
838+
stored, _ := st.ScheduleJob(job.ID)
839+
go func() { svc.runDueJobsAt(stored.CreatedAt.Add(time.Minute)); close(returned) }()
845840
select {
846841
case <-returned:
847842
case <-time.After(2 * time.Second):

0 commit comments

Comments
 (0)