PluginProbe
WANotifier for Forms and Actions / 2.7.13
WANotifier for Forms and Actions v2.7.13
3.1.1 3.1.0 3.0.4 2.7.10 2.7.11 2.7.12 2.7.13 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 3.0.0 3.0.1 3.0.2 3.0.3 trunk 0.1.0 0.1.1 1.0.0 1.0.1 1.0.2 All 67 releases
notifier / libraries / action-scheduler / classes / WP_CLI / ActionScheduler_WPCLI_Scheduler_command.php

ActionScheduler_WPCLI_Scheduler_command.php in WANotifier for Forms and Actions 2.7.13, at libraries/action-scheduler/classes/WP_CLI/ActionScheduler_WPCLI_Scheduler_command.php

211 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Commands for Action Scheduler.
5 */
6 class ActionScheduler_WPCLI_Scheduler_command extends WP_CLI_Command {
7
8 /**
9 * Force tables schema creation for Action Scheduler
10 *
11 * ## OPTIONS
12 *
13 * @param array $args Positional arguments.
14 * @param array $assoc_args Keyed arguments.
15 *
16 * @subcommand fix-schema
17 */
18 public function fix_schema( $args, $assoc_args ) {
19 $schema_classes = array( ActionScheduler_LoggerSchema::class, ActionScheduler_StoreSchema::class );
20
21 foreach ( $schema_classes as $classname ) {
22 if ( is_subclass_of( $classname, ActionScheduler_Abstract_Schema::class ) ) {
23 $obj = new $classname();
24 $obj->init();
25 $obj->register_tables( true );
26
27 WP_CLI::success(
28 sprintf(
29 /* translators: %s refers to the schema name*/
30 __( 'Registered schema for %s', 'action-scheduler' ),
31 $classname
32 )
33 );
34 }
35 }
36 }
37
38 /**
39 * Run the Action Scheduler
40 *
41 * ## OPTIONS
42 *
43 * [--batch-size=<size>]
44 * : The maximum number of actions to run. Defaults to 100.
45 *
46 * [--batches=<size>]
47 * : Limit execution to a number of batches. Defaults to 0, meaning batches will continue being executed until all actions are complete.
48 *
49 * [--cleanup-batch-size=<size>]
50 * : The maximum number of actions to clean up. Defaults to the value of --batch-size.
51 *
52 * [--hooks=<hooks>]
53 * : Only run actions with the specified hook. Omitting this option runs actions with any hook. Define multiple hooks as a comma separated string (without spaces), e.g. `--hooks=hook_one,hook_two,hook_three`
54 *
55 * [--group=<group>]
56 * : Only run actions from the specified group. Omitting this option runs actions from all groups.
57 *
58 * [--exclude-groups=<groups>]
59 * : Run actions from all groups except the specified group(s). Define multiple groups as a comma separated string (without spaces), e.g. '--group_a,group_b'. This option is ignored when `--group` is used.
60 *
61 * [--free-memory-on=<count>]
62 * : The number of actions to process between freeing memory. 0 disables freeing memory. Default 50.
63 *
64 * [--pause=<seconds>]
65 * : The number of seconds to pause when freeing memory. Default no pause.
66 *
67 * [--force]
68 * : Whether to force execution despite the maximum number of concurrent processes being exceeded.
69 *
70 * @param array $args Positional arguments.
71 * @param array $assoc_args Keyed arguments.
72 * @throws \WP_CLI\ExitException When an error occurs.
73 *
74 * @subcommand run
75 */
76 public function run( $args, $assoc_args ) {
77 // Handle passed arguments.
78 $batch = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batch-size', 100 ) );
79 $batches = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batches', 0 ) );
80 $clean = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'cleanup-batch-size', $batch ) );
81 $hooks = explode( ',', WP_CLI\Utils\get_flag_value( $assoc_args, 'hooks', '' ) );
82 $hooks = array_filter( array_map( 'trim', $hooks ) );
83 $group = \WP_CLI\Utils\get_flag_value( $assoc_args, 'group', '' );
84 $exclude_groups = \WP_CLI\Utils\get_flag_value( $assoc_args, 'exclude-groups', '' );
85 $free_on = \WP_CLI\Utils\get_flag_value( $assoc_args, 'free-memory-on', 50 );
86 $sleep = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pause', 0 );
87 $force = \WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false );
88
89 ActionScheduler_DataController::set_free_ticks( $free_on );
90 ActionScheduler_DataController::set_sleep_time( $sleep );
91
92 $batches_completed = 0;
93 $actions_completed = 0;
94 $unlimited = $batches === 0;
95 if ( is_callable( [ ActionScheduler::store(), 'set_claim_filter' ] ) ) {
96 $exclude_groups = $this->parse_comma_separated_string( $exclude_groups );
97
98 if ( ! empty( $exclude_groups ) ) {
99 ActionScheduler::store()->set_claim_filter('exclude-groups', $exclude_groups );
100 }
101 }
102
103 try {
104 // Custom queue cleaner instance.
105 $cleaner = new ActionScheduler_QueueCleaner( null, $clean );
106
107 // Get the queue runner instance
108 $runner = new ActionScheduler_WPCLI_QueueRunner( null, null, $cleaner );
109
110 // Determine how many tasks will be run in the first batch.
111 $total = $runner->setup( $batch, $hooks, $group, $force );
112
113 // Run actions for as long as possible.
114 while ( $total > 0 ) {
115 $this->print_total_actions( $total );
116 $actions_completed += $runner->run();
117 $batches_completed++;
118
119 // Maybe set up tasks for the next batch.
120 $total = ( $unlimited || $batches_completed < $batches ) ? $runner->setup( $batch, $hooks, $group, $force ) : 0;
121 }
122 } catch ( Exception $e ) {
123 $this->print_error( $e );
124 }
125
126 $this->print_total_batches( $batches_completed );
127 $this->print_success( $actions_completed );
128 }
129
130 /**
131 * Converts a string of comma-separated values into an array of those same values.
132 *
133 * @param string $string The string of one or more comma separated values.
134 *
135 * @return array
136 */
137 private function parse_comma_separated_string( $string ): array {
138 return array_filter( str_getcsv( $string ) );
139 }
140
141 /**
142 * Print WP CLI message about how many actions are about to be processed.
143 *
144 * @author Jeremy Pry
145 *
146 * @param int $total
147 */
148 protected function print_total_actions( $total ) {
149 WP_CLI::log(
150 sprintf(
151 /* translators: %d refers to how many scheduled tasks were found to run */
152 _n( 'Found %d scheduled task', 'Found %d scheduled tasks', $total, 'action-scheduler' ),
153 $total
154 )
155 );
156 }
157
158 /**
159 * Print WP CLI message about how many batches of actions were processed.
160 *
161 * @author Jeremy Pry
162 *
163 * @param int $batches_completed
164 */
165 protected function print_total_batches( $batches_completed ) {
166 WP_CLI::log(
167 sprintf(
168 /* translators: %d refers to the total number of batches executed */
169 _n( '%d batch executed.', '%d batches executed.', $batches_completed, 'action-scheduler' ),
170 $batches_completed
171 )
172 );
173 }
174
175 /**
176 * Convert an exception into a WP CLI error.
177 *
178 * @author Jeremy Pry
179 *
180 * @param Exception $e The error object.
181 *
182 * @throws \WP_CLI\ExitException
183 */
184 protected function print_error( Exception $e ) {
185 WP_CLI::error(
186 sprintf(
187 /* translators: %s refers to the exception error message */
188 __( 'There was an error running the action scheduler: %s', 'action-scheduler' ),
189 $e->getMessage()
190 )
191 );
192 }
193
194 /**
195 * Print a success message with the number of completed actions.
196 *
197 * @author Jeremy Pry
198 *
199 * @param int $actions_completed
200 */
201 protected function print_success( $actions_completed ) {
202 WP_CLI::success(
203 sprintf(
204 /* translators: %d refers to the total number of tasks completed */
205 _n( '%d scheduled task completed.', '%d scheduled tasks completed.', $actions_completed, 'action-scheduler' ),
206 $actions_completed
207 )
208 );
209 }
210 }
211