← All changes
|
vendor/woocommerce/action-scheduler/classes/WP_CLI/Migration_Command.php
+105
-63
1.1.0
→
trunk
View file →
| @@ -20,9 +20,13 @@ | ||
| 20 | 20 | * @codeCoverageIgnore |
| 21 | 21 | */ |
| 22 | 22 | class Migration_Command extends WP_CLI_Command { |
| 23 | 23 | |
| 24 | - /** @var int */ | |
| 24 | + /** | |
| 25 | + * Number of actions migrated. | |
| 26 | + * | |
| 27 | + * @var int | |
| 28 | + */ | |
| 25 | 29 | private $total_processed = 0; |
| 26 | 30 | |
| 27 | 31 | /** |
| 28 | 32 | * Register the command with WP-CLI |
| @@ -31,40 +35,44 @@ | ||
| 31 | 35 | if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { |
| 32 | 36 | return; |
| 33 | 37 | } |
| 34 | 38 | |
| 35 | - WP_CLI::add_command( 'action-scheduler migrate', [ $this, 'migrate' ], [ | |
| 36 | - 'shortdesc' => 'Migrates actions to the DB tables store', | |
| 37 | - 'synopsis' => [ | |
| 38 | - [ | |
| 39 | - 'type' => 'assoc', | |
| 40 | - 'name' => 'batch-size', | |
| 41 | - 'optional' => true, | |
| 42 | - 'default' => 100, | |
| 43 | - 'description' => 'The number of actions to process in each batch', | |
| 44 | - ], | |
| 45 | - [ | |
| 46 | - 'type' => 'assoc', | |
| 47 | - 'name' => 'free-memory-on', | |
| 48 | - 'optional' => true, | |
| 49 | - 'default' => 50, | |
| 50 | - 'description' => 'The number of actions to process between freeing memory. 0 disables freeing memory', | |
| 51 | - ], | |
| 52 | - [ | |
| 53 | - 'type' => 'assoc', | |
| 54 | - 'name' => 'pause', | |
| 55 | - 'optional' => true, | |
| 56 | - 'default' => 0, | |
| 57 | - 'description' => 'The number of seconds to pause when freeing memory', | |
| 58 | - ], | |
| 59 | - [ | |
| 60 | - 'type' => 'flag', | |
| 61 | - 'name' => 'dry-run', | |
| 62 | - 'optional' => true, | |
| 63 | - 'description' => 'Reports on the actions that would have been migrated, but does not change any data', | |
| 64 | - ], | |
| 65 | - ], | |
| 66 | - ] ); | |
| 39 | + WP_CLI::add_command( | |
| 40 | + 'action-scheduler migrate', | |
| 41 | + array( $this, 'migrate' ), | |
| 42 | + array( | |
| 43 | + 'shortdesc' => 'Migrates actions to the DB tables store', | |
| 44 | + 'synopsis' => array( | |
| 45 | + array( | |
| 46 | + 'type' => 'assoc', | |
| 47 | + 'name' => 'batch-size', | |
| 48 | + 'optional' => true, | |
| 49 | + 'default' => 100, | |
| 50 | + 'description' => 'The number of actions to process in each batch', | |
| 51 | + ), | |
| 52 | + array( | |
| 53 | + 'type' => 'assoc', | |
| 54 | + 'name' => 'free-memory-on', | |
| 55 | + 'optional' => true, | |
| 56 | + 'default' => 50, | |
| 57 | + 'description' => 'The number of actions to process between freeing memory. 0 disables freeing memory', | |
| 58 | + ), | |
| 59 | + array( | |
| 60 | + 'type' => 'assoc', | |
| 61 | + 'name' => 'pause', | |
| 62 | + 'optional' => true, | |
| 63 | + 'default' => 0, | |
| 64 | + 'description' => 'The number of seconds to pause when freeing memory', | |
| 65 | + ), | |
| 66 | + array( | |
| 67 | + 'type' => 'flag', | |
| 68 | + 'name' => 'dry-run', | |
| 69 | + 'optional' => true, | |
| 70 | + 'description' => 'Reports on the actions that would have been migrated, but does not change any data', | |
| 71 | + ), | |
| 72 | + ), | |
| 73 | + ) | |
| 74 | + ); | |
| 67 | 75 | } |
| 68 | 76 | |
| 69 | 77 | /** |
| 70 | 78 | * Process the data migration. |
| @@ -80,21 +88,21 @@ | ||
| 80 | 88 | $config = $this->get_migration_config( $assoc_args ); |
| 81 | 89 | $runner = new Runner( $config ); |
| 82 | 90 | $runner->init_destination(); |
| 83 | 91 | |
| 84 | - $batch_size = isset( $assoc_args[ 'batch-size' ] ) ? (int) $assoc_args[ 'batch-size' ] : 100; | |
| 85 | - $free_on = isset( $assoc_args[ 'free-memory-on' ] ) ? (int) $assoc_args[ 'free-memory-on' ] : 50; | |
| 86 | - $sleep = isset( $assoc_args[ 'pause' ] ) ? (int) $assoc_args[ 'pause' ] : 0; | |
| 92 | + $batch_size = isset( $assoc_args['batch-size'] ) ? (int) $assoc_args['batch-size'] : 100; | |
| 93 | + $free_on = isset( $assoc_args['free-memory-on'] ) ? (int) $assoc_args['free-memory-on'] : 50; | |
| 94 | + $sleep = isset( $assoc_args['pause'] ) ? (int) $assoc_args['pause'] : 0; | |
| 87 | 95 | \ActionScheduler_DataController::set_free_ticks( $free_on ); |
| 88 | 96 | \ActionScheduler_DataController::set_sleep_time( $sleep ); |
| 89 | 97 | |
| 90 | 98 | do { |
| 91 | - $actions_processed = $runner->run( $batch_size ); | |
| 99 | + $actions_processed = $runner->run( $batch_size ); | |
| 92 | 100 | $this->total_processed += $actions_processed; |
| 93 | 101 | } while ( $actions_processed > 0 ); |
| 94 | 102 | |
| 95 | 103 | if ( ! $config->get_dry_run() ) { |
| 96 | - // let the scheduler know that there's nothing left to do | |
| 104 | + // let the scheduler know that there's nothing left to do. | |
| 97 | 105 | $scheduler = new Scheduler(); |
| 98 | 106 | $scheduler->mark_complete(); |
| 99 | 107 | } |
| 100 | 108 | |
| @@ -108,14 +116,17 @@ | ||
| 108 | 116 | * |
| 109 | 117 | * @return ActionScheduler\Migration\Config |
| 110 | 118 | */ |
| 111 | 119 | private function get_migration_config( $args ) { |
| 112 | - $args = wp_parse_args( $args, [ | |
| 113 | - 'dry-run' => false, | |
| 114 | - ] ); | |
| 120 | + $args = wp_parse_args( | |
| 121 | + $args, | |
| 122 | + array( | |
| 123 | + 'dry-run' => false, | |
| 124 | + ) | |
| 125 | + ); | |
| 115 | 126 | |
| 116 | 127 | $config = Controller::instance()->get_migration_config_object(); |
| 117 | - $config->set_dry_run( ! empty( $args[ 'dry-run' ] ) ); | |
| 128 | + $config->set_dry_run( ! empty( $args['dry-run'] ) ); | |
| 118 | 129 | |
| 119 | 130 | return $config; |
| 120 | 131 | } |
| 121 | 132 | |
| @@ -122,27 +133,58 @@ | ||
| 122 | 133 | /** |
| 123 | 134 | * Hook command line logging into migration actions. |
| 124 | 135 | */ |
| 125 | 136 | private function init_logging() { |
| 126 | - add_action( 'action_scheduler/migrate_action_dry_run', function ( $action_id ) { | |
| 127 | - WP_CLI::debug( sprintf( 'Dry-run: migrated action %d', $action_id ) ); | |
| 128 | - }, 10, 1 ); | |
| 129 | - add_action( 'action_scheduler/no_action_to_migrate', function ( $action_id ) { | |
| 130 | - WP_CLI::debug( sprintf( 'No action found to migrate for ID %d', $action_id ) ); | |
| 131 | - }, 10, 1 ); | |
| 132 | - add_action( 'action_scheduler/migrate_action_failed', function ( $action_id ) { | |
| 133 | - WP_CLI::warning( sprintf( 'Failed migrating action with ID %d', $action_id ) ); | |
| 134 | - }, 10, 1 ); | |
| 135 | - add_action( 'action_scheduler/migrate_action_incomplete', function ( $source_id, $destination_id ) { | |
| 136 | - WP_CLI::warning( sprintf( 'Unable to remove source action with ID %d after migrating to new ID %d', $source_id, $destination_id ) ); | |
| 137 | - }, 10, 2 ); | |
| 138 | - add_action( 'action_scheduler/migrated_action', function ( $source_id, $destination_id ) { | |
| 139 | - WP_CLI::debug( sprintf( 'Migrated source action with ID %d to new store with ID %d', $source_id, $destination_id ) ); | |
| 140 | - }, 10, 2 ); | |
| 141 | - add_action( 'action_scheduler/migration_batch_starting', function ( $batch ) { | |
| 142 | - WP_CLI::debug( 'Beginning migration of batch: ' . print_r( $batch, true ) ); | |
| 143 | - }, 10, 1 ); | |
| 144 | - add_action( 'action_scheduler/migration_batch_complete', function ( $batch ) { | |
| 145 | - WP_CLI::log( sprintf( 'Completed migration of %d actions', count( $batch ) ) ); | |
| 146 | - }, 10, 1 ); | |
| 137 | + add_action( | |
| 138 | + 'action_scheduler/migrate_action_dry_run', | |
| 139 | + function ( $action_id ) { | |
| 140 | + WP_CLI::debug( sprintf( 'Dry-run: migrated action %d', $action_id ) ); | |
| 141 | + } | |
| 142 | + ); | |
| 143 | + | |
| 144 | + add_action( | |
| 145 | + 'action_scheduler/no_action_to_migrate', | |
| 146 | + function ( $action_id ) { | |
| 147 | + WP_CLI::debug( sprintf( 'No action found to migrate for ID %d', $action_id ) ); | |
| 148 | + } | |
| 149 | + ); | |
| 150 | + | |
| 151 | + add_action( | |
| 152 | + 'action_scheduler/migrate_action_failed', | |
| 153 | + function ( $action_id ) { | |
| 154 | + WP_CLI::warning( sprintf( 'Failed migrating action with ID %d', $action_id ) ); | |
| 155 | + } | |
| 156 | + ); | |
| 157 | + | |
| 158 | + add_action( | |
| 159 | + 'action_scheduler/migrate_action_incomplete', | |
| 160 | + function ( $source_id, $destination_id ) { | |
| 161 | + WP_CLI::warning( sprintf( 'Unable to remove source action with ID %d after migrating to new ID %d', $source_id, $destination_id ) ); | |
| 162 | + }, | |
| 163 | + 10, | |
| 164 | + 2 | |
| 165 | + ); | |
| 166 | + | |
| 167 | + add_action( | |
| 168 | + 'action_scheduler/migrated_action', | |
| 169 | + function ( $source_id, $destination_id ) { | |
| 170 | + WP_CLI::debug( sprintf( 'Migrated source action with ID %d to new store with ID %d', $source_id, $destination_id ) ); | |
| 171 | + }, | |
| 172 | + 10, | |
| 173 | + 2 | |
| 174 | + ); | |
| 175 | + | |
| 176 | + add_action( | |
| 177 | + 'action_scheduler/migration_batch_starting', | |
| 178 | + function ( $batch ) { | |
| 179 | + WP_CLI::debug( 'Beginning migration of batch: ' . print_r( $batch, true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r | |
| 180 | + } | |
| 181 | + ); | |
| 182 | + | |
| 183 | + add_action( | |
| 184 | + 'action_scheduler/migration_batch_complete', | |
| 185 | + function ( $batch ) { | |
| 186 | + WP_CLI::log( sprintf( 'Completed migration of %d actions', count( $batch ) ) ); | |
| 187 | + } | |
| 188 | + ); | |
| 147 | 189 | } |
| 148 | 190 | } |