class-lock.php 3.94 KB
Newer Older
1
<?php
Erick Hitter's avatar
Erick Hitter committed
2
3
4
5
6
/**
 * Manage plugin's locks via WP-CLI
 *
 * @package a8c_Cron_Control
 */
7
8
9
10
11
12
13
14

namespace Automattic\WP\Cron_Control\CLI;

/**
 * Manage Cron Control's internal locks
 */
class Lock extends \WP_CLI_Command {
	/**
15
	 * Manage the lock that limits concurrent job executions
16
	 *
17
	 * @subcommand manage-run-lock
18
	 * @synopsis [--reset]
Erick Hitter's avatar
Erick Hitter committed
19
20
	 * @param array $args Array of positional arguments.
	 * @param array $assoc_args Array of flags.
21
	 */
22
	public function manage_run_lock( $args, $assoc_args ) {
23
24
		$lock_name        = \Automattic\WP\Cron_Control\Events::LOCK;
		$lock_limit       = \Automattic\WP\Cron_Control\JOB_CONCURRENCY_LIMIT;
25
26
27
28
29
		$lock_description = __( 'This lock limits the number of events run concurrently.', 'automattic-cron-control' );

		$this->get_reset_lock( $args, $assoc_args, $lock_name, $lock_limit, $lock_description );
	}

30
31
32
33
34
	/**
	 * Manage the lock that limits concurrent execution of jobs with the same action
	 *
	 * @subcommand manage-event-lock
	 * @synopsis <action> [--reset]
Erick Hitter's avatar
Erick Hitter committed
35
36
	 * @param array $args Array of positional arguments.
	 * @param array $assoc_args Array of flags.
37
38
39
40
41
42
	 */
	public function manage_event_lock( $args, $assoc_args ) {
		if ( empty( $args[0] ) ) {
			\WP_CLI::error( sprintf( __( 'Specify an action', 'automattic-cron-control' ) ) );
		}

Erick Hitter's avatar
Erick Hitter committed
43
		$lock_name        = \Automattic\WP\Cron_Control\Events::instance()->get_lock_key_for_event_action( (object) array( 'action' => $args[0] ) );
44
45
46
47
48
49
		$lock_limit       = 1;
		$lock_description = __( "This lock prevents concurrent executions of events with the same action, regardless of the action's arguments.", 'automattic-cron-control' );

		$this->get_reset_lock( $args, $assoc_args, $lock_name, $lock_limit, $lock_description );
	}

50
51
	/**
	 * Retrieve a lock's current value, or reset it
Erick Hitter's avatar
Erick Hitter committed
52
53
54
55
56
57
	 *
	 * @param array  $args Array of positional arguments.
	 * @param array  $assoc_args Array of flags.
	 * @param string $lock_name Name of lock to reset.
	 * @param int    $lock_limit Lock's maximum concurrency.
	 * @param string $lock_description Human-friendly explanation of lock's purpose.
58
59
	 */
	private function get_reset_lock( $args, $assoc_args, $lock_name, $lock_limit, $lock_description ) {
Erick Hitter's avatar
Erick Hitter committed
60
		// Output information about the lock.
61
		\WP_CLI::log( $lock_description . "\n" );
62

Erick Hitter's avatar
Erick Hitter committed
63
		/* translators: 1: Lock limit */
64
		\WP_CLI::log( sprintf( __( 'Maximum: %s', 'automattic-cron-control' ), number_format_i18n( $lock_limit ) ) . "\n" );
65

Erick Hitter's avatar
Erick Hitter committed
66
		// Reset requested.
67
		if ( isset( $assoc_args['reset'] ) ) {
68
			\WP_CLI::warning( __( 'Resetting lock...', 'automattic-cron-control' ) . "\n" );
69

70
71
			$lock      = \Automattic\WP\Cron_Control\Lock::get_lock_value( $lock_name );
			$timestamp = \Automattic\WP\Cron_Control\Lock::get_lock_timestamp( $lock_name );
72

Erick Hitter's avatar
Erick Hitter committed
73
			/* translators: 1: Previous lock value */
74
			\WP_CLI::log( sprintf( __( 'Previous value: %s', 'automattic-cron-control' ), number_format_i18n( $lock ) ) );
Erick Hitter's avatar
Erick Hitter committed
75
			/* translators: 1: Previous lock timestamp */
76
			\WP_CLI::log( sprintf( __( 'Previously modified: %s UTC', 'automattic-cron-control' ), date_i18n( TIME_FORMAT, $timestamp ) ) . "\n" );
77
78

			\WP_CLI::confirm( sprintf( __( 'Are you sure you want to reset this lock?', 'automattic-cron-control' ) ) );
79
			\WP_CLI::log( '' );
80

81
			\Automattic\WP\Cron_Control\Lock::reset_lock( $lock_name );
82
			\WP_CLI::success( __( 'Lock reset', 'automattic-cron-control' ) . "\n" );
83
			\WP_CLI::log( __( 'New lock values:', 'automattic-cron-control' ) );
84
85
		}

Erick Hitter's avatar
Erick Hitter committed
86
		// Output lock state.
87
88
		$lock      = \Automattic\WP\Cron_Control\Lock::get_lock_value( $lock_name );
		$timestamp = \Automattic\WP\Cron_Control\Lock::get_lock_timestamp( $lock_name );
89

Erick Hitter's avatar
Erick Hitter committed
90
		/* translators: 1: Current lock value */
91
		\WP_CLI::log( sprintf( __( 'Current value: %s', 'automattic-cron-control' ), number_format_i18n( $lock ) ) );
Erick Hitter's avatar
Erick Hitter committed
92
		/* translators: 1: Current lock timestamp */
93
		\WP_CLI::log( sprintf( __( 'Last modified: %s UTC', 'automattic-cron-control' ), date_i18n( TIME_FORMAT, $timestamp ) ) );
94
95
96
97
	}
}

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