batch.inc

  1. 8.0.x includes/batch.inc
  2. 8.0.x commands/core/drupal/batch.inc
  3. 6.x includes/batch.inc
  4. 6.x commands/core/drupal/batch.inc
  5. 7.x includes/batch.inc
  6. 7.x commands/core/drupal/batch.inc
  7. 3.x includes/batch.inc
  8. 4.x commands/core/drupal/batch.inc
  9. 4.x includes/batch.inc
  10. 5.x commands/core/drupal/batch.inc
  11. 5.x includes/batch.inc
  12. master commands/core/drupal/batch.inc
  13. master includes/batch.inc

Drupal 7 engine for the Batch API

Functions

Namesort descending Description
_drush_backend_batch_process Main loop for the Drush batch API.
_drush_batch_command Initialize the batch command and call the worker function.
_drush_batch_finished End the batch processing: Call the 'finished' callbacks to allow custom handling of results, and resolve page redirection.
_drush_batch_shutdown Shutdown function: store the batch data for next request, or clear the table if the batch is finished.
_drush_batch_worker Process batch operations

File

commands/core/drupal/batch.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Drupal 7 engine for the Batch API
  5. */
  6. /**
  7. * Main loop for the Drush batch API.
  8. *
  9. * Saves a record of the batch into the database, and progressively call $command to
  10. * process the operations.
  11. *
  12. * @param command
  13. * The command to call to process the batch.
  14. *
  15. */
  16. function _drush_backend_batch_process($command = 'batch-process', $args, $options) {
  17. $batch =& batch_get();
  18. if (isset($batch)) {
  19. $process_info = array(
  20. 'current_set' => 0,
  21. );
  22. $batch += $process_info;
  23. // The batch is now completely built. Allow other modules to make changes
  24. // to the batch so that it is easier to reuse batch processes in other
  25. // enviroments.
  26. drupal_alter('batch', $batch);
  27. // Assign an arbitrary id: don't rely on a serial column in the 'batch'
  28. // table, since non-progressive batches skip database storage completely.
  29. $batch['id'] = db_next_id();
  30. $args[] = $batch['id'];
  31. $batch['progressive'] = TRUE;
  32. // Move operations to a job queue. Non-progressive batches will use a
  33. // memory-based queue.
  34. foreach ($batch['sets'] as $key => $batch_set) {
  35. _batch_populate_queue($batch, $key);
  36. }
  37. // Store the batch.
  38. db_insert('batch')
  39. ->fields(array(
  40. 'bid' => $batch['id'],
  41. 'timestamp' => REQUEST_TIME,
  42. 'token' => drupal_get_token($batch['id']),
  43. 'batch' => serialize($batch),
  44. ))
  45. ->execute();
  46. $finished = FALSE;
  47. global $user;
  48. while (!$finished) {
  49. if ($user->uid) {
  50. $data = drush_invoke_process('@self', $command, $args, array($batch['id'], '-u', $user->uid));
  51. }
  52. else {
  53. $data = drush_invoke_process('@self', $command, $args, array($batch['id']));
  54. }
  55. $finished = drush_get_error() || !$data || (isset($data['context']['drush_batch_process_finished']) && $data['context']['drush_batch_process_finished'] == TRUE);
  56. }
  57. }
  58. }
  59. /**
  60. * Initialize the batch command and call the worker function.
  61. *
  62. * Loads the batch record from the database and sets up the requirements
  63. * for the worker, such as registering the shutdown function.
  64. *
  65. * @param id
  66. * The batch id of the batch being processed.
  67. */
  68. function _drush_batch_command($id) {
  69. $batch =& batch_get();
  70. $data = db_query("SELECT batch FROM {batch} WHERE bid = :bid", array(
  71. ':bid' => $id))->fetchField();
  72. if ($data) {
  73. $batch = unserialize($data);
  74. }
  75. else {
  76. return FALSE;
  77. }
  78. if (!isset($batch['running'])) {
  79. $batch['running'] = TRUE;
  80. }
  81. // Register database update for end of processing.
  82. register_shutdown_function('_drush_batch_shutdown');
  83. if (_drush_batch_worker()) {
  84. _drush_batch_finished();
  85. }
  86. }
  87. /**
  88. * Process batch operations
  89. *
  90. * Using the current $batch process each of the operations until the batch
  91. * has been completed or half of the available memory for the process has been
  92. * reached.
  93. */
  94. function _drush_batch_worker() {
  95. $batch =& batch_get();
  96. $current_set =& _batch_current_set();
  97. $set_changed = TRUE;
  98. timer_start('batch_processing');
  99. if (empty($current_set['start'])) {
  100. $current_set['start'] = microtime(TRUE);
  101. }
  102. $queue = _batch_queue($current_set);
  103. while (!$current_set['success']) {
  104. // If this is the first time we iterate this batch set in the current
  105. // request, we check if it requires an additional file for functions
  106. // definitions.
  107. if ($set_changed && isset($current_set['file']) && is_file($current_set['file'])) {
  108. include_once DRUPAL_ROOT . '/' . $current_set['file'];
  109. }
  110. $task_message = '';
  111. // Assume a single pass operation and set the completion level to 1 by
  112. // default.
  113. $finished = 1;
  114. if ($item = $queue->claimItem()) {
  115. list($function, $args) = $item->data;
  116. // Build the 'context' array and execute the function call.
  117. $batch_context = array(
  118. 'sandbox' => &$current_set['sandbox'],
  119. 'results' => &$current_set['results'],
  120. 'finished' => &$finished,
  121. 'message' => &$task_message,
  122. );
  123. // Magic wrap to catch changes to 'message' key.
  124. $batch_context = new DrushBatchContext($batch_context);
  125. call_user_func_array($function, array_merge($args, array(&$batch_context)));
  126. $finished = $batch_context['finished'];
  127. if ($finished >= 1) {
  128. // Make sure this step is not counted twice when computing $current.
  129. $finished = 0;
  130. // Remove the processed operation and clear the sandbox.
  131. $queue->deleteItem($item);
  132. $current_set['count']--;
  133. $current_set['sandbox'] = array();
  134. }
  135. }
  136. // When all operations in the current batch set are completed, browse
  137. // through the remaining sets, marking them 'successfully processed'
  138. // along the way, until we find a set that contains operations.
  139. // _batch_next_set() executes form submit handlers stored in 'control'
  140. // sets (see form_execute_handlers()), which can in turn add new sets to
  141. // the batch.
  142. $set_changed = FALSE;
  143. $old_set = $current_set;
  144. while (empty($current_set['count']) && ($current_set['success'] = TRUE) && _batch_next_set()) {
  145. $current_set = &_batch_current_set();
  146. $current_set['start'] = microtime(TRUE);
  147. $set_changed = TRUE;
  148. }
  149. // At this point, either $current_set contains operations that need to be
  150. // processed or all sets have been completed.
  151. $queue = _batch_queue($current_set);
  152. // If we are in progressive mode, break processing after 1 second.
  153. if ((memory_get_usage() * 2) >= drush_memory_limit()) {
  154. drush_log(dt("Batch process has consumed in excess of 50% of available memory. Starting new thread"), "batch");
  155. // Record elapsed wall clock time.
  156. $current_set['elapsed'] = round((microtime(TRUE) - $current_set['start']) * 1000, 2);
  157. break;
  158. }
  159. }
  160. // Reporting 100% progress will cause the whole batch to be considered
  161. // processed. If processing was paused right after moving to a new set,
  162. // we have to use the info from the new (unprocessed) set.
  163. if ($set_changed && isset($current_set['queue'])) {
  164. // Processing will continue with a fresh batch set.
  165. $remaining = $current_set['count'];
  166. $total = $current_set['total'];
  167. $progress_message = $current_set['init_message'];
  168. $task_message = '';
  169. }
  170. else {
  171. // Processing will continue with the current batch set.
  172. $remaining = $old_set['count'];
  173. $total = $old_set['total'];
  174. $progress_message = $old_set['progress_message'];
  175. }
  176. $current = $total - $remaining + $finished;
  177. $percentage = _batch_api_percentage($total, $current);
  178. return ($percentage == 100);
  179. }
  180. /**
  181. * End the batch processing:
  182. * Call the 'finished' callbacks to allow custom handling of results,
  183. * and resolve page redirection.
  184. */
  185. function _drush_batch_finished() {
  186. $batch = &batch_get();
  187. // Execute the 'finished' callbacks for each batch set, if defined.
  188. foreach ($batch['sets'] as $batch_set) {
  189. if (isset($batch_set['finished'])) {
  190. // Check if the set requires an additional file for function definitions.
  191. if (isset($batch_set['file']) && is_file($batch_set['file'])) {
  192. include_once DRUPAL_ROOT . '/' . $batch_set['file'];
  193. }
  194. if (function_exists($batch_set['finished'])) {
  195. $queue = _batch_queue($batch_set);
  196. $operations = $queue->getAllItems();
  197. $batch_set['finished']($batch_set['success'], $batch_set['results'], $operations, format_interval($batch_set['elapsed'] / 1000));
  198. }
  199. }
  200. }
  201. // Clean up the batch table and unset the static $batch variable.
  202. db_delete('batch')
  203. ->condition('bid', $batch['id'])
  204. ->execute();
  205. foreach ($batch['sets'] as $batch_set) {
  206. if ($queue = _batch_queue($batch_set)) {
  207. $queue->deleteQueue();
  208. }
  209. }
  210. $_batch = $batch;
  211. $batch = NULL;
  212. drush_set_option('drush_batch_process_finished', TRUE);
  213. }
  214. /**
  215. * Shutdown function: store the batch data for next request,
  216. * or clear the table if the batch is finished.
  217. */
  218. function _drush_batch_shutdown() {
  219. if ($batch = batch_get()) {
  220. db_update('batch')
  221. ->fields(array('batch' => serialize($batch)))
  222. ->condition('bid', $batch['id'])
  223. ->execute();
  224. }
  225. }