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

Postpone reenqueuing the iteration job until after callbacks are done #345

Merged
merged 2 commits into from
Mar 1, 2021
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
19 changes: 19 additions & 0 deletions app/jobs/maintenance_tasks/task_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,27 @@ def on_shutdown
@ticker.persist
end

# We are reopening a private part of Job Iteration's API here, so we should
# ensure the method is still defined upstream. This way, in the case where
# the method changes upstream, we catch it at load time instead of at
# runtime while calling `super`.
unless private_method_defined?(:reenqueue_iteration_job)
error_message = <<~HEREDOC
JobIteration::Iteration#reenqueue_iteration_job is expected to be
defined. Upgrading the maintenance_tasks gem should solve this problem.
HEREDOC
raise error_message
end
def reenqueue_iteration_job(should_ignore: true)
Copy link
Member

Choose a reason for hiding this comment

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

Technically we're not ignoring it, more like postponing it but ok 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about using "postpone" instead, but then figured it's not really postponed because we call it again explicitly, it is more just outright ignored the first time 😛

super() unless should_ignore
@reenqueue_iteration_job = true
end

def after_perform
@run.save!
if defined?(@reenqueue_iteration_job) && @reenqueue_iteration_job
reenqueue_iteration_job(should_ignore: false)
end
end

def on_error(error)
Expand Down
4 changes: 4 additions & 0 deletions lib/maintenance_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
require 'pagy'
require 'pagy/extras/bulma'

# Force the TaskJob class to load so we can verify upstream compatibility with
# the JobIteration gem
require_relative '../app/jobs/maintenance_tasks/task_job'
Copy link
Member

Choose a reason for hiding this comment

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

Maybe instead of requiring the file we could to the check here instead? But that separates it from the patch itself so it's probably worse.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I think it might be less confusing to keep them altogether for now


# The engine's namespace module. It provides isolation between the host
# application's code and the engine-specific code. Top-level engine constants
# and variables are defined under this module.
Expand Down
18 changes: 18 additions & 0 deletions test/jobs/maintenance_tasks/task_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,24 @@ class TaskJobTest < ActiveJob::TestCase
assert_equal 'Task Maintenance::DeletedTask not found.', run.error_message
end

test '.perform_now delays reenqueuing the job after interruption until all callbacks are finished' do
JobIteration.stubs(interruption_adapter: -> { true })

AnotherTaskJob = Class.new(TaskJob) do
after_perform { self.class.times_interrupted = times_interrupted }

class << self
attr_accessor :times_interrupted
end
end
AnotherTaskJob.perform_now(@run)

# The job should not yet have been reenqueued, so times_interrupted should
# be 0
assert_equal 0, AnotherTaskJob.times_interrupted
assert_enqueued_jobs 1
end

test '.perform_now does not enqueue another job if Run errors' do
run = Run.create!(task_name: 'Maintenance::ErrorTask')

Expand Down