class-orchestrate-runner.php 4.87 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 {
	/**
Erick Hitter's avatar
Clarify    
Erick Hitter committed
10
	 * List the next set of events to run; meant for Runner
11
	 *
12
	 * Will not be all events, just those atop the curated queue
13
	 *
Erick Hitter's avatar
Clarify    
Erick Hitter committed
14
15
	 * Not intended for human use, rather it powers the Go-based Runner. Use the `events list` command instead.
	 *
16
	 * @subcommand list-due-batch
17
	 */
18
	public function list_due_now( $args, $assoc_args ) {
19
20
21
22
		if ( 0 !== \Automattic\WP\Cron_Control\Events::instance()->run_disabled() ) {
			\WP_CLI::error( __( 'Automatic event execution is disabled', 'automattic-cron-control' ) );
		}

23
24
25
26
27
28
29
30
31
		$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',
		) );
32
	}
33
34

	/**
Erick Hitter's avatar
Clarify    
Erick Hitter committed
35
36
37
	 * 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.
38
39
40
41
42
	 *
	 * @subcommand run
	 * @synopsis --timestamp=<timestamp> --action=<action-hashed> --instance=<instance>
	 */
	public function run_event( $args, $assoc_args ) {
43
44
45
46
		if ( 0 !== \Automattic\WP\Cron_Control\Events::instance()->run_disabled() ) {
			\WP_CLI::error( __( 'Automatic event execution is disabled', 'automattic-cron-control' ) );
		}

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
		$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'] );
		}
	}
76
77

	/**
Erick Hitter's avatar
Clarify    
Erick Hitter committed
78
79
80
	 * 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.
81
82
83
84
85
86
	 *
	 * @subcommand get-info
	 */
	public function get_info( $args, $assoc_args ) {
		$info = array(
			array(
87
				'multisite' => is_multisite() ? 1 : 0,
88
				'siteurl'   => site_url(),
89
				'disabled'  => \Automattic\WP\Cron_Control\Events::instance()->run_disabled(),
90
91
92
93
94
			),
		);

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

95
		\WP_CLI\Utils\format_items( $format, $info, array_keys( $info[0] ) );
96
	}
97
98

	/**
Erick Hitter's avatar
Clarify    
Erick Hitter committed
99
	 * Check and change status of automatic event execution
100
	 *
Erick Hitter's avatar
Clarify    
Erick Hitter committed
101
	 * When using the Go-based runner, it may be necessary to stop execution for a period, or indefinitely
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
	 *
	 * @subcommand manage-automatic-execution
	 * @synopsis [--enable] [--disable] [--disable_until=<disable_until>]
	 */
	public function toggle_event_execution( $args, $assoc_args ) {
		// Update execution status
		$disable_ts = \WP_CLI\Utils\get_flag_value( $assoc_args, 'disable_until', 0 );
		$disable_ts = absint( $disable_ts );

		if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'enable', false ) ) {
			update_option( \Automattic\WP\Cron_Control\Events::DISABLE_RUN_OPTION, 0 );
			\WP_CLI::success( 'Enabled' );
			return;
		} elseif ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'disable', false ) ) {
			update_option( \Automattic\WP\Cron_Control\Events::DISABLE_RUN_OPTION, 1 );
			\WP_CLI::success( 'Disabled' );
			return;
		} elseif( $disable_ts > 0 ) {
			if ( $disable_ts > time() ) {
				update_option( \Automattic\WP\Cron_Control\Events::DISABLE_RUN_OPTION, $disable_ts );
				\WP_CLI::success( sprintf( 'Disabled until %s', date( 'Y-m-d H:i:s T', $disable_ts ) ) );
				return;
			} else {
				\WP_CLI::error( 'Timestamp is in the past.' );
			}
		}

		// Display existing status
		$status = get_option( \Automattic\WP\Cron_Control\Events::DISABLE_RUN_OPTION, 0 );

		switch ( $status ) {
			case 0 :
Erick Hitter's avatar
Clarify    
Erick Hitter committed
134
				$status = 'Automatic execution is enabled';
135
136
137
				break;

			case 1 :
Erick Hitter's avatar
Clarify    
Erick Hitter committed
138
				$status = 'Automatic execution is disabled indefinitely';
139
140
141
				break;

			default :
Erick Hitter's avatar
Clarify    
Erick Hitter committed
142
				$status = sprintf( 'Automatic execution is disabled until %s', date( 'Y-m-d H:i:s T', $status ) );
143
144
145
146
147
				break;
		}

		\WP_CLI::log( $status );
	}
148
149
}

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