class-lock.php 3.15 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

namespace Automattic\WP\Cron_Control\CLI;

/**
 * Manage Cron Control's internal locks
 */
class Lock extends \WP_CLI_Command {
	/**
10
	 * Manage the lock that limits concurrent job executions
11
	 *
12
	 * @subcommand manage-run-lock
13
	 * @synopsis [--reset]
14
	 */
15
	public function manage_run_lock( $args, $assoc_args ) {
16
17
		$lock_name        = \Automattic\WP\Cron_Control\Events::LOCK;
		$lock_limit       = \Automattic\WP\Cron_Control\JOB_CONCURRENCY_LIMIT;
18
19
20
21
22
		$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 );
	}

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
	/**
	 * Manage the lock that limits concurrent execution of jobs with the same action
	 *
	 * @subcommand manage-event-lock
	 * @synopsis <action> [--reset]
	 */
	public function manage_event_lock( $args, $assoc_args ) {
		if ( empty( $args[0] ) ) {
			\WP_CLI::error( sprintf( __( 'Specify an action', 'automattic-cron-control' ) ) );
		}

		$lock_name        = \Automattic\WP\Cron_Control\Events::instance()->get_lock_key_for_event_action( array( 'action' => $args[0], ) );
		$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 );
	}

41
42
43
44
	/**
	 * Retrieve a lock's current value, or reset it
	 */
	private function get_reset_lock( $args, $assoc_args, $lock_name, $lock_limit, $lock_description ) {
45
		// Output information about the lock
46
		\WP_CLI::log( $lock_description . "\n" );
47

48
		\WP_CLI::log( sprintf( __( 'Maximum: %s', 'automattic-cron-control' ), number_format_i18n( $lock_limit ) ) . "\n" );
49

50
51
		// Reset requested
		if ( isset( $assoc_args['reset'] ) ) {
52
			\WP_CLI::warning( __( 'Resetting lock...', 'automattic-cron-control' ) . "\n" );
53

54
55
			$lock      = \Automattic\WP\Cron_Control\Lock::get_lock_value( $lock_name );
			$timestamp = \Automattic\WP\Cron_Control\Lock::get_lock_timestamp( $lock_name );
56

57
58
			\WP_CLI::log( sprintf( __( 'Previous value: %s', 'automattic-cron-control' ), number_format_i18n( $lock ) ) );
			\WP_CLI::log( sprintf( __( 'Previously modified: %s GMT', 'automattic-cron-control' ), date( TIME_FORMAT, $timestamp ) ) . "\n" );
59
60

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

63
			\Automattic\WP\Cron_Control\Lock::reset_lock( $lock_name );
64
			\WP_CLI::success( __( 'Lock reset', 'automattic-cron-control' ) . "\n" );
65
			\WP_CLI::log( __( 'New lock values:', 'automattic-cron-control' ) );
66
67
68
		}

		// Output lock state
69
70
		$lock      = \Automattic\WP\Cron_Control\Lock::get_lock_value( $lock_name );
		$timestamp = \Automattic\WP\Cron_Control\Lock::get_lock_timestamp( $lock_name );
71

72
73
		\WP_CLI::log( sprintf( __( 'Current value: %s', 'automattic-cron-control' ), number_format_i18n( $lock ) ) );
		\WP_CLI::log( sprintf( __( 'Last modified: %s GMT', 'automattic-cron-control' ), date( TIME_FORMAT, $timestamp ) ) );
74
75
76
77
	}
}

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