Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions features/cron-event.feature
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,42 @@ Feature: Manage WP Cron events
"""
Debug: Arguments:
"""

Scenario: Run cron events with --network flag on non-multisite
When I try `wp cron event run --due-now --network`
Then STDERR should be:
"""
Error: This is not a multisite installation.
"""
And the return code should be 1

Scenario: Run cron events with --network flag on multisite
Given a WP multisite subdirectory install
And I run `wp site create --slug=site2`
And I run `wp site create --slug=site3`

When I run `wp cron event schedule wp_cli_network_test now`
Then STDOUT should contain:
"""
Success: Scheduled event with hook 'wp_cli_network_test'
"""

When I run `wp --url=example.com/site2 cron event schedule wp_cli_network_test_site2 now`
Then STDOUT should contain:
"""
Success: Scheduled event with hook 'wp_cli_network_test_site2'
"""

When I run `wp cron event run --due-now --network`
Then STDOUT should contain:
"""
Executed the cron event 'wp_cli_network_test'
"""
And STDOUT should contain:
"""
Executed the cron event 'wp_cli_network_test_site2'
"""
And STDOUT should contain:
"""
Success: Executed a total of 2 cron events across 3 sites.
"""
68 changes: 68 additions & 0 deletions src/Cron_Event_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,84 @@ public function schedule( $args, $assoc_args ) {
* [--all]
* : Run all hooks.
*
* [--network]
* : Run hooks across all sites in a multisite installation.
*
* ## EXAMPLES
*
* # Run all cron events due right now
* $ wp cron event run --due-now
* Executed the cron event 'cron_test_1' in 0.01s.
* Executed the cron event 'cron_test_2' in 0.006s.
* Success: Executed a total of 2 cron events.
*
* # Run all cron events due right now across all sites in a multisite
* $ wp cron event run --due-now --network
* Executed the cron event 'cron_test_1' in 0.01s.
* Executed the cron event 'cron_test_2' in 0.006s.
* Success: Executed a total of 2 cron events across 3 sites.
*/
public function run( $args, $assoc_args ) {

$network = Utils\get_flag_value( $assoc_args, 'network' );

if ( $network ) {
if ( ! is_multisite() ) {
WP_CLI::error( 'This is not a multisite installation.' );
}

$sites = get_sites(
array(
'fields' => 'ids',
'number' => 0,
)
);

if ( empty( $sites ) ) {
WP_CLI::error( 'No sites found in the network.' );
}

// Remove network flag before passing to get_selected_cron_events.
$network_assoc_args = $assoc_args;
unset( $network_assoc_args['network'] );

$total_executed = 0;
$site_count = count( $sites );

foreach ( $sites as $site_id ) {
switch_to_blog( $site_id );

$events = self::get_selected_cron_events( $args, $network_assoc_args );

if ( ! is_wp_error( $events ) ) {
foreach ( $events as $event ) {
$start = microtime( true );
$result = self::run_event( $event );
Comment thread
swissspidy marked this conversation as resolved.
Outdated
$total = round( microtime( true ) - $start, 3 );
++$total_executed;
WP_CLI::log( sprintf( "Executed the cron event '%s' in %ss.", $event->hook, $total ) );
if ( ! empty( $event->args ) ) {
WP_CLI::debug( sprintf( 'Arguments: %s', wp_json_encode( $event->args ) ), 'cron' );
}
}
Comment thread
swissspidy marked this conversation as resolved.
Outdated
} else {
WP_CLI::debug( sprintf( 'No events found for site %d: %s', $site_id, $events->get_error_message() ), 'cron' );
}

restore_current_blog();
}

$message = sprintf(
'Executed a total of %d %s across %d %s.',
$total_executed,
Utils\pluralize( 'cron event', $total_executed ),
$site_count,
Utils\pluralize( 'site', $site_count )
);
WP_CLI::success( $message );
return;
}

$events = self::get_selected_cron_events( $args, $assoc_args );

if ( is_wp_error( $events ) ) {
Expand Down
Loading