class-lock.php 2.03 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
16
	public function run_lock( $args, $assoc_args ) {
		// Output information about the lock
17
18
19
20
		\WP_CLI::line( __( 'This lock limits the number of concurrent events that are run.', 'automattic-cron-control' ) . "\n" );

		\WP_CLI::line( sprintf( __( 'Maximum: %s', 'automattic-cron-control' ), number_format_i18n( \Automattic\WP\Cron_Control\JOB_CONCURRENCY_LIMIT ) ) . "\n" );

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
		// Reset requested
		if ( isset( $assoc_args['reset'] ) ) {
			\WP_CLI::warning( __( 'Resetting lock', 'automattic-cron-control' ) . "\n" );

			$lock      = \Automattic\WP\Cron_Control\Lock::get_lock_value( \Automattic\WP\Cron_Control\Events::LOCK );
			$timestamp = \Automattic\WP\Cron_Control\Lock::get_lock_timestamp( \Automattic\WP\Cron_Control\Events::LOCK );

			\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" );

			\Automattic\WP\Cron_Control\Lock::reset_lock( \Automattic\WP\Cron_Control\Events::LOCK );
			\WP_CLI::success( __( 'Lock reset', 'automattic-cron-control' ) . "\n" );
			\WP_CLI::line( __( 'New lock values:', 'automattic-cron-control' ) );
		}

		// Output lock state
37
38
39
40
41
42
43
44
45
		$lock      = \Automattic\WP\Cron_Control\Lock::get_lock_value( \Automattic\WP\Cron_Control\Events::LOCK );
		$timestamp = \Automattic\WP\Cron_Control\Lock::get_lock_timestamp( \Automattic\WP\Cron_Control\Events::LOCK );

		\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' );