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
Migration_Command.php
149 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Action_Scheduler\WP_CLI; |
| 5 | |
| 6 | use Action_Scheduler\Migration\Config; |
| 7 | use Action_Scheduler\Migration\Runner; |
| 8 | use Action_Scheduler\Migration\Scheduler; |
| 9 | use Action_Scheduler\Migration\Controller; |
| 10 | use WP_CLI; |
| 11 | use WP_CLI_Command; |
| 12 | |
| 13 | /** |
| 14 | * Class Migration_Command |
| 15 | * |
| 16 | * @package Action_Scheduler\WP_CLI |
| 17 | * |
| 18 | * @since 3.0.0 |
| 19 | * |
| 20 | * @codeCoverageIgnore |
| 21 | */ |
| 22 | class Migration_Command extends WP_CLI_Command { |
| 23 | |
| 24 | /** @var int */ |
| 25 | private $total_processed = 0; |
| 26 | |
| 27 | /** |
| 28 | * Register the command with WP-CLI |
| 29 | */ |
| 30 | public function register() { |
| 31 | if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 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 | ] ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Process the data migration. |
| 71 | * |
| 72 | * @param array $positional_args Required for WP CLI. Not used in migration. |
| 73 | * @param array $assoc_args Optional arguments. |
| 74 | * |
| 75 | * @return void |
| 76 | */ |
| 77 | public function migrate( $positional_args, $assoc_args ) { |
| 78 | $this->init_logging(); |
| 79 | |
| 80 | $config = $this->get_migration_config( $assoc_args ); |
| 81 | $runner = new Runner( $config ); |
| 82 | $runner->init_destination(); |
| 83 | |
| 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; |
| 87 | \ActionScheduler_DataController::set_free_ticks( $free_on ); |
| 88 | \ActionScheduler_DataController::set_sleep_time( $sleep ); |
| 89 | |
| 90 | do { |
| 91 | $actions_processed = $runner->run( $batch_size ); |
| 92 | $this->total_processed += $actions_processed; |
| 93 | } while ( $actions_processed > 0 ); |
| 94 | |
| 95 | if ( ! $config->get_dry_run() ) { |
| 96 | // let the scheduler know that there's nothing left to do |
| 97 | $scheduler = new Scheduler(); |
| 98 | $scheduler->mark_complete(); |
| 99 | } |
| 100 | |
| 101 | WP_CLI::success( sprintf( '%s complete. %d actions processed.', $config->get_dry_run() ? 'Dry run' : 'Migration', $this->total_processed ) ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Build the config object used to create the Runner |
| 106 | * |
| 107 | * @param array $args Optional arguments. |
| 108 | * |
| 109 | * @return ActionScheduler\Migration\Config |
| 110 | */ |
| 111 | private function get_migration_config( $args ) { |
| 112 | $args = wp_parse_args( $args, [ |
| 113 | 'dry-run' => false, |
| 114 | ] ); |
| 115 | |
| 116 | $config = Controller::instance()->get_migration_config_object(); |
| 117 | $config->set_dry_run( ! empty( $args[ 'dry-run' ] ) ); |
| 118 | |
| 119 | return $config; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Hook command line logging into migration actions. |
| 124 | */ |
| 125 | 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 ); |
| 147 | } |
| 148 | } |
| 149 |