class-orchestrate-runner.php 4.19 KB
Newer Older
1
<?php
Erick Hitter's avatar
Erick Hitter committed
2
3
4
5
6
7
8
/**
 * Execute cron via WP-CLI
 *
 * Not intended for human use, rather it powers the Go-based Runner. Use the `events` command instead.
 *
 * @package a8c_Cron_Control
 */
9
10
11
12

namespace Automattic\WP\Cron_Control\CLI;

/**
13
 * Commands used by the Go-based runner to execute events
14
 */
15
class Orchestrate_Runner extends \WP_CLI_Command {
16
	/**
Erick Hitter's avatar
Clarify    
Erick Hitter committed
17
	 * List the next set of events to run; meant for Runner
18
	 *
19
	 * Will not be all events, just those atop the curated queue
20
	 *
Erick Hitter's avatar
Clarify    
Erick Hitter committed
21
22
	 * Not intended for human use, rather it powers the Go-based Runner. Use the `events list` command instead.
	 *
23
	 * @subcommand list-due-batch
Erick Hitter's avatar
Erick Hitter committed
24
25
	 * @param array $args Array of positional arguments.
	 * @param array $assoc_args Array of flags.
26
	 */
27
	public function list_due_now( $args, $assoc_args ) {
28
29
30
31
		if ( 0 !== \Automattic\WP\Cron_Control\Events::instance()->run_disabled() ) {
			\WP_CLI::error( __( 'Automatic event execution is disabled', 'automattic-cron-control' ) );
		}

32
33
34
35
36
37
38
39
40
		$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',
		) );
41
	}
42
43

	/**
Erick Hitter's avatar
Clarify    
Erick Hitter committed
44
45
46
	 * Run a given event; meant for Runner
	 *
	 * Not intended for human use, rather it powers the Go-based Runner. Use the `events run` command instead.
47
48
49
	 *
	 * @subcommand run
	 * @synopsis --timestamp=<timestamp> --action=<action-hashed> --instance=<instance>
Erick Hitter's avatar
Erick Hitter committed
50
51
	 * @param array $args Array of positional arguments.
	 * @param array $assoc_args Array of flags.
52
53
	 */
	public function run_event( $args, $assoc_args ) {
54
55
56
57
		if ( 0 !== \Automattic\WP\Cron_Control\Events::instance()->run_disabled() ) {
			\WP_CLI::error( __( 'Automatic event execution is disabled', 'automattic-cron-control' ) );
		}

58
59
60
61
		$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 );

62
63
64
65
66
67
68
69
		if ( ! is_numeric( $timestamp ) ) {
			\WP_CLI::error( __( 'Invalid timestamp', 'automattic-cron-control' ) );
		}

		if ( ! is_string( $action ) ) {
			\WP_CLI::error( __( 'Invalid action', 'automattic-cron-control' ) );
		}

Erick Hitter's avatar
Erick Hitter committed
70
		if ( ! is_string( $instance ) ) {
71
			\WP_CLI::error( __( 'Invalid instance', 'automattic-cron-control' ) );
72
73
74
75
		}

		$now = time();
		if ( $timestamp > $now ) {
Erick Hitter's avatar
Erick Hitter committed
76
			/* translators: 1: Event execution time in UTC, 2: Human time diff */
77
			\WP_CLI::error( sprintf( __( 'Given timestamp is for %1$s UTC, %2$s from now. The event\'s existence was not confirmed, and no attempt was made to execute it.', 'automattic-cron-control' ), date_i18n( TIME_FORMAT, $timestamp ), human_time_diff( $now, $timestamp ) ) );
78
79
		}

Erick Hitter's avatar
Erick Hitter committed
80
		// Prepare environment.
81
82
		\Automattic\WP\Cron_Control\set_doing_cron();

Erick Hitter's avatar
Erick Hitter committed
83
		// Run the event.
84
85
86
		$run = \Automattic\WP\Cron_Control\run_event( $timestamp, $action, $instance );

		if ( is_wp_error( $run ) ) {
87
88
89
			$error_data = $run->get_error_data();

			if ( isset( $error_data['status'] ) && 404 === $error_data['status'] ) {
90
91
92
				\WP_CLI::warning( $run->get_error_message() );

				exit;
93
94
95
			} else {
				\WP_CLI::error( $run->get_error_message() );
			}
96
		} elseif ( isset( $run['success'] ) && true === $run['success'] ) {
97
98
99
100
101
			\WP_CLI::success( $run['message'] );
		} else {
			\WP_CLI::error( $run['message'] );
		}
	}
102
103

	/**
Erick Hitter's avatar
Clarify    
Erick Hitter committed
104
105
106
	 * Get some details needed to execute events; meant for Runner
	 *
	 * Not intended for human use, rather it powers the Go-based Runner. Use the `orchestrate manage-automatic-execution` command instead.
107
108
	 *
	 * @subcommand get-info
Erick Hitter's avatar
Erick Hitter committed
109
110
	 * @param array $args Array of positional arguments.
	 * @param array $assoc_args Array of flags.
111
112
113
114
	 */
	public function get_info( $args, $assoc_args ) {
		$info = array(
			array(
115
				'multisite' => is_multisite() ? 1 : 0,
116
				'siteurl'   => site_url(),
117
				'disabled'  => \Automattic\WP\Cron_Control\Events::instance()->run_disabled(),
118
119
120
121
122
			),
		);

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

123
		\WP_CLI\Utils\format_items( $format, $info, array_keys( $info[0] ) );
124
	}
125
126
}

127
\WP_CLI::add_command( 'cron-control orchestrate runner-only', 'Automattic\WP\Cron_Control\CLI\Orchestrate_Runner' );