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