batch_6.inc

  1. 8.0.x commands/core/drupal/batch_6.inc
  2. 6.x commands/core/drupal/batch_6.inc
  3. 7.x commands/core/drupal/batch_6.inc
  4. 3.x commands/core/drupal/batch_6.inc
  5. 4.x commands/core/drupal/batch_6.inc
  6. 5.x commands/core/drupal/batch_6.inc

Drupal 6 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_6.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Drupal 6 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. // Initiate db storage in order to get a batch id. We have to provide
  24. // at least an empty string for the (not null) 'token' column.
  25. db_query("INSERT INTO {batch} (token, timestamp) VALUES ('', %d)", time());
  26. $batch['id'] = db_last_insert_id('batch', 'bid');
  27. $args[] = $batch['id'];
  28. // Actually store the batch data and the token generated form the batch id.
  29. db_query("UPDATE {batch} SET token = '%s', batch = '%s' WHERE bid = %d", drupal_get_token($batch['id']), serialize($batch), $batch['id']);
  30. $finished = FALSE;
  31. while (!$finished) {
  32. $data = drush_invoke_process('@self', $command, $args, $options);
  33. $finished = drush_get_error() || !$data || (isset($data['context']['drush_batch_process_finished']) && $data['context']['drush_batch_process_finished'] == TRUE);
  34. }
  35. }
  36. }
  37. /**
  38. * Initialize the batch command and call the worker function.
  39. *
  40. * Loads the batch record from the database and sets up the requirements
  41. * for the worker, such as registering the shutdown function.
  42. *
  43. * @param id
  44. * The batch id of the batch being processed.
  45. */
  46. function _drush_batch_command($id) {
  47. $batch =& batch_get();
  48. // Retrieve the current state of batch from db.
  49. if ($data = db_result(db_query("SELECT batch FROM {batch} WHERE bid = %d", $id))) {
  50. $batch = unserialize($data);
  51. }
  52. else {
  53. return FALSE;
  54. }
  55. if (!isset($batch['running'])) {
  56. $batch['running'] = TRUE;
  57. }
  58. // Register database update for end of processing.
  59. register_shutdown_function('_drush_batch_shutdown');
  60. if (_drush_batch_worker()) {
  61. _drush_batch_finished();
  62. }
  63. }
  64. /**
  65. * Process batch operations
  66. *
  67. * Using the current $batch process each of the operations until the batch
  68. * has been completed or half of the available memory for the process has been
  69. * reached.
  70. */
  71. function _drush_batch_worker() {
  72. $batch =& batch_get();
  73. $current_set =& _batch_current_set();
  74. $set_changed = TRUE;
  75. timer_start('batch_processing');
  76. while (!$current_set['success']) {
  77. // If this is the first time we iterate this batch set in the current
  78. // request, we check if it requires an additional file for functions
  79. // definitions.
  80. if ($set_changed && isset($current_set['file']) && is_file($current_set['file'])) {
  81. include_once($current_set['file']);
  82. }
  83. $finished = 1;
  84. $task_message = '';
  85. if ((list($function, $args) = reset($current_set['operations'])) && function_exists($function)) {
  86. // Build the 'context' array, execute the function call,
  87. // and retrieve the user message.
  88. $batch_context = array('sandbox' => &$current_set['sandbox'], 'results' => &$current_set['results'], 'finished' => &$finished, 'message' => &$task_message);
  89. // Magic wrap to catch changes to 'message' key.
  90. $batch_context = new DrushBatchContext($batch_context);
  91. // Process the current operation.
  92. call_user_func_array($function, array_merge($args, array(&$batch_context)));
  93. $finished = $batch_context['finished'];
  94. }
  95. if ($finished >= 1) {
  96. // Make sure this step isn't counted double when computing $current.
  97. $finished = 0;
  98. // Remove the operation and clear the sandbox.
  99. array_shift($current_set['operations']);
  100. $current_set['sandbox'] = array();
  101. }
  102. // If the batch set is completed, browse through the remaining sets,
  103. // executing 'control sets' (stored form submit handlers) along the way -
  104. // this might in turn insert new batch sets.
  105. // Stop when we find a set that actually has operations.
  106. $set_changed = FALSE;
  107. $old_set = $current_set;
  108. while (empty($current_set['operations']) && ($current_set['success'] = TRUE) && _batch_next_set()) {
  109. $current_set =& _batch_current_set();
  110. $set_changed = TRUE;
  111. }
  112. // At this point, either $current_set is a 'real' batch set (has operations),
  113. // or all sets have been completed.
  114. // TODO - replace with memory check!
  115. // If we're in progressive mode, stop after 1 second.
  116. if ((memory_get_usage() * 2) >= drush_memory_limit()) {
  117. drush_log(dt("Batch process has consumed in excess of 50% of available memory. Starting new thread"), "batch");
  118. break;
  119. }
  120. }
  121. // Gather progress information.
  122. // Reporting 100% progress will cause the whole batch to be considered
  123. // processed. If processing was paused right after moving to a new set,
  124. // we have to use the info from the new (unprocessed) one.
  125. if ($set_changed && isset($current_set['operations'])) {
  126. // Processing will continue with a fresh batch set.
  127. $remaining = count($current_set['operations']);
  128. $total = $current_set['total'];
  129. $task_message = '';
  130. }
  131. else {
  132. $remaining = count($old_set['operations']);
  133. $total = $old_set['total'];
  134. }
  135. $current = $total - $remaining + $finished;
  136. $percentage = $total ? floor($current / $total * 100) : 100;
  137. return ($percentage == 100);
  138. }
  139. /**
  140. * End the batch processing:
  141. * Call the 'finished' callbacks to allow custom handling of results,
  142. * and resolve page redirection.
  143. */
  144. function _drush_batch_finished() {
  145. $batch =& batch_get();
  146. // Execute the 'finished' callbacks for each batch set.
  147. foreach ($batch['sets'] as $key => $batch_set) {
  148. if (isset($batch_set['finished'])) {
  149. // Check if the set requires an additional file for functions definitions.
  150. if (isset($batch_set['file']) && is_file($batch_set['file'])) {
  151. include_once($batch_set['file']);
  152. }
  153. if (function_exists($batch_set['finished'])) {
  154. $batch_set['finished']($batch_set['success'], $batch_set['results'], $batch_set['operations']);
  155. }
  156. }
  157. }
  158. // Cleanup the batch table and unset the global $batch variable.
  159. db_query("DELETE FROM {batch} WHERE bid = %d", $batch['id']);
  160. $_batch = $batch;
  161. $batch = NULL;
  162. drush_set_option('drush_batch_process_finished', TRUE);
  163. }
  164. /**
  165. * Shutdown function: store the batch data for next request,
  166. * or clear the table if the batch is finished.
  167. */
  168. function _drush_batch_shutdown() {
  169. if ($batch = batch_get()) {
  170. db_query("UPDATE {batch} SET batch = '%s' WHERE bid = %d", serialize($batch), $batch['id']);
  171. }
  172. }