class-lock.php 2.95 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
23
24
25
26
27
28
29
30
31
32
		$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 );
	}

	/**
	 * Manage the lock that limits concurrent job creation
	 *
	 * @subcommand manage-create-lock
	 * @synopsis [--reset]
	 */
	public function manage_create_lock( $args, $assoc_args ) {
		$lock_name        = \Automattic\WP\Cron_Control\Cron_Options_CPT::LOCK;
		$lock_limit       = \Automattic\WP\Cron_Control\JOB_CREATION_CONCURRENCY_LIMIT;
		$lock_description = __( 'This lock limits the number of events created concurrently.', 'automattic-cron-control' );
33
34
35
36
37
38
39
40

		$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 ) {
41
		// Output information about the lock
42
		\WP_CLI::line( $lock_description . "\n" );
43

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

46
47
		// Reset requested
		if ( isset( $assoc_args['reset'] ) ) {
48
			\WP_CLI::warning( __( 'Resetting lock...', 'automattic-cron-control' ) . "\n" );
49

50
51
			$lock      = \Automattic\WP\Cron_Control\Lock::get_lock_value( $lock_name );
			$timestamp = \Automattic\WP\Cron_Control\Lock::get_lock_timestamp( $lock_name );
52
53

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

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

59
			\Automattic\WP\Cron_Control\Lock::reset_lock( $lock_name );
60
61
62
63
64
			\WP_CLI::success( __( 'Lock reset', 'automattic-cron-control' ) . "\n" );
			\WP_CLI::line( __( 'New lock values:', 'automattic-cron-control' ) );
		}

		// Output lock state
65
66
		$lock      = \Automattic\WP\Cron_Control\Lock::get_lock_value( $lock_name );
		$timestamp = \Automattic\WP\Cron_Control\Lock::get_lock_timestamp( $lock_name );
67
68

		\WP_CLI::line( sprintf( __( 'Current value: %s', 'automattic-cron-control' ), number_format_i18n( $lock ) ) );
69
		\WP_CLI::line( sprintf( __( 'Last modified: %s GMT', 'automattic-cron-control' ), date( TIME_FORMAT, $timestamp ) ) );
70
71
72
73
	}
}

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