Docs: correction in code example for continued background jobs

  • Start this issue's title with Docs: or Docs feedback:.

    Title: Docs: Flawed code example for continued background jobs

Problem to solve

The code example for creating background jobs that reschedule themselves is logically flawed and will cause an infinite loop if implemented as written.

  • Product/feature affected: Batching in background jobs section in the docs.
  • Doc section affected: The documentation page describing how to write long-running batching jobs that reschedule themselves to avoid hitting runtime limits. The specific example demonstrates a "continued job" pattern.

The problem is that the example code fails to update the cursor (iid) when it reschedules itself, causing the next job to start from the same position as the one that timed out.

Further details

The goal of this pattern is to allow a background job to process a large number of records in batches, safely rescheduling itself if it runs out of time. A user following this documentation would inadvertently create a job that never makes progress past the first time it hits a limit, repeatedly processing the same batch of records.

Proposal

Update the code example to correctly pass the cursor from the last processed batch to the next scheduled job.

The flaw is in this line:

MyJob.perform_in(2.minutes, project_id, iid)

It should be corrected to use the max_iid calculated within the batch loop:

MyJob.perform_in(2.minutes, project_id, max_iid)

This ensures the next job starts where the previous one left off.

Who can address the issue

N/A

Other links/references

N/A

Edited by 🤖 GitLab Bot 🤖