6 * This file contains a fork of the Drupal Batch API that has been drastically
7 * simplified and tailored to Drush's unique use case.
9 * The existing API is very targeted towards environments that are web accessible,
10 * and would frequently attempt to redirect the user which would result in the
11 * drush process being completely destroyed with no hope of recovery.
13 * While the original API does offer a 'non progressive' mode which simply
14 * calls each operation in sequence within the current process, in most
15 * implementations (D6), it would still attempt to redirect
16 * unless very specific conditions were met.
18 * When operating in 'non progressive' mode, Drush would experience the problems
19 * that the API was written to solve in the first place, specifically that processes
20 * would exceed the available memory and exit with an error.
22 * Each major release of Drupal has also had slightly different implementations
23 * of the batch API, and this provides a uniform interface to all of these
27 use Drush\Log\LogLevel;
30 * Class extending ArrayObject to allow the batch API to perform logging when
31 * some keys of the array change.
33 * It is used to wrap batch's $context array and set log messages when values
34 * are assigned to keys 'message' or 'error_message'.
36 * @see _drush_batch_worker().
38 class DrushBatchContext extends ArrayObject {
39 function offsetSet($name, $value) {
40 if ($name == 'message') {
41 drush_log(strip_tags($value), LogLevel::OK);
43 elseif ($name == 'error_message') {
44 drush_set_error('DRUSH_BATCH_ERROR', strip_tags($value));
46 parent::offsetSet($name, $value);
51 * Process a Drupal batch by spawning multiple Drush processes.
53 * This function will include the correct batch engine for the current
54 * major version of Drupal, and will make use of the drush_backend_invoke
55 * system to spawn multiple worker threads to handle the processing of
56 * the current batch, while keeping track of available memory.
58 * The batch system will process as many batch sets as possible until
59 * the entire batch has been completed or half of the available memory
62 * This function is a drop in replacement for the existing batch_process()
65 * @param string $command
66 * (optional) The command to call for the back end process. By default this will be
67 * the 'batch-process' command, but some commands such as updatedb will
68 * have special initialization requirements, and will need to define and
69 * use their own command.
72 * @param array $options
75 function drush_backend_batch_process($command = 'batch-process', $args = array(), $options = array()) {
76 // Command line options to pass to the command.
77 $options['u'] = drush_user_get_class()->getCurrentUserAsSingle()->id();
79 drush_include_engine('drupal', 'batch');
80 _drush_backend_batch_process($command, $args, $options);
84 * Process sets from the specified batch.
86 * This function is called by the worker process that is spawned by the
87 * drush_backend_batch_process function.
89 * The command called needs to call this function after it's special bootstrap
90 * requirements have been taken care of.
93 * The batch ID of the batch being processed.
95 function drush_batch_command($id) {
96 include_once(DRUSH_DRUPAL_CORE . '/includes/batch.inc');
97 drush_include_engine('drupal', 'batch');
98 _drush_batch_command($id);