| 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 |
/** |
| 25 |
* Number of actions migrated. |
| 26 |
* |
| 27 |
* @var int |
| 28 |
*/ |
| 29 |
private $total_processed = 0; |
| 30 |
|
| 31 |
/** |
| 32 |
* Register the command with WP-CLI |
| 33 |
*/ |
| 34 |
public function register() { |
| 35 |
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 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 |
); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Process the data migration. |
| 79 |
* |
| 80 |
* @param array $positional_args Required for WP CLI. Not used in migration. |
| 81 |
* @param array $assoc_args Optional arguments. |
| 82 |
* |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
public function migrate( $positional_args, $assoc_args ) { |
| 86 |
$this->init_logging(); |
| 87 |
|
| 88 |
$config = $this->get_migration_config( $assoc_args ); |
| 89 |
$runner = new Runner( $config ); |
| 90 |
$runner->init_destination(); |
| 91 |
|
| 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; |
| 95 |
\ActionScheduler_DataController::set_free_ticks( $free_on ); |
| 96 |
\ActionScheduler_DataController::set_sleep_time( $sleep ); |
| 97 |
|
| 98 |
do { |
| 99 |
$actions_processed = $runner->run( $batch_size ); |
| 100 |
$this->total_processed += $actions_processed; |
| 101 |
} while ( $actions_processed > 0 ); |
| 102 |
|
| 103 |
if ( ! $config->get_dry_run() ) { |
| 104 |
// let the scheduler know that there's nothing left to do. |
| 105 |
$scheduler = new Scheduler(); |
| 106 |
$scheduler->mark_complete(); |
| 107 |
} |
| 108 |
|
| 109 |
WP_CLI::success( sprintf( '%s complete. %d actions processed.', $config->get_dry_run() ? 'Dry run' : 'Migration', $this->total_processed ) ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Build the config object used to create the Runner |
| 114 |
* |
| 115 |
* @param array $args Optional arguments. |
| 116 |
* |
| 117 |
* @return ActionScheduler\Migration\Config |
| 118 |
*/ |
| 119 |
private function get_migration_config( $args ) { |
| 120 |
$args = wp_parse_args( |
| 121 |
$args, |
| 122 |
array( |
| 123 |
'dry-run' => false, |
| 124 |
) |
| 125 |
); |
| 126 |
|
| 127 |
$config = Controller::instance()->get_migration_config_object(); |
| 128 |
$config->set_dry_run( ! empty( $args['dry-run'] ) ); |
| 129 |
|
| 130 |
return $config; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Hook command line logging into migration actions. |
| 135 |
*/ |
| 136 |
private function init_logging() { |
| 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 |
); |
| 189 |
} |
| 190 |
} |
| 191 |
|