Skip to content

Instantly share code, notes, and snippets.

@barryhughes
Last active April 20, 2021 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barryhughes/2ee381bbd7c7a973e120baaf396e3ebd to your computer and use it in GitHub Desktop.
Save barryhughes/2ee381bbd7c7a973e120baaf396e3ebd to your computer and use it in GitHub Desktop.
Handy dandy testing tool; used to generate fake scheduled actions [WP CLI | Action Scheduler]
<?php
/**
* Generates a set of scheduled actions for testing purposes.
*
* Additionally, it provides test callbacks (which do very little except burn time).
* How much fun is that?
*
* An easy way to add this command is to place the file in the wp-content/mu-plugins
* directory, or else incldue it from a file in that directory.
*
* @version 2021.4
* @license GPL-3.0
*/
namespace Harry_Bewes\Action_Scheduler;
use DateTime;
use WP_CLI;
use function WP_CLI\Utils\make_progress_bar;
if ( ! class_exists( 'WP_CLI' ) ) {
return;
}
/**
* Generates scheduled actions for testing purposes.
*
* ## OPTIONS
*
* <total>
* : Total number of scheduled action to generate
*
* [--from=<date_string>]
* : The earliest date for which actions will be scheduled
*
* [--to=<date_string>]
* : The latest date for which actions will be scheduled.
*
* ## EXAMPLES
*
* wp action-scheduler generate 1000
*
* @when after_wp_load
*/
function generate( array $args, array $assoc_args ) {
safety_check();
$total = $args[0];
$default_from = time();
$default_to = time() + MONTH_IN_SECONDS;
$from = isset( $assoc_args['from'] )
? try_to_extract_timestamp( $assoc_args['from'], $default_from )
: $default_from;
$to = isset( $assoc_args['to'] )
? try_to_extract_timestamp( $assoc_args['to'], $default_to )
: $default_to;
$progress_bar = make_progress_bar( 'Generating', $total );
$progress_bar->display();
for ( $i = 0; $i <= $total; $i++ ) {
as_schedule_single_action(
get_timestamp_in_range( $from, $to ),
get_test_hook(),
get_set_of_args(),
get_test_group()
);
$progress_bar->tick();
}
$progress_bar->finish();
}
function safety_check() {
if ( ! function_exists( 'as_schedule_single_action' ) ) {
WP_CLI::error( 'Oh oh! Action Scheduler is unavailable.' );
}
}
function get_timestamp_in_range( int $from, int $to ): int {
if ( $from === $to ) {
return $from;
}
if ( $to < $from ) {
$swap = $from;
$to = $from;
$from = $swap;
}
return (int) rand( $from, $to );
}
function get_test_hook(): string {
$chance = rand( 1, 100 );
// Mostly return normal test actions.
if ( $chance < 70 ) {
return 'as_test_normal';
}
// Return some that are especially fast.
if ( $chance < 90 ) {
return 'as_test_fast';
}
// Return a few that may take a long time to execute.
return 'as_test_slow';
}
function get_set_of_args() {
return [
'uid' => rand(1, 2000000),
'task' => rand(1, 2000000),
];
}
function get_test_group() {
switch ( rand( 0, 2 ) ) {
case 0: return 'test_group_alpha';
case 1: return 'test_group_bravo';
case 2: return 'test_group_charlie';
}
}
function try_to_extract_timestamp( $some_value, $default ) {
try {
return ( new DateTime( $some_value ) )->format( 'U' );
} catch ( Exception $e ) {
return $default;
}
}
// A 'fast' callback
add_action( 'as_test_fast', function() {} );
// A 'normal' callback will execute in 2 seconds or less.
add_action( 'as_test_normal', function() {
sleep( rand( 0, 2 ) );
} );
// A 'slow' callback will execute in the range 2-20 seconds.
add_action( 'as_test_slow', function() {
sleep( rand( 2, 20 ) );
} );
WP_CLI::add_command( 'action-scheduler generate', __NAMESPACE__ . '\generate' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment