Skip to content

Instantly share code, notes, and snippets.

@johnennewdeeson
Created April 12, 2016 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnennewdeeson/2641682018495a0201cbebcecbd57a61 to your computer and use it in GitHub Desktop.
Save johnennewdeeson/2641682018495a0201cbebcecbd57a61 to your computer and use it in GitHub Desktop.
Executing batched operation with Drush
<?php
/**
* @file
* sites/all/drush/play.drush.php
*
* Oftentimes one needs to run an operation on a list of things.
* If this list is of variable length, running it in one process may
* cause a time out and/or out of memory.
*
* This is a simple drush based way of dealing with this. There are
* two drush commands, one which works out the list of things to
* operate over and then uses drush_invoke_process() to execute a
* second drush command on just one of the things.
*/
/**
* Implements of hook_drush_command().
*/
function ebip_members_system_drush_command() {
$items = array();
$items['outer_command'] = array(
'description' => 'The outer command works out the size of what needs to be done then runs the inner command multiple times.',
'aliases' => array('oc'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
);
$items['inner_command'] = array(
'description' => 'The inner command works on a single item in the list of items to be processed. As this happens in its own process memory concerns are less.',
'aliases' => array('ic'),
'arguments' => [
'id' => 'Id of thing to be worked on (e.g. node nid).',
],
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
);
return $items;
}
/**
* Outer command.
*/
function drush_play_outer_command() {
// Get a list of things to process.
$node_nids = $query = db_select('node', 'n');
->condition('n.type', 'article', '=')
->fields('d', ['nid'])
->execute()
->fetchCol(0);
drush_print(dt('Found !count nodes of type article', ['!count' => count($node_ids)]));
// Foreach node, run the inner command.
foreach ($node_ids as $node_id) {
drush_invoke_process('@self', 'inner_comand', [$node_id]);
}
}
/**
* Inner command
*/
function drush_play_inner_command($nid) {
$node = node_load($nid);
if (empty($node) || $node->type !== 'article') {
return drush_set_error('play', 'Node id supplied is bad: ' . $nid);
}
// Do something useful and save the node... e.g.
// $node->title = $node->title . ' - processed';
// node_save($node);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment