I notice that the drush documentation includes a batch-process command, but I haven't been able to figure out how to use it. Does drush support the Batch API? If so, how can I set and process a predefined batch job using drush? (Drupal 6.17)


moshe weitzman’s picture

Status: Active » Fixed

There are no docs at this time. The updatedb command is the only core command to use drush's implementation of batch api. Study it for guidance. Its a bit complex, unfortunately.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

domidc’s picture

I tried looking at the updatedb command but I couldnt make it work.

I have this drush command building up a batch

function import_drush_import_fast($start, $stop) {
  $result = db_query("SELECT * FROM {importer_17} WHERE id > %d AND id < %d", $start, $stop);
  import_drush_import_fast_operations($batch, $result);
  $batch =& batch_get();
  $batch['progressive'] = FALSE;

 *  Creates operations for importing bedrijf nodes
function import_drush_import_fast_operations(&$batch, &$result) {
  while ($fields = db_fetch_array($result)) {    
    $fields_out = array();
    foreach ($fields as $field) {
      $fields_out[] = $field;

    $batch['operations'][] = array('import_create_bedrijf_nodes', array($fields_out, TRUE, 17));    

It creates the batch in the databse, but when it gets to drush_backend_batch_process() it doesnt execute the batch.

In batch_6.inc I checked if the correct id is submitted and it is.

function _drush_backend_batch_process($command = 'batch-process') {
  $batch =& batch_get();

  if (isset($batch)) {
    $process_info = array(
      'current_set' => 0,
    $batch += $process_info;

    // Initiate db storage in order to get a batch id. We have to provide
    // at least an empty string for the (not null) 'token' column.
    db_query("INSERT INTO {batch} (token, timestamp) VALUES ('', %d)", time());
    $batch['id'] = db_last_insert_id('batch', 'bid');

    // Actually store the batch data and the token generated form the batch id.
    db_query("UPDATE {batch} SET token = '%s', batch = '%s' WHERE bid = %d", drupal_get_token($batch['id']), serialize($batch), $batch['id']);

    $finished = FALSE;

    while (!$finished) {

//The $batch['id'] is what it should be
//The command outputs "batch-process"

      $data = drush_backend_invoke($command, array($batch['id']));

//$data outputs false

      $finished = drush_get_error() || !$data || ($data['context']['drush_batch_process_finished'] == TRUE);

I have no idea what I m doing wrong at this point, wouldnt this be like typing: "drush batch-process 5". (btw I tried that and that works)
Can you give me a clue please? Thanks in advance.

domidc’s picture

It must have been something with the windows drush. I tried it on a linux machine and everything works. Still dont know what the cause is, but since production will be on a linux machine its not an issue for me anymore.

xjm’s picture

Thanks for the snippet! I got lost trying to follow updatedb. I'll try this as a template and maybe post a handbook page if I can get it working.

xjm’s picture

I was able to use #3 to create working batch jobs. I created a handbook page based on this issue at: http://drupal.org/node/873132. Feel free to insert any corrections.

moshe weitzman’s picture

Awesome work, xjm. Thanks.

greg.1.anderson’s picture

Title: Processing batch jobs with drush? » Processing batch jobs with drush
Version: All-versions-3.1 »
Component: Code » Base system (internal API)

Deleted some spam that changed the title of this issue. Should have recovered the title first, it appears.