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