class-lock.php 2.27 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
13
	 * @subcommand run-lock
	 * @synopsis [--reset]
14
	 */
15
	public function run_lock( $args, $assoc_args ) {
16
17
18
19
20
21
22
23
24
25
26
		$lock_name        = \Automattic\WP\Cron_Control\Events::LOCK;
		$lock_limit       = \Automattic\WP\Cron_Control\JOB_CONCURRENCY_LIMIT;
		$lock_description = __( 'This lock limits the number of events that can run concurrently.', 'automattic-cron-control' );

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

	/**
	 * Retrieve a lock's current value, or reset it
	 */
	private function get_reset_lock( $args, $assoc_args, $lock_name, $lock_limit, $lock_description ) {
27
		// Output information about the lock
28
		\WP_CLI::line( $lock_description . "\n" );
29

30
		\WP_CLI::line( sprintf( __( 'Maximum: %s', 'automattic-cron-control' ), number_format_i18n( $lock_limit ) ) . "\n" );
31

32
33
34
35
		// Reset requested
		if ( isset( $assoc_args['reset'] ) ) {
			\WP_CLI::warning( __( 'Resetting lock', 'automattic-cron-control' ) . "\n" );

36
37
			$lock      = \Automattic\WP\Cron_Control\Lock::get_lock_value( $lock_name );
			$timestamp = \Automattic\WP\Cron_Control\Lock::get_lock_timestamp( $lock_name );
38
39
40
41

			\WP_CLI::line( sprintf( __( 'Previous value: %s', 'automattic-cron-control' ), number_format_i18n( $lock ) ) );
			\WP_CLI::line( sprintf( __( 'Previous lock expiration: %s GMT', 'automattic-cron-control' ), date( TIME_FORMAT, $timestamp ) ) . "\n" );

42
			\Automattic\WP\Cron_Control\Lock::reset_lock( $lock_name );
43
44
45
46
47
			\WP_CLI::success( __( 'Lock reset', 'automattic-cron-control' ) . "\n" );
			\WP_CLI::line( __( 'New lock values:', 'automattic-cron-control' ) );
		}

		// Output lock state
48
49
		$lock      = \Automattic\WP\Cron_Control\Lock::get_lock_value( $lock_name );
		$timestamp = \Automattic\WP\Cron_Control\Lock::get_lock_timestamp( $lock_name );
50
51
52
53
54
55
56

		\WP_CLI::line( sprintf( __( 'Current value: %s', 'automattic-cron-control' ), number_format_i18n( $lock ) ) );
		\WP_CLI::line( sprintf( __( 'Lock expiration: %s GMT', 'automattic-cron-control' ), date( TIME_FORMAT, $timestamp ) ) );
	}
}

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