Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove arguments from perform method #140

Merged
merged 1 commit into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions lib/good_job/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,25 +177,17 @@ def self.enqueue(active_job, scheduled_at: nil, create_with_advisory_lock: false
end

# Execute the ActiveJob job this {Job} represents.
# @param destroy_after [Boolean]
# Whether to destroy the {Job} record after executing it if the job did
# not need to be reperformed. Defaults to the value of
# {GoodJob.preserve_job_records}.
# @param reperform_on_standard_error [Boolean]
# Whether to re-queue the job to execute again if it raised an instance
# of +StandardError+. Defaults to the value of
# {GoodJob.reperform_jobs_on_standard_error}.
# @return [Array<(Object, Exception)>]
# An array of the return value of the job's +#perform+ method and the
# exception raised by the job, if any. If the job completed successfully,
# the second array entry (the exception) will be +nil+ and vice versa.
def perform(destroy_after: !GoodJob.preserve_job_records, reperform_on_standard_error: GoodJob.reperform_jobs_on_standard_error)
def perform
raise PreviouslyPerformedError, 'Cannot perform a job that has already been performed' if finished_at

GoodJob::CurrentExecution.reset

self.performed_at = Time.current
save! unless destroy_after
save! if GoodJob.preserve_job_records

result, rescued_error = execute

Expand All @@ -215,15 +207,15 @@ def perform(destroy_after: !GoodJob.preserve_job_records, reperform_on_standard_
error_message = "#{error.class}: #{error.message}" if error
self.error = error_message

if rescued_error && reperform_on_standard_error
if rescued_error && GoodJob.reperform_jobs_on_standard_error
save!
else
self.finished_at = Time.current

if destroy_after
destroy!
else
if GoodJob.preserve_job_records
save!
else
destroy!
Comment on lines +215 to +218
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads so much better now with the positive condition coming first 🎉

end
end

Expand Down
32 changes: 26 additions & 6 deletions spec/lib/good_job/job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ def perform(result_value = nil, raise_error: false)
end

it 'can preserve the job' do
good_job.perform(destroy_after: false)
allow(GoodJob).to receive(:preserve_job_records).and_return(true)

good_job.perform
expect(good_job.reload).to have_attributes(
performed_at: within(1.second).of(Time.current),
finished_at: within(1.second).of(Time.current)
Expand Down Expand Up @@ -235,7 +237,9 @@ def perform(result_value = nil, raise_error: false)
end

it 'can preserve the job' do
good_job.perform(destroy_after: false)
allow(GoodJob).to receive(:preserve_job_records).and_return(true)

good_job.perform

expect(good_job.reload).to have_attributes(
error: "ExpectedError: Raised expected error",
Expand All @@ -257,8 +261,14 @@ def perform(result_value = nil, raise_error: false)

describe 'GoodJob.reperform_jobs_on_standard_error behavior' do
context 'when true' do
before do
allow(GoodJob).to receive(:reperform_jobs_on_standard_error).and_return(true)
end

it 'leaves the job record unfinished' do
good_job.perform(destroy_after: false)
allow(GoodJob).to receive(:preserve_job_records).and_return(true)

good_job.perform

expect(good_job.reload).to have_attributes(
error: "ExpectedError: Raised expected error",
Expand All @@ -268,19 +278,29 @@ def perform(result_value = nil, raise_error: false)
end

it 'does not destroy the job record' do
good_job.perform(destroy_after: true)
allow(GoodJob).to receive(:preserve_job_records).and_return(false)

good_job.perform
expect { good_job.reload }.not_to raise_error
end
end

context 'when false' do
before do
allow(GoodJob).to receive(:reperform_jobs_on_standard_error).and_return(false)
end

it 'destroys the job' do
good_job.perform(destroy_after: true, reperform_on_standard_error: false)
allow(GoodJob).to receive(:preserve_job_records).and_return(false)

good_job.perform
expect { good_job.reload }.to raise_error ActiveRecord::RecordNotFound
end

it 'can preserve the job' do
good_job.perform(destroy_after: false, reperform_on_standard_error: false)
allow(GoodJob).to receive(:preserve_job_records).and_return(true)

good_job.perform

expect(good_job.reload).to have_attributes(
error: "ExpectedError: Raised expected error",
Expand Down