Skip to content

Commit

Permalink
Fix notifier/scheduler handling of integer/epoch serialized scheduled…
Browse files Browse the repository at this point in the history
…_at values (#1583)
  • Loading branch information
bensheldon authored Jan 24, 2025
1 parent 358def5 commit d45fb07
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/good_job/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ def create_thread(state = nil)
if state[:scheduled_at]
scheduled_at = if state[:scheduled_at].is_a? String
Time.zone.parse state[:scheduled_at]
elsif state[:scheduled_at].is_a? Numeric
Time.zone.at state[:scheduled_at]
else
state[:scheduled_at]
end
Expand Down
11 changes: 11 additions & 0 deletions spec/lib/good_job/adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ def perform(succeed: true)
priority: -55,
scheduled_at: be_within(1).of(10.minutes.from_now)
)
expect(GoodJob::Notifier).to have_received(:notify).with({
queue_name: 'elephant',
count: 1,
scheduled_at: within(1).of(10.minutes.from_now),
})
end

it 'sets default values' do
Expand All @@ -176,6 +181,12 @@ def perform(succeed: true)
priority: 0,
scheduled_at: be_within(1.second).of(Time.current)
)

expect(GoodJob::Notifier).to have_received(:notify).with({
queue_name: 'default',
count: 1,
scheduled_at: within(1).of(Time.current),
})
end

context 'when a job fails to enqueue' do
Expand Down
24 changes: 24 additions & 0 deletions spec/lib/good_job/scheduler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,30 @@
expect(scheduler.create_thread({ queue_name: 'elephant' })).to be false
end

it 'uses state[:scheduled_at] to cache future jobs' do
scheduler = described_class.new(GoodJob::JobPerformer.new('mice'), max_threads: 2)

# Handle Time objects
result = scheduler.create_thread({ scheduled_at: 1.day.from_now })
expect(result).to be_nil
expect(scheduler.stats[:active_cache]).to eq 1

# Handle JSON / ISO8601
result = scheduler.create_thread({ scheduled_at: 1.day.from_now.to_json })
expect(result).to be_nil
expect(scheduler.stats[:active_cache]).to eq 2

# Handle integers
result = scheduler.create_thread({ scheduled_at: 1.day.from_now.to_i })
expect(result).to be_nil
expect(scheduler.stats[:active_cache]).to eq 3

# Past scheduled_at should not be cached
result = scheduler.create_thread({ scheduled_at: 1.day.ago })
expect(result).to be true
expect(scheduler.stats[:active_cache]).to eq 3
end

it 'uses state[:count] to create multiple threads' do
job_performer = instance_double(GoodJob::JobPerformer, next: nil, next?: true, name: '', next_at: [], cleanup: nil, reset_stats: nil)
scheduler = described_class.new(job_performer, max_threads: 1)
Expand Down

0 comments on commit d45fb07

Please sign in to comment.