Skip to content

Commit

Permalink
Make sure the run cursor is saved in the job (#1036)
Browse files Browse the repository at this point in the history
If a job starts without a cursor (i.e. after a run is resumed), but the
run has a cursor, we do use the cursor from the run in build_enumerator.

However, we don't store the cursor on the job itself at this point. If
the first iteration fails, we go through the `on_error` callback and
store the job cursor, which is `nil`, on the run, effectively losing the
progress previously made on the run.

This fixes this issue by always storing the cursor on the job in
`build_enumerator`, making sure the job and run have the same cursor.
  • Loading branch information
etiennebarrie authored Jun 4, 2024
1 parent 080e83f commit ac3872a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/jobs/concerns/maintenance_tasks/task_job_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def retry_on(*, **)

def build_enumerator(_run, cursor:)
cursor ||= @run.cursor
self.cursor_position = cursor
@collection_enum = @task.enumerator_builder(cursor: cursor)

@collection_enum ||= case (collection = @task.collection)
Expand Down
11 changes: 11 additions & 0 deletions test/jobs/maintenance_tasks/task_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,17 @@ class << self
assert_equal "0", @run.reload.cursor
end

test ".perform_now persists cursor when there's an error" do
run = Run.create!(task_name: "Maintenance::ErrorTask")

TaskJob.perform_now(run)
assert_equal "1", run.reload.cursor

run.enqueued!
TaskJob.perform_now(run)
assert_equal "1", run.reload.cursor
end

test ".perform_now starts job from cursor position when job resumes" do
@run.update!(cursor: "0")

Expand Down

0 comments on commit ac3872a

Please sign in to comment.