woocommerce
/
packages
/
action-scheduler
/
classes
/
WP_CLI
/
ActionScheduler_WPCLI_Scheduler_command.php
ActionScheduler_WPCLI_QueueRunner.php
6 years ago
ActionScheduler_WPCLI_Scheduler_command.php
6 years ago
Migration_Command.php
6 years ago
ProgressBar.php
6 years ago
ActionScheduler_WPCLI_Scheduler_command.php
159 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Commands for Action Scheduler. |
| 5 | */ |
| 6 | class ActionScheduler_WPCLI_Scheduler_command extends WP_CLI_Command { |
| 7 | |
| 8 | /** |
| 9 | * Run the Action Scheduler |
| 10 | * |
| 11 | * ## OPTIONS |
| 12 | * |
| 13 | * [--batch-size=<size>] |
| 14 | * : The maximum number of actions to run. Defaults to 100. |
| 15 | * |
| 16 | * [--batches=<size>] |
| 17 | * : Limit execution to a number of batches. Defaults to 0, meaning batches will continue being executed until all actions are complete. |
| 18 | * |
| 19 | * [--cleanup-batch-size=<size>] |
| 20 | * : The maximum number of actions to clean up. Defaults to the value of --batch-size. |
| 21 | * |
| 22 | * [--hooks=<hooks>] |
| 23 | * : 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` |
| 24 | * |
| 25 | * [--group=<group>] |
| 26 | * : Only run actions from the specified group. Omitting this option runs actions from all groups. |
| 27 | * |
| 28 | * [--free-memory-on=<count>] |
| 29 | * : The number of actions to process between freeing memory. 0 disables freeing memory. Default 50. |
| 30 | * |
| 31 | * [--pause=<seconds>] |
| 32 | * : The number of seconds to pause when freeing memory. Default no pause. |
| 33 | * |
| 34 | * [--force] |
| 35 | * : Whether to force execution despite the maximum number of concurrent processes being exceeded. |
| 36 | * |
| 37 | * @param array $args Positional arguments. |
| 38 | * @param array $assoc_args Keyed arguments. |
| 39 | * @throws \WP_CLI\ExitException When an error occurs. |
| 40 | * |
| 41 | * @subcommand run |
| 42 | */ |
| 43 | public function run( $args, $assoc_args ) { |
| 44 | // Handle passed arguments. |
| 45 | $batch = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batch-size', 100 ) ); |
| 46 | $batches = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batches', 0 ) ); |
| 47 | $clean = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'cleanup-batch-size', $batch ) ); |
| 48 | $hooks = explode( ',', WP_CLI\Utils\get_flag_value( $assoc_args, 'hooks', '' ) ); |
| 49 | $hooks = array_filter( array_map( 'trim', $hooks ) ); |
| 50 | $group = \WP_CLI\Utils\get_flag_value( $assoc_args, 'group', '' ); |
| 51 | $free_on = \WP_CLI\Utils\get_flag_value( $assoc_args, 'free-memory-on', 50 ); |
| 52 | $sleep = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pause', 0 ); |
| 53 | $force = \WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false ); |
| 54 | |
| 55 | ActionScheduler_DataController::set_free_ticks( $free_on ); |
| 56 | ActionScheduler_DataController::set_sleep_time( $sleep ); |
| 57 | |
| 58 | $batches_completed = 0; |
| 59 | $actions_completed = 0; |
| 60 | $unlimited = $batches === 0; |
| 61 | |
| 62 | try { |
| 63 | // Custom queue cleaner instance. |
| 64 | $cleaner = new ActionScheduler_QueueCleaner( null, $clean ); |
| 65 | |
| 66 | // Get the queue runner instance |
| 67 | $runner = new ActionScheduler_WPCLI_QueueRunner( null, null, $cleaner ); |
| 68 | |
| 69 | // Determine how many tasks will be run in the first batch. |
| 70 | $total = $runner->setup( $batch, $hooks, $group, $force ); |
| 71 | |
| 72 | // Run actions for as long as possible. |
| 73 | while ( $total > 0 ) { |
| 74 | $this->print_total_actions( $total ); |
| 75 | $actions_completed += $runner->run(); |
| 76 | $batches_completed++; |
| 77 | |
| 78 | // Maybe set up tasks for the next batch. |
| 79 | $total = ( $unlimited || $batches_completed < $batches ) ? $runner->setup( $batch, $hooks, $group, $force ) : 0; |
| 80 | } |
| 81 | } catch ( Exception $e ) { |
| 82 | $this->print_error( $e ); |
| 83 | } |
| 84 | |
| 85 | $this->print_total_batches( $batches_completed ); |
| 86 | $this->print_success( $actions_completed ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Print WP CLI message about how many actions are about to be processed. |
| 91 | * |
| 92 | * @author Jeremy Pry |
| 93 | * |
| 94 | * @param int $total |
| 95 | */ |
| 96 | protected function print_total_actions( $total ) { |
| 97 | WP_CLI::log( |
| 98 | sprintf( |
| 99 | /* translators: %d refers to how many scheduled taks were found to run */ |
| 100 | _n( 'Found %d scheduled task', 'Found %d scheduled tasks', $total, 'woocommerce' ), |
| 101 | number_format_i18n( $total ) |
| 102 | ) |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Print WP CLI message about how many batches of actions were processed. |
| 108 | * |
| 109 | * @author Jeremy Pry |
| 110 | * |
| 111 | * @param int $batches_completed |
| 112 | */ |
| 113 | protected function print_total_batches( $batches_completed ) { |
| 114 | WP_CLI::log( |
| 115 | sprintf( |
| 116 | /* translators: %d refers to the total number of batches executed */ |
| 117 | _n( '%d batch executed.', '%d batches executed.', $batches_completed, 'woocommerce' ), |
| 118 | number_format_i18n( $batches_completed ) |
| 119 | ) |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Convert an exception into a WP CLI error. |
| 125 | * |
| 126 | * @author Jeremy Pry |
| 127 | * |
| 128 | * @param Exception $e The error object. |
| 129 | * |
| 130 | * @throws \WP_CLI\ExitException |
| 131 | */ |
| 132 | protected function print_error( Exception $e ) { |
| 133 | WP_CLI::error( |
| 134 | sprintf( |
| 135 | /* translators: %s refers to the exception error message */ |
| 136 | __( 'There was an error running the action scheduler: %s', 'woocommerce' ), |
| 137 | $e->getMessage() |
| 138 | ) |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Print a success message with the number of completed actions. |
| 144 | * |
| 145 | * @author Jeremy Pry |
| 146 | * |
| 147 | * @param int $actions_completed |
| 148 | */ |
| 149 | protected function print_success( $actions_completed ) { |
| 150 | WP_CLI::success( |
| 151 | sprintf( |
| 152 | /* translators: %d refers to the total number of taskes completed */ |
| 153 | _n( '%d scheduled task completed.', '%d scheduled tasks completed.', $actions_completed, 'woocommerce' ), |
| 154 | number_format_i18n( $actions_completed ) |
| 155 | ) |
| 156 | ); |
| 157 | } |
| 158 | } |
| 159 |