Skip to content

Commit

Permalink
Fix inline to run unscheduled jobs immediately; fix inline deprecatio…
Browse files Browse the repository at this point in the history
…n messages in test suite
  • Loading branch information
bensheldon committed Jun 18, 2022
1 parent 727942b commit c2c1e73
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/good_job/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def enqueue_at(active_job, timestamp)
scheduled_at = timestamp ? Time.zone.at(timestamp) : nil

if execute_inline?
future_scheduled = (scheduled_at.nil? || scheduled_at > Time.current)
future_scheduled = scheduled_at && scheduled_at > Time.current
will_execute_inline = !future_scheduled || (future_scheduled && !@configuration.inline_execution_respects_schedule?)
end

Expand Down
22 changes: 18 additions & 4 deletions spec/app/jobs/example_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
describe "ERROR_ONCE_TYPE" do
it 'errors once then succeeds' do
active_job = described_class.perform_later(described_class::ERROR_ONCE_TYPE)
10.times do
GoodJob.perform_inline
travel(5.minutes)
end
travel_back

executions = GoodJob::Execution.where(active_job_id: active_job.job_id).order(created_at: :asc)
expect(executions.size).to eq 2
expect(executions.last.error).to be_nil
Expand All @@ -28,6 +34,12 @@
describe "ERROR_FIVE_TIMES_TYPE" do
it 'errors five times then succeeds' do
active_job = described_class.perform_later(described_class::ERROR_FIVE_TIMES_TYPE)
10.times do
GoodJob.perform_inline
travel(5.minutes)
end
travel_back

executions = GoodJob::Execution.where(active_job_id: active_job.job_id).order(created_at: :asc)
expect(executions.size).to eq 6
expect(executions.last.error).to be_nil
Expand All @@ -36,11 +48,13 @@

describe "DEAD_TYPE" do
it 'errors but does not retry' do
begin
described_class.perform_later(described_class::DEAD_TYPE)
rescue ExampleJob::DeadError
nil
described_class.perform_later(described_class::DEAD_TYPE)
10.times do
GoodJob.perform_inline
travel(5.minutes)
end
travel_back

active_job_id = GoodJob::Execution.last.active_job_id

executions = GoodJob::Execution.where(active_job_id: active_job_id).order(created_at: :asc)
Expand Down
20 changes: 11 additions & 9 deletions spec/engine/filters/good_job/jobs_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
GoodJob::ActiveJobJob.order(created_at: :asc).last.update!(cron_key: "frequent_cron")

ActiveJob::Base.queue_adapter = GoodJob::Adapter.new(execution_mode: :inline)
ExampleJob.set(queue: 'default').perform_later('success')
ExampleJob.set(queue: 'mice').perform_later('error_once')

begin
ExampleJob.set(queue: 'elephants').perform_later('dead')
rescue ExampleJob::DeadError
nil
ExampleJob.set(queue: 'default').perform_later(ExampleJob::SUCCESS_TYPE)
ExampleJob.set(queue: 'mice').perform_later(ExampleJob::ERROR_ONCE_TYPE)

travel_to 1.hour.ago
ExampleJob.set(queue: 'elephants').perform_later(ExampleJob::DEAD_TYPE)
5.times do
travel 5.minutes
GoodJob.perform_inline
end
travel_back

running_job = ExampleJob.perform_later('success')
running_execution = GoodJob::Execution.find(running_job.provider_job_id)
Expand Down Expand Up @@ -58,9 +60,9 @@
describe '#states' do
it 'is a valid result' do
expect(filter.states).to eq({
"scheduled" => 0,
"scheduled" => 1,
"retried" => 0,
"queued" => 1,
"queued" => 0,
"running" => 1,
"finished" => 2,
"discarded" => 1,
Expand Down
11 changes: 11 additions & 0 deletions spec/integration/adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,20 @@ def perform
end)
end

it 'executes unscheduled jobs immediately' do
ExampleJob.perform_later
expect(PERFORMED.size).to eq 1
end

it 'raises unhandled exceptions' do
expect do
TestJob.perform_later
5.times do
travel(5.minutes)
GoodJob.perform_inline
end
travel_back

end.to raise_error JobError
expect(PERFORMED.size).to eq 3
end
Expand Down
7 changes: 6 additions & 1 deletion spec/system/jobs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@
end

let(:discarded_job) do
travel_to 1.hour.ago
ExampleJob.set(queue: :elephants).perform_later(ExampleJob::DEAD_TYPE)
rescue StandardError
5.times do
travel 5.minutes
GoodJob.perform_inline
end
travel_back
GoodJob::ActiveJobJob.order(created_at: :asc).last
end

Expand Down
4 changes: 2 additions & 2 deletions spec/test_app/app/jobs/example_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class ExampleJob < ApplicationJob
DEAD_TYPE = 'dead',
SLOW_TYPE = 'slow',
]
retry_on DeadError, attempts: 3

retry_on(DeadError, attempts: 3) { nil }

def perform(type = SUCCESS_TYPE)
if type == SUCCESS_TYPE
Expand Down

0 comments on commit c2c1e73

Please sign in to comment.