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