PluginProbe
WANotifier for Forms and Actions / 1.0.0
WANotifier for Forms and Actions v1.0.0
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 1.0.0, at libraries/action-scheduler/classes/WP_CLI/ActionScheduler_WPCLI_Scheduler_command.php

189 lines 5.8 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 * [--free-memory-on=<count>]
59 * : The number of actions to process between freeing memory. 0 disables freeing memory. Default 50.
60 *
61 * [--pause=<seconds>]
62 * : The number of seconds to pause when freeing memory. Default no pause.
63 *
64 * [--force]
65 * : Whether to force execution despite the maximum number of concurrent processes being exceeded.
66 *
67 * @param array $args Positional arguments.
68 * @param array $assoc_args Keyed arguments.
69 * @throws \WP_CLI\ExitException When an error occurs.
70 *
71 * @subcommand run
72 */
73 public function run( $args, $assoc_args ) {
74 // Handle passed arguments.
75 $batch = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batch-size', 100 ) );
76 $batches = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batches', 0 ) );
77 $clean = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'cleanup-batch-size', $batch ) );
78 $hooks = explode( ',', WP_CLI\Utils\get_flag_value( $assoc_args, 'hooks', '' ) );
79 $hooks = array_filter( array_map( 'trim', $hooks ) );
80 $group = \WP_CLI\Utils\get_flag_value( $assoc_args, 'group', '' );
81 $free_on = \WP_CLI\Utils\get_flag_value( $assoc_args, 'free-memory-on', 50 );
82 $sleep = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pause', 0 );
83 $force = \WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false );
84
85 ActionScheduler_DataController::set_free_ticks( $free_on );
86 ActionScheduler_DataController::set_sleep_time( $sleep );
87
88 $batches_completed = 0;
89 $actions_completed = 0;
90 $unlimited = $batches === 0;
91
92 try {
93 // Custom queue cleaner instance.
94 $cleaner = new ActionScheduler_QueueCleaner( null, $clean );
95
96 // Get the queue runner instance
97 $runner = new ActionScheduler_WPCLI_QueueRunner( null, null, $cleaner );
98
99 // Determine how many tasks will be run in the first batch.
100 $total = $runner->setup( $batch, $hooks, $group, $force );
101
102 // Run actions for as long as possible.
103 while ( $total > 0 ) {
104 $this->print_total_actions( $total );
105 $actions_completed += $runner->run();
106 $batches_completed++;
107
108 // Maybe set up tasks for the next batch.
109 $total = ( $unlimited || $batches_completed < $batches ) ? $runner->setup( $batch, $hooks, $group, $force ) : 0;
110 }
111 } catch ( Exception $e ) {
112 $this->print_error( $e );
113 }
114
115 $this->print_total_batches( $batches_completed );
116 $this->print_success( $actions_completed );
117 }
118
119 /**
120 * Print WP CLI message about how many actions are about to be processed.
121 *
122 * @author Jeremy Pry
123 *
124 * @param int $total
125 */
126 protected function print_total_actions( $total ) {
127 WP_CLI::log(
128 sprintf(
129 /* translators: %d refers to how many scheduled taks were found to run */
130 _n( 'Found %d scheduled task', 'Found %d scheduled tasks', $total, 'action-scheduler' ),
131 number_format_i18n( $total )
132 )
133 );
134 }
135
136 /**
137 * Print WP CLI message about how many batches of actions were processed.
138 *
139 * @author Jeremy Pry
140 *
141 * @param int $batches_completed
142 */
143 protected function print_total_batches( $batches_completed ) {
144 WP_CLI::log(
145 sprintf(
146 /* translators: %d refers to the total number of batches executed */
147 _n( '%d batch executed.', '%d batches executed.', $batches_completed, 'action-scheduler' ),
148 number_format_i18n( $batches_completed )
149 )
150 );
151 }
152
153 /**
154 * Convert an exception into a WP CLI error.
155 *
156 * @author Jeremy Pry
157 *
158 * @param Exception $e The error object.
159 *
160 * @throws \WP_CLI\ExitException
161 */
162 protected function print_error( Exception $e ) {
163 WP_CLI::error(
164 sprintf(
165 /* translators: %s refers to the exception error message */
166 __( 'There was an error running the action scheduler: %s', 'action-scheduler' ),
167 $e->getMessage()
168 )
169 );
170 }
171
172 /**
173 * Print a success message with the number of completed actions.
174 *
175 * @author Jeremy Pry
176 *
177 * @param int $actions_completed
178 */
179 protected function print_success( $actions_completed ) {
180 WP_CLI::success(
181 sprintf(
182 /* translators: %d refers to the total number of taskes completed */
183 _n( '%d scheduled task completed.', '%d scheduled tasks completed.', $actions_completed, 'action-scheduler' ),
184 number_format_i18n( $actions_completed )
185 )
186 );
187 }
188 }
189