fix(scheduler): make Schedule() actually fire job only once#170
Open
marcus wants to merge 2 commits into
Open
Conversation
Nightshift-Task: release-notes Nightshift-Ref: https://github.com/marcus/nightshift
Schedule(at, job) was documented as a one-time job, but the closure it added to the scheduler ran on every tick after `at`. Wrap the call in sync.Once so the job fires exactly once. Use `!time.Now().Before(at)` so the boundary tick (exactly at `at`) also fires. Nightshift-Task: bug-finder Nightshift-Ref: https://github.com/marcus/nightshift Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Scheduler.Schedule(at, job)was documented as a one-time job, but its closure ran on every scheduler tick afteratwas reached. With a typical 5-minute interval, a job scheduled for 14:00 would fire at 14:00, 14:05, 14:10, … indefinitely.The fix wraps the inner call in
sync.Onceso the job fires exactly once. Also switches fromtime.Now().After(at)to!time.Now().Before(at)so the boundary tick (exactly atat) is included.Bug detail
Before:
After: a
sync.Oncearound the call gives one-shot semantics. The job stays in the slice (harmless — closures cost nothing onceOncehas fired).Audit context
Ran
go vet,staticcheck, andgolangci-lintagainst the repo — all clean. Manual review focused on resource leaks, concurrency, error handling, and time/scheduling. Inspectedinternal/orchestrator,internal/budget,internal/state,internal/db,internal/snapshots,internal/providers, andinternal/scheduler. The only high-confidence concrete bug found was this one. (Two other suspected issues —ParseMetadataBlockindex arithmetic andCopilot.GetResetTime("weekly")— were verified and turned out to be intentional / correct.)Test plan
go test ./internal/scheduler/...passesTestSchedule_RunsExactlyOnce: simulates 5 ticks afterat, asserts job ran 1×.TestSchedule_NotBeforeTargetTime: schedules for 1h in the future, asserts job runs 0× across 3 ticks.go test ./...passesgo vet ./...,gofmt,go build ./...all clean (also enforced by pre-commit hook)🤖 Generated with Claude Code
Automated by nightshift