Initial Commit
This commit is contained in:
125
dependencies/woocommerce/action-scheduler/classes/WP_CLI/ActionScheduler_WPCLI_Clean_Command.php
vendored
Normal file
125
dependencies/woocommerce/action-scheduler/classes/WP_CLI/ActionScheduler_WPCLI_Clean_Command.php
vendored
Normal file
@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Commands for Action Scheduler.
|
||||
*/
|
||||
class ActionScheduler_WPCLI_Clean_Command extends WP_CLI_Command {
|
||||
/**
|
||||
* Run the Action Scheduler Queue Cleaner
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* [--batch-size=<size>]
|
||||
* : The maximum number of actions to delete per batch. Defaults to 20.
|
||||
*
|
||||
* [--batches=<size>]
|
||||
* : Limit execution to a number of batches. Defaults to 0, meaning batches will continue all eligible actions are deleted.
|
||||
*
|
||||
* [--status=<status>]
|
||||
* : Only clean actions with the specified status. Defaults to Canceled, Completed. Define multiple statuses as a comma separated string (without spaces), e.g. `--status=complete,failed,canceled`
|
||||
*
|
||||
* [--before=<datestring>]
|
||||
* : Only delete actions with scheduled date older than this. Defaults to 31 days. e.g `--before='7 days ago'`, `--before='02-Feb-2020 20:20:20'`
|
||||
*
|
||||
* [--pause=<seconds>]
|
||||
* : The number of seconds to pause between batches. Default no pause.
|
||||
*
|
||||
* @param array $args Positional arguments.
|
||||
* @param array $assoc_args Keyed arguments.
|
||||
* @throws \WP_CLI\ExitException When an error occurs.
|
||||
*
|
||||
* @subcommand clean
|
||||
*/
|
||||
public function clean( $args, $assoc_args ) {
|
||||
// Handle passed arguments.
|
||||
$batch = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batch-size', 20 ) );
|
||||
$batches = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batches', 0 ) );
|
||||
$status = explode( ',', WP_CLI\Utils\get_flag_value( $assoc_args, 'status', '' ) );
|
||||
$status = array_filter( array_map( 'trim', $status ) );
|
||||
$before = \WP_CLI\Utils\get_flag_value( $assoc_args, 'before', '' );
|
||||
$sleep = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pause', 0 );
|
||||
|
||||
$batches_completed = 0;
|
||||
$actions_deleted = 0;
|
||||
$unlimited = $batches === 0;
|
||||
try {
|
||||
$lifespan = as_get_datetime_object( $before );
|
||||
} catch ( Exception $e ) {
|
||||
$lifespan = null;
|
||||
}
|
||||
|
||||
try {
|
||||
// Custom queue cleaner instance.
|
||||
$cleaner = new ActionScheduler_QueueCleaner( null, $batch );
|
||||
|
||||
// Clean actions for as long as possible.
|
||||
while ( $unlimited || $batches_completed < $batches ) {
|
||||
if ( $sleep && $batches_completed > 0 ) {
|
||||
sleep( $sleep );
|
||||
}
|
||||
|
||||
$deleted = count( $cleaner->clean_actions( $status, $lifespan, null,'CLI' ) );
|
||||
if ( $deleted <= 0 ) {
|
||||
break;
|
||||
}
|
||||
$actions_deleted += $deleted;
|
||||
$batches_completed++;
|
||||
$this->print_success( $deleted );
|
||||
}
|
||||
} catch ( Exception $e ) {
|
||||
$this->print_error( $e );
|
||||
}
|
||||
|
||||
$this->print_total_batches( $batches_completed );
|
||||
if ( $batches_completed > 1 ) {
|
||||
$this->print_success( $actions_deleted );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Print WP CLI message about how many batches of actions were processed.
|
||||
*
|
||||
* @param int $batches_processed
|
||||
*/
|
||||
protected function print_total_batches( int $batches_processed ) {
|
||||
WP_CLI::log(
|
||||
sprintf(
|
||||
/* translators: %d refers to the total number of batches processed */
|
||||
_n( '%d batch processed.', '%d batches processed.', $batches_processed, 'action-scheduler' ),
|
||||
$batches_processed
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an exception into a WP CLI error.
|
||||
*
|
||||
* @param Exception $e The error object.
|
||||
*
|
||||
* @throws \WP_CLI\ExitException
|
||||
*/
|
||||
protected function print_error( Exception $e ) {
|
||||
WP_CLI::error(
|
||||
sprintf(
|
||||
/* translators: %s refers to the exception error message */
|
||||
__( 'There was an error deleting an action: %s', 'action-scheduler' ),
|
||||
$e->getMessage()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a success message with the number of completed actions.
|
||||
*
|
||||
* @param int $actions_deleted
|
||||
*/
|
||||
protected function print_success( int $actions_deleted ) {
|
||||
WP_CLI::success(
|
||||
sprintf(
|
||||
/* translators: %d refers to the total number of actions deleted */
|
||||
_n( '%d action deleted.', '%d actions deleted.', $actions_deleted, 'action-scheduler' ),
|
||||
$actions_deleted
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
197
dependencies/woocommerce/action-scheduler/classes/WP_CLI/ActionScheduler_WPCLI_QueueRunner.php
vendored
Normal file
197
dependencies/woocommerce/action-scheduler/classes/WP_CLI/ActionScheduler_WPCLI_QueueRunner.php
vendored
Normal file
@ -0,0 +1,197 @@
|
||||
<?php
|
||||
|
||||
use Action_Scheduler\WP_CLI\ProgressBar;
|
||||
|
||||
/**
|
||||
* WP CLI Queue runner.
|
||||
*
|
||||
* This class can only be called from within a WP CLI instance.
|
||||
*/
|
||||
class ActionScheduler_WPCLI_QueueRunner extends ActionScheduler_Abstract_QueueRunner {
|
||||
|
||||
/** @var array */
|
||||
protected $actions;
|
||||
|
||||
/** @var ActionScheduler_ActionClaim */
|
||||
protected $claim;
|
||||
|
||||
/** @var \cli\progress\Bar */
|
||||
protected $progress_bar;
|
||||
|
||||
/**
|
||||
* ActionScheduler_WPCLI_QueueRunner constructor.
|
||||
*
|
||||
* @param ActionScheduler_Store $store
|
||||
* @param ActionScheduler_FatalErrorMonitor $monitor
|
||||
* @param ActionScheduler_QueueCleaner $cleaner
|
||||
*
|
||||
* @throws Exception When this is not run within WP CLI
|
||||
*/
|
||||
public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null ) {
|
||||
if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) ) {
|
||||
/* translators: %s php class name */
|
||||
throw new Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'action-scheduler' ), __CLASS__ ) );
|
||||
}
|
||||
|
||||
parent::__construct( $store, $monitor, $cleaner );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up the Queue before processing.
|
||||
*
|
||||
* @author Jeremy Pry
|
||||
*
|
||||
* @param int $batch_size The batch size to process.
|
||||
* @param array $hooks The hooks being used to filter the actions claimed in this batch.
|
||||
* @param string $group The group of actions to claim with this batch.
|
||||
* @param bool $force Whether to force running even with too many concurrent processes.
|
||||
*
|
||||
* @return int The number of actions that will be run.
|
||||
* @throws \WP_CLI\ExitException When there are too many concurrent batches.
|
||||
*/
|
||||
public function setup( $batch_size, $hooks = array(), $group = '', $force = false ) {
|
||||
$this->run_cleanup();
|
||||
$this->add_hooks();
|
||||
|
||||
// Check to make sure there aren't too many concurrent processes running.
|
||||
if ( $this->has_maximum_concurrent_batches() ) {
|
||||
if ( $force ) {
|
||||
WP_CLI::warning( __( 'There are too many concurrent batches, but the run is forced to continue.', 'action-scheduler' ) );
|
||||
} else {
|
||||
WP_CLI::error( __( 'There are too many concurrent batches.', 'action-scheduler' ) );
|
||||
}
|
||||
}
|
||||
|
||||
// Stake a claim and store it.
|
||||
$this->claim = $this->store->stake_claim( $batch_size, null, $hooks, $group );
|
||||
$this->monitor->attach( $this->claim );
|
||||
$this->actions = $this->claim->get_actions();
|
||||
|
||||
return count( $this->actions );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add our hooks to the appropriate actions.
|
||||
*
|
||||
* @author Jeremy Pry
|
||||
*/
|
||||
protected function add_hooks() {
|
||||
add_action( 'action_scheduler_before_execute', array( $this, 'before_execute' ) );
|
||||
add_action( 'action_scheduler_after_execute', array( $this, 'after_execute' ), 10, 2 );
|
||||
add_action( 'action_scheduler_failed_execution', array( $this, 'action_failed' ), 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up the WP CLI progress bar.
|
||||
*
|
||||
* @author Jeremy Pry
|
||||
*/
|
||||
protected function setup_progress_bar() {
|
||||
$count = count( $this->actions );
|
||||
$this->progress_bar = new ProgressBar(
|
||||
/* translators: %d: amount of actions */
|
||||
sprintf( _n( 'Running %d action', 'Running %d actions', $count, 'action-scheduler' ), $count ),
|
||||
$count
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process actions in the queue.
|
||||
*
|
||||
* @author Jeremy Pry
|
||||
*
|
||||
* @param string $context Optional runner context. Default 'WP CLI'.
|
||||
*
|
||||
* @return int The number of actions processed.
|
||||
*/
|
||||
public function run( $context = 'WP CLI' ) {
|
||||
do_action( 'action_scheduler_before_process_queue' );
|
||||
$this->setup_progress_bar();
|
||||
foreach ( $this->actions as $action_id ) {
|
||||
// Error if we lost the claim.
|
||||
if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $this->claim->get_id() ) ) ) {
|
||||
WP_CLI::warning( __( 'The claim has been lost. Aborting current batch.', 'action-scheduler' ) );
|
||||
break;
|
||||
}
|
||||
|
||||
$this->process_action( $action_id, $context );
|
||||
$this->progress_bar->tick();
|
||||
}
|
||||
|
||||
$completed = $this->progress_bar->current();
|
||||
$this->progress_bar->finish();
|
||||
$this->store->release_claim( $this->claim );
|
||||
do_action( 'action_scheduler_after_process_queue' );
|
||||
|
||||
return $completed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle WP CLI message when the action is starting.
|
||||
*
|
||||
* @author Jeremy Pry
|
||||
*
|
||||
* @param $action_id
|
||||
*/
|
||||
public function before_execute( $action_id ) {
|
||||
/* translators: %s refers to the action ID */
|
||||
WP_CLI::log( sprintf( __( 'Started processing action %s', 'action-scheduler' ), $action_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle WP CLI message when the action has completed.
|
||||
*
|
||||
* @author Jeremy Pry
|
||||
*
|
||||
* @param int $action_id
|
||||
* @param null|ActionScheduler_Action $action The instance of the action. Default to null for backward compatibility.
|
||||
*/
|
||||
public function after_execute( $action_id, $action = null ) {
|
||||
// backward compatibility
|
||||
if ( null === $action ) {
|
||||
$action = $this->store->fetch_action( $action_id );
|
||||
}
|
||||
/* translators: 1: action ID 2: hook name */
|
||||
WP_CLI::log( sprintf( __( 'Completed processing action %1$s with hook: %2$s', 'action-scheduler' ), $action_id, $action->get_hook() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle WP CLI message when the action has failed.
|
||||
*
|
||||
* @author Jeremy Pry
|
||||
*
|
||||
* @param int $action_id
|
||||
* @param Exception $exception
|
||||
* @throws \WP_CLI\ExitException With failure message.
|
||||
*/
|
||||
public function action_failed( $action_id, $exception ) {
|
||||
WP_CLI::error(
|
||||
/* translators: 1: action ID 2: exception message */
|
||||
sprintf( __( 'Error processing action %1$s: %2$s', 'action-scheduler' ), $action_id, $exception->getMessage() ),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sleep and help avoid hitting memory limit
|
||||
*
|
||||
* @param int $sleep_time Amount of seconds to sleep
|
||||
* @deprecated 3.0.0
|
||||
*/
|
||||
protected function stop_the_insanity( $sleep_time = 0 ) {
|
||||
_deprecated_function( 'ActionScheduler_WPCLI_QueueRunner::stop_the_insanity', '3.0.0', 'ActionScheduler_DataController::free_memory' );
|
||||
|
||||
ActionScheduler_DataController::free_memory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe trigger the stop_the_insanity() method to free up memory.
|
||||
*/
|
||||
protected function maybe_stop_the_insanity() {
|
||||
// The value returned by progress_bar->current() might be padded. Remove padding, and convert to int.
|
||||
$current_iteration = intval( trim( $this->progress_bar->current() ) );
|
||||
if ( 0 === $current_iteration % 50 ) {
|
||||
$this->stop_the_insanity();
|
||||
}
|
||||
}
|
||||
}
|
210
dependencies/woocommerce/action-scheduler/classes/WP_CLI/ActionScheduler_WPCLI_Scheduler_command.php
vendored
Normal file
210
dependencies/woocommerce/action-scheduler/classes/WP_CLI/ActionScheduler_WPCLI_Scheduler_command.php
vendored
Normal file
@ -0,0 +1,210 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Commands for Action Scheduler.
|
||||
*/
|
||||
class ActionScheduler_WPCLI_Scheduler_command extends WP_CLI_Command {
|
||||
|
||||
/**
|
||||
* Force tables schema creation for Action Scheduler
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* @param array $args Positional arguments.
|
||||
* @param array $assoc_args Keyed arguments.
|
||||
*
|
||||
* @subcommand fix-schema
|
||||
*/
|
||||
public function fix_schema( $args, $assoc_args ) {
|
||||
$schema_classes = array( ActionScheduler_LoggerSchema::class, ActionScheduler_StoreSchema::class );
|
||||
|
||||
foreach ( $schema_classes as $classname ) {
|
||||
if ( is_subclass_of( $classname, ActionScheduler_Abstract_Schema::class ) ) {
|
||||
$obj = new $classname();
|
||||
$obj->init();
|
||||
$obj->register_tables( true );
|
||||
|
||||
WP_CLI::success(
|
||||
sprintf(
|
||||
/* translators: %s refers to the schema name*/
|
||||
__( 'Registered schema for %s', 'action-scheduler' ),
|
||||
$classname
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the Action Scheduler
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* [--batch-size=<size>]
|
||||
* : The maximum number of actions to run. Defaults to 100.
|
||||
*
|
||||
* [--batches=<size>]
|
||||
* : Limit execution to a number of batches. Defaults to 0, meaning batches will continue being executed until all actions are complete.
|
||||
*
|
||||
* [--cleanup-batch-size=<size>]
|
||||
* : The maximum number of actions to clean up. Defaults to the value of --batch-size.
|
||||
*
|
||||
* [--hooks=<hooks>]
|
||||
* : 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`
|
||||
*
|
||||
* [--group=<group>]
|
||||
* : Only run actions from the specified group. Omitting this option runs actions from all groups.
|
||||
*
|
||||
* [--exclude-groups=<groups>]
|
||||
* : Run actions from all groups except the specified group(s). Define multiple groups as a comma separated string (without spaces), e.g. '--group_a,group_b'. This option is ignored when `--group` is used.
|
||||
*
|
||||
* [--free-memory-on=<count>]
|
||||
* : The number of actions to process between freeing memory. 0 disables freeing memory. Default 50.
|
||||
*
|
||||
* [--pause=<seconds>]
|
||||
* : The number of seconds to pause when freeing memory. Default no pause.
|
||||
*
|
||||
* [--force]
|
||||
* : Whether to force execution despite the maximum number of concurrent processes being exceeded.
|
||||
*
|
||||
* @param array $args Positional arguments.
|
||||
* @param array $assoc_args Keyed arguments.
|
||||
* @throws \WP_CLI\ExitException When an error occurs.
|
||||
*
|
||||
* @subcommand run
|
||||
*/
|
||||
public function run( $args, $assoc_args ) {
|
||||
// Handle passed arguments.
|
||||
$batch = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batch-size', 100 ) );
|
||||
$batches = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batches', 0 ) );
|
||||
$clean = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'cleanup-batch-size', $batch ) );
|
||||
$hooks = explode( ',', WP_CLI\Utils\get_flag_value( $assoc_args, 'hooks', '' ) );
|
||||
$hooks = array_filter( array_map( 'trim', $hooks ) );
|
||||
$group = \WP_CLI\Utils\get_flag_value( $assoc_args, 'group', '' );
|
||||
$exclude_groups = \WP_CLI\Utils\get_flag_value( $assoc_args, 'exclude-groups', '' );
|
||||
$free_on = \WP_CLI\Utils\get_flag_value( $assoc_args, 'free-memory-on', 50 );
|
||||
$sleep = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pause', 0 );
|
||||
$force = \WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false );
|
||||
|
||||
ActionScheduler_DataController::set_free_ticks( $free_on );
|
||||
ActionScheduler_DataController::set_sleep_time( $sleep );
|
||||
|
||||
$batches_completed = 0;
|
||||
$actions_completed = 0;
|
||||
$unlimited = $batches === 0;
|
||||
if ( is_callable( [ ActionScheduler::store(), 'set_claim_filter' ] ) ) {
|
||||
$exclude_groups = $this->parse_comma_separated_string( $exclude_groups );
|
||||
|
||||
if ( ! empty( $exclude_groups ) ) {
|
||||
ActionScheduler::store()->set_claim_filter('exclude-groups', $exclude_groups );
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// Custom queue cleaner instance.
|
||||
$cleaner = new ActionScheduler_QueueCleaner( null, $clean );
|
||||
|
||||
// Get the queue runner instance
|
||||
$runner = new ActionScheduler_WPCLI_QueueRunner( null, null, $cleaner );
|
||||
|
||||
// Determine how many tasks will be run in the first batch.
|
||||
$total = $runner->setup( $batch, $hooks, $group, $force );
|
||||
|
||||
// Run actions for as long as possible.
|
||||
while ( $total > 0 ) {
|
||||
$this->print_total_actions( $total );
|
||||
$actions_completed += $runner->run();
|
||||
$batches_completed++;
|
||||
|
||||
// Maybe set up tasks for the next batch.
|
||||
$total = ( $unlimited || $batches_completed < $batches ) ? $runner->setup( $batch, $hooks, $group, $force ) : 0;
|
||||
}
|
||||
} catch ( Exception $e ) {
|
||||
$this->print_error( $e );
|
||||
}
|
||||
|
||||
$this->print_total_batches( $batches_completed );
|
||||
$this->print_success( $actions_completed );
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a string of comma-separated values into an array of those same values.
|
||||
*
|
||||
* @param string $string The string of one or more comma separated values.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function parse_comma_separated_string( $string ): array {
|
||||
return array_filter( str_getcsv( $string ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Print WP CLI message about how many actions are about to be processed.
|
||||
*
|
||||
* @author Jeremy Pry
|
||||
*
|
||||
* @param int $total
|
||||
*/
|
||||
protected function print_total_actions( $total ) {
|
||||
WP_CLI::log(
|
||||
sprintf(
|
||||
/* translators: %d refers to how many scheduled tasks were found to run */
|
||||
_n( 'Found %d scheduled task', 'Found %d scheduled tasks', $total, 'action-scheduler' ),
|
||||
$total
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print WP CLI message about how many batches of actions were processed.
|
||||
*
|
||||
* @author Jeremy Pry
|
||||
*
|
||||
* @param int $batches_completed
|
||||
*/
|
||||
protected function print_total_batches( $batches_completed ) {
|
||||
WP_CLI::log(
|
||||
sprintf(
|
||||
/* translators: %d refers to the total number of batches executed */
|
||||
_n( '%d batch executed.', '%d batches executed.', $batches_completed, 'action-scheduler' ),
|
||||
$batches_completed
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an exception into a WP CLI error.
|
||||
*
|
||||
* @author Jeremy Pry
|
||||
*
|
||||
* @param Exception $e The error object.
|
||||
*
|
||||
* @throws \WP_CLI\ExitException
|
||||
*/
|
||||
protected function print_error( Exception $e ) {
|
||||
WP_CLI::error(
|
||||
sprintf(
|
||||
/* translators: %s refers to the exception error message */
|
||||
__( 'There was an error running the action scheduler: %s', 'action-scheduler' ),
|
||||
$e->getMessage()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a success message with the number of completed actions.
|
||||
*
|
||||
* @author Jeremy Pry
|
||||
*
|
||||
* @param int $actions_completed
|
||||
*/
|
||||
protected function print_success( $actions_completed ) {
|
||||
WP_CLI::success(
|
||||
sprintf(
|
||||
/* translators: %d refers to the total number of tasks completed */
|
||||
_n( '%d scheduled task completed.', '%d scheduled tasks completed.', $actions_completed, 'action-scheduler' ),
|
||||
$actions_completed
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
148
dependencies/woocommerce/action-scheduler/classes/WP_CLI/Migration_Command.php
vendored
Normal file
148
dependencies/woocommerce/action-scheduler/classes/WP_CLI/Migration_Command.php
vendored
Normal file
@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Action_Scheduler\WP_CLI;
|
||||
|
||||
use Action_Scheduler\Migration\Config;
|
||||
use Action_Scheduler\Migration\Runner;
|
||||
use Action_Scheduler\Migration\Scheduler;
|
||||
use Action_Scheduler\Migration\Controller;
|
||||
use WP_CLI;
|
||||
use WP_CLI_Command;
|
||||
|
||||
/**
|
||||
* Class Migration_Command
|
||||
*
|
||||
* @package Action_Scheduler\WP_CLI
|
||||
*
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class Migration_Command extends WP_CLI_Command {
|
||||
|
||||
/** @var int */
|
||||
private $total_processed = 0;
|
||||
|
||||
/**
|
||||
* Register the command with WP-CLI
|
||||
*/
|
||||
public function register() {
|
||||
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
|
||||
return;
|
||||
}
|
||||
|
||||
WP_CLI::add_command( 'action-scheduler migrate', [ $this, 'migrate' ], [
|
||||
'shortdesc' => 'Migrates actions to the DB tables store',
|
||||
'synopsis' => [
|
||||
[
|
||||
'type' => 'assoc',
|
||||
'name' => 'batch-size',
|
||||
'optional' => true,
|
||||
'default' => 100,
|
||||
'description' => 'The number of actions to process in each batch',
|
||||
],
|
||||
[
|
||||
'type' => 'assoc',
|
||||
'name' => 'free-memory-on',
|
||||
'optional' => true,
|
||||
'default' => 50,
|
||||
'description' => 'The number of actions to process between freeing memory. 0 disables freeing memory',
|
||||
],
|
||||
[
|
||||
'type' => 'assoc',
|
||||
'name' => 'pause',
|
||||
'optional' => true,
|
||||
'default' => 0,
|
||||
'description' => 'The number of seconds to pause when freeing memory',
|
||||
],
|
||||
[
|
||||
'type' => 'flag',
|
||||
'name' => 'dry-run',
|
||||
'optional' => true,
|
||||
'description' => 'Reports on the actions that would have been migrated, but does not change any data',
|
||||
],
|
||||
],
|
||||
] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the data migration.
|
||||
*
|
||||
* @param array $positional_args Required for WP CLI. Not used in migration.
|
||||
* @param array $assoc_args Optional arguments.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function migrate( $positional_args, $assoc_args ) {
|
||||
$this->init_logging();
|
||||
|
||||
$config = $this->get_migration_config( $assoc_args );
|
||||
$runner = new Runner( $config );
|
||||
$runner->init_destination();
|
||||
|
||||
$batch_size = isset( $assoc_args[ 'batch-size' ] ) ? (int) $assoc_args[ 'batch-size' ] : 100;
|
||||
$free_on = isset( $assoc_args[ 'free-memory-on' ] ) ? (int) $assoc_args[ 'free-memory-on' ] : 50;
|
||||
$sleep = isset( $assoc_args[ 'pause' ] ) ? (int) $assoc_args[ 'pause' ] : 0;
|
||||
\ActionScheduler_DataController::set_free_ticks( $free_on );
|
||||
\ActionScheduler_DataController::set_sleep_time( $sleep );
|
||||
|
||||
do {
|
||||
$actions_processed = $runner->run( $batch_size );
|
||||
$this->total_processed += $actions_processed;
|
||||
} while ( $actions_processed > 0 );
|
||||
|
||||
if ( ! $config->get_dry_run() ) {
|
||||
// let the scheduler know that there's nothing left to do
|
||||
$scheduler = new Scheduler();
|
||||
$scheduler->mark_complete();
|
||||
}
|
||||
|
||||
WP_CLI::success( sprintf( '%s complete. %d actions processed.', $config->get_dry_run() ? 'Dry run' : 'Migration', $this->total_processed ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the config object used to create the Runner
|
||||
*
|
||||
* @param array $args Optional arguments.
|
||||
*
|
||||
* @return ActionScheduler\Migration\Config
|
||||
*/
|
||||
private function get_migration_config( $args ) {
|
||||
$args = wp_parse_args( $args, [
|
||||
'dry-run' => false,
|
||||
] );
|
||||
|
||||
$config = Controller::instance()->get_migration_config_object();
|
||||
$config->set_dry_run( ! empty( $args[ 'dry-run' ] ) );
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook command line logging into migration actions.
|
||||
*/
|
||||
private function init_logging() {
|
||||
add_action( 'action_scheduler/migrate_action_dry_run', function ( $action_id ) {
|
||||
WP_CLI::debug( sprintf( 'Dry-run: migrated action %d', $action_id ) );
|
||||
}, 10, 1 );
|
||||
add_action( 'action_scheduler/no_action_to_migrate', function ( $action_id ) {
|
||||
WP_CLI::debug( sprintf( 'No action found to migrate for ID %d', $action_id ) );
|
||||
}, 10, 1 );
|
||||
add_action( 'action_scheduler/migrate_action_failed', function ( $action_id ) {
|
||||
WP_CLI::warning( sprintf( 'Failed migrating action with ID %d', $action_id ) );
|
||||
}, 10, 1 );
|
||||
add_action( 'action_scheduler/migrate_action_incomplete', function ( $source_id, $destination_id ) {
|
||||
WP_CLI::warning( sprintf( 'Unable to remove source action with ID %d after migrating to new ID %d', $source_id, $destination_id ) );
|
||||
}, 10, 2 );
|
||||
add_action( 'action_scheduler/migrated_action', function ( $source_id, $destination_id ) {
|
||||
WP_CLI::debug( sprintf( 'Migrated source action with ID %d to new store with ID %d', $source_id, $destination_id ) );
|
||||
}, 10, 2 );
|
||||
add_action( 'action_scheduler/migration_batch_starting', function ( $batch ) {
|
||||
WP_CLI::debug( 'Beginning migration of batch: ' . print_r( $batch, true ) );
|
||||
}, 10, 1 );
|
||||
add_action( 'action_scheduler/migration_batch_complete', function ( $batch ) {
|
||||
WP_CLI::log( sprintf( 'Completed migration of %d actions', count( $batch ) ) );
|
||||
}, 10, 1 );
|
||||
}
|
||||
}
|
119
dependencies/woocommerce/action-scheduler/classes/WP_CLI/ProgressBar.php
vendored
Normal file
119
dependencies/woocommerce/action-scheduler/classes/WP_CLI/ProgressBar.php
vendored
Normal file
@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace Action_Scheduler\WP_CLI;
|
||||
|
||||
/**
|
||||
* WP_CLI progress bar for Action Scheduler.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class ProgressBar
|
||||
*
|
||||
* @package Action_Scheduler\WP_CLI
|
||||
*
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class ProgressBar {
|
||||
|
||||
/** @var integer */
|
||||
protected $total_ticks;
|
||||
|
||||
/** @var integer */
|
||||
protected $count;
|
||||
|
||||
/** @var integer */
|
||||
protected $interval;
|
||||
|
||||
/** @var string */
|
||||
protected $message;
|
||||
|
||||
/** @var \cli\progress\Bar */
|
||||
protected $progress_bar;
|
||||
|
||||
/**
|
||||
* ProgressBar constructor.
|
||||
*
|
||||
* @param string $message Text to display before the progress bar.
|
||||
* @param integer $count Total number of ticks to be performed.
|
||||
* @param integer $interval Optional. The interval in milliseconds between updates. Default 100.
|
||||
*
|
||||
* @throws Exception When this is not run within WP CLI
|
||||
*/
|
||||
public function __construct( $message, $count, $interval = 100 ) {
|
||||
if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) ) {
|
||||
/* translators: %s php class name */
|
||||
throw new \Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'action-scheduler' ), __CLASS__ ) );
|
||||
}
|
||||
|
||||
$this->total_ticks = 0;
|
||||
$this->message = $message;
|
||||
$this->count = $count;
|
||||
$this->interval = $interval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment the progress bar ticks.
|
||||
*/
|
||||
public function tick() {
|
||||
if ( null === $this->progress_bar ) {
|
||||
$this->setup_progress_bar();
|
||||
}
|
||||
|
||||
$this->progress_bar->tick();
|
||||
$this->total_ticks++;
|
||||
|
||||
do_action( 'action_scheduler/progress_tick', $this->total_ticks );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the progress bar tick count.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function current() {
|
||||
return $this->progress_bar ? $this->progress_bar->current() : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finish the current progress bar.
|
||||
*/
|
||||
public function finish() {
|
||||
if ( null !== $this->progress_bar ) {
|
||||
$this->progress_bar->finish();
|
||||
}
|
||||
|
||||
$this->progress_bar = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the message used when creating the progress bar.
|
||||
*
|
||||
* @param string $message The message to be used when the next progress bar is created.
|
||||
*/
|
||||
public function set_message( $message ) {
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the count for a new progress bar.
|
||||
*
|
||||
* @param integer $count The total number of ticks expected to complete.
|
||||
*/
|
||||
public function set_count( $count ) {
|
||||
$this->count = $count;
|
||||
$this->finish();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up the progress bar.
|
||||
*/
|
||||
protected function setup_progress_bar() {
|
||||
$this->progress_bar = \WP_CLI\Utils\make_progress_bar(
|
||||
$this->message,
|
||||
$this->count,
|
||||
$this->interval
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user