Make non-progressive batch operations possible

Created on 20 November 2009, over 14 years ago
Updated 13 June 2024, 12 days ago

My module does some tasks via Batch API. To avoid duplicating code, I want to do those same tasks during a cron run by programmatically starting and executing the batch without trying to redirect the user to the standard batch job progress bar thingie. There seems to be the facility for this by setting the 'progressive' value of the $process_info array to FALSE, but by the same token, there doesn't seem to be a way to do that - it's hard-coded to TRUE . From D6's form.inc (D7's version is the same in this regard):

function batch_process($redirect = NULL, $url = NULL) {
  $batch =& batch_get();

  if (isset($batch)) {
    // Add process information
    $url = isset($url) ? $url : 'batch';
    $process_info = array(
      'current_set' => 0,
      'progressive' => TRUE, // <= HERE
      'url' => isset($url) ? $url : 'batch',
      'source_page' => $_GET['q'],
      'redirect' => $redirect,
    );
    $batch += $process_info;

    if ($batch['progressive']) {
      // Clear the way for the drupal_goto redirection to the batch processing
      // page, by saving and unsetting the 'destination' if any, on both places
      // drupal_goto looks for it.
      // […snip…]
      drupal_goto($batch['url'], 'op=start&id='. $batch['id']);
    }
    else {
      // Non-progressive execution: bypass the whole progressbar workflow
      // and execute the batch in one pass.
      require_once './includes/batch.inc';
      _batch_process();
    }
  }
}

Unless I'm missing something, this means that, though the functionality exists to do what I want to do, it's not actually possible to do so without hacking core - the code in that "else" branch will never, ever execute. One dead kitten later, I get the expected result.

However, I will allow that I'm just misunderstanding how to use an esoteric feature of this esoteric API and that doing what I need to do is possible without slaughtering kittens; in which case, a bit of edumacation would be appreciated.

Proposed resolution

Set the progressive status in the batch based on batch definition e.g.

  $operations = ['my operations'];
  $batch = [
    'title' => 'My batch runs in single run',
    'operations' => $operations,
    'progressive' => FALSE,
  ];
  batch_set($batch);
  batch_process();

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

RTBC

Version

11.0 🔥

Component
Batch 

Last updated 4 days ago

Created by

🇺🇸United States Garrett Albright

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • Merge request !7999non-progressive batch fix → (Open) created by sukr_s
  • Pipeline finished with Failed
    about 2 months ago
    Total: 606s
    #168509
  • Pipeline finished with Failed
    about 2 months ago
    Total: 668s
    #168554
  • Pipeline finished with Failed
    about 2 months ago
    Total: 556s
    #168576
  • Pipeline finished with Failed
    about 2 months ago
    Total: 166s
    #168609
  • Pipeline finished with Failed
    about 2 months ago
    #168610
  • Pipeline finished with Failed
    about 2 months ago
    Total: 604s
    #168696
  • Pipeline finished with Success
    about 2 months ago
    Total: 569s
    #168729
  • 🇮🇳India sukr_s

    sukr_s changed the visibility of the branch 638712-make-non-progressive-batch to hidden.

  • 🇮🇳India sukr_s

    sukr_s changed the visibility of the branch 11.x to hidden.

  • Status changed to Needs review about 2 months ago
  • 🇮🇳India sukr_s

    With this fix progressive value can be set in batch definition. if progressive is false then it run in a single run else it uses multiple run. usage e.g

    $operations = ['my operations'];
      $batch = [
        'title' => 'My batch runs in single run',
        'operations' => $operations,
        'progressive' => FALSE,
      ];
      batch_set($batch);
      batch_process();
    
  • 🇮🇳India Bhanu951

    Bhanu951 changed the visibility of the branch 11.x to hidden.

  • Status changed to RTBC about 1 month ago
  • 🇺🇸United States smustgrave

    Removing tests tag as coverage can be seen here https://git.drupalcode.org/issue/drupal-638712/-/jobs/1645989
    Hiding patches since fix is in the MR.

    Issue summary appears complete and believe matches the solution.

    Only one I'm iffy about is

      $this->drupalGet('admin/reports/dblog');
        $this->assertSession()->pageTextContains('Non progressive operation 100');
    

    If there is just a better way to check the values without having to go to the page but may not be a big deal. Was the only thing I saw.

  • Status changed to Needs work 30 days ago
  • 🇳🇿New Zealand quietone New Zealand

    I read the issue summary, the comments and the MR.

    @satbir.singh, thanks for getting this working. To help reviewers, the information in #69 should be in the issue summary in the proposed resolution section. That is the best place for people to find and understand the consequences of the proposed change. Thanks.

    In #54 the solution of the patch in #47 was questioned as doing too much work. They then preferred the solution from #41. The solution in the MR is difference again. There should be discussion here on the reason the current solution was chosen over the others.

    I am setting back to NW for a comment in the MR and to get agreement on the solution.

  • Pipeline finished with Canceled
    30 days ago
    Total: 1572s
    #183182
  • Pipeline finished with Failed
    30 days ago
    Total: 877s
    #183206
  • Pipeline finished with Success
    29 days ago
    Total: 4432s
    #183276
  • Status changed to Needs review 29 days ago
  • 🇮🇳India sukr_s

    batch_set function accepts the batch definition with progressive as a parameter. #41 introduces a new parameter to the batch_process. Having a new parameter in batch_process can cause conflict between the value set when creating the batch with batch_set vis-a-vis calling the batch_process.

    The proposed solution uses the value set in the batch definition so that it's consistently available throughout the processing of the batch.

  • Status changed to RTBC 12 days ago
  • 🇺🇸United States smustgrave

    Feedback appears to be addressed here

Production build 0.69.0 2024