PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wpforms-lite/vendor/woocommerce/action-scheduler/classes/WP_CLI/ActionScheduler_WPCLI_QueueRunner.php

https://gitlab.com/ebrjose/comcebu
PHP | 197 lines | 82 code | 23 blank | 92 comment | 8 complexity | 67c7a4d86552810d8f113ce7264d9edf MD5 | raw file
  1. <?php
  2. use Action_Scheduler\WP_CLI\ProgressBar;
  3. /**
  4. * WP CLI Queue runner.
  5. *
  6. * This class can only be called from within a WP CLI instance.
  7. */
  8. class ActionScheduler_WPCLI_QueueRunner extends ActionScheduler_Abstract_QueueRunner {
  9. /** @var array */
  10. protected $actions;
  11. /** @var ActionScheduler_ActionClaim */
  12. protected $claim;
  13. /** @var \cli\progress\Bar */
  14. protected $progress_bar;
  15. /**
  16. * ActionScheduler_WPCLI_QueueRunner constructor.
  17. *
  18. * @param ActionScheduler_Store $store
  19. * @param ActionScheduler_FatalErrorMonitor $monitor
  20. * @param ActionScheduler_QueueCleaner $cleaner
  21. *
  22. * @throws Exception When this is not run within WP CLI
  23. */
  24. public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null ) {
  25. if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) ) {
  26. /* translators: %s php class name */
  27. throw new Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'action-scheduler' ), __CLASS__ ) );
  28. }
  29. parent::__construct( $store, $monitor, $cleaner );
  30. }
  31. /**
  32. * Set up the Queue before processing.
  33. *
  34. * @author Jeremy Pry
  35. *
  36. * @param int $batch_size The batch size to process.
  37. * @param array $hooks The hooks being used to filter the actions claimed in this batch.
  38. * @param string $group The group of actions to claim with this batch.
  39. * @param bool $force Whether to force running even with too many concurrent processes.
  40. *
  41. * @return int The number of actions that will be run.
  42. * @throws \WP_CLI\ExitException When there are too many concurrent batches.
  43. */
  44. public function setup( $batch_size, $hooks = array(), $group = '', $force = false ) {
  45. $this->run_cleanup();
  46. $this->add_hooks();
  47. // Check to make sure there aren't too many concurrent processes running.
  48. if ( $this->has_maximum_concurrent_batches() ) {
  49. if ( $force ) {
  50. WP_CLI::warning( __( 'There are too many concurrent batches, but the run is forced to continue.', 'action-scheduler' ) );
  51. } else {
  52. WP_CLI::error( __( 'There are too many concurrent batches.', 'action-scheduler' ) );
  53. }
  54. }
  55. // Stake a claim and store it.
  56. $this->claim = $this->store->stake_claim( $batch_size, null, $hooks, $group );
  57. $this->monitor->attach( $this->claim );
  58. $this->actions = $this->claim->get_actions();
  59. return count( $this->actions );
  60. }
  61. /**
  62. * Add our hooks to the appropriate actions.
  63. *
  64. * @author Jeremy Pry
  65. */
  66. protected function add_hooks() {
  67. add_action( 'action_scheduler_before_execute', array( $this, 'before_execute' ) );
  68. add_action( 'action_scheduler_after_execute', array( $this, 'after_execute' ), 10, 2 );
  69. add_action( 'action_scheduler_failed_execution', array( $this, 'action_failed' ), 10, 2 );
  70. }
  71. /**
  72. * Set up the WP CLI progress bar.
  73. *
  74. * @author Jeremy Pry
  75. */
  76. protected function setup_progress_bar() {
  77. $count = count( $this->actions );
  78. $this->progress_bar = new ProgressBar(
  79. /* translators: %d: amount of actions */
  80. sprintf( _n( 'Running %d action', 'Running %d actions', $count, 'action-scheduler' ), number_format_i18n( $count ) ),
  81. $count
  82. );
  83. }
  84. /**
  85. * Process actions in the queue.
  86. *
  87. * @author Jeremy Pry
  88. *
  89. * @param string $context Optional runner context. Default 'WP CLI'.
  90. *
  91. * @return int The number of actions processed.
  92. */
  93. public function run( $context = 'WP CLI' ) {
  94. do_action( 'action_scheduler_before_process_queue' );
  95. $this->setup_progress_bar();
  96. foreach ( $this->actions as $action_id ) {
  97. // Error if we lost the claim.
  98. if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $this->claim->get_id() ) ) ) {
  99. WP_CLI::warning( __( 'The claim has been lost. Aborting current batch.', 'action-scheduler' ) );
  100. break;
  101. }
  102. $this->process_action( $action_id, $context );
  103. $this->progress_bar->tick();
  104. }
  105. $completed = $this->progress_bar->current();
  106. $this->progress_bar->finish();
  107. $this->store->release_claim( $this->claim );
  108. do_action( 'action_scheduler_after_process_queue' );
  109. return $completed;
  110. }
  111. /**
  112. * Handle WP CLI message when the action is starting.
  113. *
  114. * @author Jeremy Pry
  115. *
  116. * @param $action_id
  117. */
  118. public function before_execute( $action_id ) {
  119. /* translators: %s refers to the action ID */
  120. WP_CLI::log( sprintf( __( 'Started processing action %s', 'action-scheduler' ), $action_id ) );
  121. }
  122. /**
  123. * Handle WP CLI message when the action has completed.
  124. *
  125. * @author Jeremy Pry
  126. *
  127. * @param int $action_id
  128. * @param null|ActionScheduler_Action $action The instance of the action. Default to null for backward compatibility.
  129. */
  130. public function after_execute( $action_id, $action = null ) {
  131. // backward compatibility
  132. if ( null === $action ) {
  133. $action = $this->store->fetch_action( $action_id );
  134. }
  135. /* translators: 1: action ID 2: hook name */
  136. WP_CLI::log( sprintf( __( 'Completed processing action %1$s with hook: %2$s', 'action-scheduler' ), $action_id, $action->get_hook() ) );
  137. }
  138. /**
  139. * Handle WP CLI message when the action has failed.
  140. *
  141. * @author Jeremy Pry
  142. *
  143. * @param int $action_id
  144. * @param Exception $exception
  145. * @throws \WP_CLI\ExitException With failure message.
  146. */
  147. public function action_failed( $action_id, $exception ) {
  148. WP_CLI::error(
  149. /* translators: 1: action ID 2: exception message */
  150. sprintf( __( 'Error processing action %1$s: %2$s', 'action-scheduler' ), $action_id, $exception->getMessage() ),
  151. false
  152. );
  153. }
  154. /**
  155. * Sleep and help avoid hitting memory limit
  156. *
  157. * @param int $sleep_time Amount of seconds to sleep
  158. * @deprecated 3.0.0
  159. */
  160. protected function stop_the_insanity( $sleep_time = 0 ) {
  161. _deprecated_function( 'ActionScheduler_WPCLI_QueueRunner::stop_the_insanity', '3.0.0', 'ActionScheduler_DataController::free_memory' );
  162. ActionScheduler_DataController::free_memory();
  163. }
  164. /**
  165. * Maybe trigger the stop_the_insanity() method to free up memory.
  166. */
  167. protected function maybe_stop_the_insanity() {
  168. // The value returned by progress_bar->current() might be padded. Remove padding, and convert to int.
  169. $current_iteration = intval( trim( $this->progress_bar->current() ) );
  170. if ( 0 === $current_iteration % 50 ) {
  171. $this->stop_the_insanity();
  172. }
  173. }
  174. }