PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / trunk
Image Optimizer – Compress Images and Convert to WebP or AVIF vtrunk
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / vendor / woocommerce / action-scheduler / classes / WP_CLI / ActionScheduler_WPCLI_QueueRunner.php

ActionScheduler_WPCLI_QueueRunner.php in Image Optimizer – Compress Images and Convert to WebP or AVIF trunk, at vendor/woocommerce/action-scheduler/classes/WP_CLI/ActionScheduler_WPCLI_QueueRunner.php

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