Action
1 year ago
ActionScheduler_WPCLI_Clean_Command.php
1 year ago
ActionScheduler_WPCLI_QueueRunner.php
1 year ago
ActionScheduler_WPCLI_Scheduler_command.php
1 year ago
Action_Command.php
1 year ago
Migration_Command.php
1 year ago
ProgressBar.php
1 year ago
System_Command.php
1 year ago
index.php
3 years ago
Migration_Command.php
134 lines
| 1 | <?php |
| 2 | namespace Action_Scheduler\WP_CLI; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use Action_Scheduler\Migration\Config; |
| 5 | use Action_Scheduler\Migration\Runner; |
| 6 | use Action_Scheduler\Migration\Scheduler; |
| 7 | use Action_Scheduler\Migration\Controller; |
| 8 | use WP_CLI; |
| 9 | use WP_CLI_Command; |
| 10 | class Migration_Command extends WP_CLI_Command { |
| 11 | private $total_processed = 0; |
| 12 | public function register() { |
| 13 | if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { |
| 14 | return; |
| 15 | } |
| 16 | WP_CLI::add_command( |
| 17 | 'action-scheduler migrate', |
| 18 | array( $this, 'migrate' ), |
| 19 | array( |
| 20 | 'shortdesc' => 'Migrates actions to the DB tables store', |
| 21 | 'synopsis' => array( |
| 22 | array( |
| 23 | 'type' => 'assoc', |
| 24 | 'name' => 'batch-size', |
| 25 | 'optional' => true, |
| 26 | 'default' => 100, |
| 27 | 'description' => 'The number of actions to process in each batch', |
| 28 | ), |
| 29 | array( |
| 30 | 'type' => 'assoc', |
| 31 | 'name' => 'free-memory-on', |
| 32 | 'optional' => true, |
| 33 | 'default' => 50, |
| 34 | 'description' => 'The number of actions to process between freeing memory. 0 disables freeing memory', |
| 35 | ), |
| 36 | array( |
| 37 | 'type' => 'assoc', |
| 38 | 'name' => 'pause', |
| 39 | 'optional' => true, |
| 40 | 'default' => 0, |
| 41 | 'description' => 'The number of seconds to pause when freeing memory', |
| 42 | ), |
| 43 | array( |
| 44 | 'type' => 'flag', |
| 45 | 'name' => 'dry-run', |
| 46 | 'optional' => true, |
| 47 | 'description' => 'Reports on the actions that would have been migrated, but does not change any data', |
| 48 | ), |
| 49 | ), |
| 50 | ) |
| 51 | ); |
| 52 | } |
| 53 | public function migrate( $positional_args, $assoc_args ) { |
| 54 | $this->init_logging(); |
| 55 | $config = $this->get_migration_config( $assoc_args ); |
| 56 | $runner = new Runner( $config ); |
| 57 | $runner->init_destination(); |
| 58 | $batch_size = isset( $assoc_args['batch-size'] ) ? (int) $assoc_args['batch-size'] : 100; |
| 59 | $free_on = isset( $assoc_args['free-memory-on'] ) ? (int) $assoc_args['free-memory-on'] : 50; |
| 60 | $sleep = isset( $assoc_args['pause'] ) ? (int) $assoc_args['pause'] : 0; |
| 61 | \ActionScheduler_DataController::set_free_ticks( $free_on ); |
| 62 | \ActionScheduler_DataController::set_sleep_time( $sleep ); |
| 63 | do { |
| 64 | $actions_processed = $runner->run( $batch_size ); |
| 65 | $this->total_processed += $actions_processed; |
| 66 | } while ( $actions_processed > 0 ); |
| 67 | if ( ! $config->get_dry_run() ) { |
| 68 | // let the scheduler know that there's nothing left to do. |
| 69 | $scheduler = new Scheduler(); |
| 70 | $scheduler->mark_complete(); |
| 71 | } |
| 72 | WP_CLI::success( sprintf( '%s complete. %d actions processed.', $config->get_dry_run() ? 'Dry run' : 'Migration', $this->total_processed ) ); |
| 73 | } |
| 74 | private function get_migration_config( $args ) { |
| 75 | $args = wp_parse_args( |
| 76 | $args, |
| 77 | array( |
| 78 | 'dry-run' => false, |
| 79 | ) |
| 80 | ); |
| 81 | $config = Controller::instance()->get_migration_config_object(); |
| 82 | $config->set_dry_run( ! empty( $args['dry-run'] ) ); |
| 83 | return $config; |
| 84 | } |
| 85 | private function init_logging() { |
| 86 | add_action( |
| 87 | 'action_scheduler/migrate_action_dry_run', |
| 88 | function ( $action_id ) { |
| 89 | WP_CLI::debug( sprintf( 'Dry-run: migrated action %d', $action_id ) ); |
| 90 | } |
| 91 | ); |
| 92 | add_action( |
| 93 | 'action_scheduler/no_action_to_migrate', |
| 94 | function ( $action_id ) { |
| 95 | WP_CLI::debug( sprintf( 'No action found to migrate for ID %d', $action_id ) ); |
| 96 | } |
| 97 | ); |
| 98 | add_action( |
| 99 | 'action_scheduler/migrate_action_failed', |
| 100 | function ( $action_id ) { |
| 101 | WP_CLI::warning( sprintf( 'Failed migrating action with ID %d', $action_id ) ); |
| 102 | } |
| 103 | ); |
| 104 | add_action( |
| 105 | 'action_scheduler/migrate_action_incomplete', |
| 106 | function ( $source_id, $destination_id ) { |
| 107 | WP_CLI::warning( sprintf( 'Unable to remove source action with ID %d after migrating to new ID %d', $source_id, $destination_id ) ); |
| 108 | }, |
| 109 | 10, |
| 110 | 2 |
| 111 | ); |
| 112 | add_action( |
| 113 | 'action_scheduler/migrated_action', |
| 114 | function ( $source_id, $destination_id ) { |
| 115 | WP_CLI::debug( sprintf( 'Migrated source action with ID %d to new store with ID %d', $source_id, $destination_id ) ); |
| 116 | }, |
| 117 | 10, |
| 118 | 2 |
| 119 | ); |
| 120 | add_action( |
| 121 | 'action_scheduler/migration_batch_starting', |
| 122 | function ( $batch ) { |
| 123 | WP_CLI::debug( 'Beginning migration of batch: ' . print_r( $batch, true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r |
| 124 | } |
| 125 | ); |
| 126 | add_action( |
| 127 | 'action_scheduler/migration_batch_complete', |
| 128 | function ( $batch ) { |
| 129 | WP_CLI::log( sprintf( 'Completed migration of %d actions', count( $batch ) ) ); |
| 130 | } |
| 131 | ); |
| 132 | } |
| 133 | } |
| 134 |