class-orchestrate.php 2.38 KB
Newer Older
1
2
3
4
5
<?php

namespace Automattic\WP\Cron_Control\CLI;

/**
6
 * Commands used by the Go-based runner to execute events
7
8
9
 */
class Orchestrate extends \WP_CLI_Command {
	/**
10
	 * List the next set of events to run
11
	 *
12
	 * Will not be all events, just those atop the curated queue
13
	 *
14
	 * @subcommand list-due-batch
15
	 */
16
17
18
19
20
21
22
23
24
25
	public function list_due_now( $args, $assoc_args ) {
		$events = \Automattic\WP\Cron_Control\Events::instance()->get_events();

		$format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'table' );

		\WP_CLI\Utils\format_items( $format, $events['events'], array(
			'timestamp',
			'action',
			'instance',
		) );
26
	}
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

	/**
	 * Run a given event
	 *
	 * @subcommand run
	 * @synopsis --timestamp=<timestamp> --action=<action-hashed> --instance=<instance>
	 */
	public function run_event( $args, $assoc_args ) {
		$timestamp = \WP_CLI\Utils\get_flag_value( $assoc_args, 'timestamp', null );
		$action    = \WP_CLI\Utils\get_flag_value( $assoc_args, 'action',    null );
		$instance  = \WP_CLI\Utils\get_flag_value( $assoc_args, 'instance',  null );

		if ( ! is_numeric( $timestamp ) || ! is_string( $action ) || ! is_string( $instance ) ) {
			\WP_CLI::error( __( 'Invalid event arguments', 'automattic-cron-control' ) );
		}

		// Prepare environment
		if ( ! defined( 'DOING_CRON' ) ) {
			define( 'DOING_CRON', true );
		}

		$now = time();
		if ( $timestamp > $now ) {
			\WP_CLI::error( sprintf( __( 'This event is not scheduled to run until %1$s GMT', 'automattic-cron-control' ), date( TIME_FORMAT, $timestamp ) ) );
		}

		// Run the event
		$run = \Automattic\WP\Cron_Control\run_event( $timestamp, $action, $instance );

		if ( is_wp_error( $run ) ) {
			\WP_CLI::error( $run->get_error_message() );
		} elseif ( is_array( $run ) && isset( $run['success'] ) && true === $run['success'] ) {
			\WP_CLI::success( $run['message'] );
		} else {
			\WP_CLI::error( $run['message'] );
		}
	}
64
65
66
67
68
69
70
71
72

	/**
	 * Get some details needed to execute events
	 *
	 * @subcommand get-info
	 */
	public function get_info( $args, $assoc_args ) {
		$info = array(
			array(
73
				'multisite' => is_multisite() ? 1 : 0,
74
75
76
77
78
79
80
81
82
83
84
				'siteurl'   => site_url(),
			),
		);

		$format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'table' );

		\WP_CLI\Utils\format_items( $format, $info, array(
			'multisite',
			'siteurl',
		) );
	}
85
86
87
}

\WP_CLI::add_command( 'cron-control orchestrate', 'Automattic\WP\Cron_Control\CLI\Orchestrate' );