Commit 2119fb4f authored by Erick Hitter's avatar Erick Hitter
Browse files

PHPCS fixes

parent 38f65156
<?php <?php
/**
* Concurrency locks
*
* @package a8c_Cron_Control
*/
namespace Automattic\WP\Cron_Control; namespace Automattic\WP\Cron_Control;
/**
* Lock class
*/
class Lock { class Lock {
/** /**
* Set a lock and limit how many concurrent jobs are permitted * Set a lock and limit how many concurrent jobs are permitted
* *
* @param $lock string Lock name * @param string $lock Lock name.
* @param $limit int Concurrency limit * @param int $limit Concurrency limit.
* @param $timeout int Timeout in seconds * @param int $timeout Timeout in seconds.
*
* @return bool * @return bool
*/ */
public static function check_lock( $lock, $limit = null, $timeout = null ) { public static function check_lock( $lock, $limit = null, $timeout = null ) {
// Timeout, should a process die before its lock is freed // Timeout, should a process die before its lock is freed.
if ( ! is_numeric( $timeout ) ) { if ( ! is_numeric( $timeout ) ) {
$timeout = LOCK_DEFAULT_TIMEOUT_IN_MINUTES * \MINUTE_IN_SECONDS; $timeout = LOCK_DEFAULT_TIMEOUT_IN_MINUTES * \MINUTE_IN_SECONDS;
} }
// Check for, and recover from, deadlock // Check for, and recover from, deadlock.
if ( self::get_lock_timestamp( $lock ) < time() - $timeout ) { if ( self::get_lock_timestamp( $lock ) < time() - $timeout ) {
self::reset_lock( $lock ); self::reset_lock( $lock );
return true; return true;
} }
// Default limit for concurrent events // Default limit for concurrent events.
if ( ! is_numeric( $limit ) ) { if ( ! is_numeric( $limit ) ) {
$limit = LOCK_DEFAULT_LIMIT; $limit = LOCK_DEFAULT_LIMIT;
} }
// Check if process can run // Check if process can run.
if ( self::get_lock_value( $lock ) >= $limit ) { if ( self::get_lock_value( $lock ) >= $limit ) {
return false; return false;
} else { } else {
...@@ -40,6 +47,10 @@ class Lock { ...@@ -40,6 +47,10 @@ class Lock {
/** /**
* When event completes, allow another * When event completes, allow another
*
* @param string $lock Lock name.
* @param int $expires Lock expiration timestamp.
* @return bool
*/ */
public static function free_lock( $lock, $expires = 0 ) { public static function free_lock( $lock, $expires = 0 ) {
if ( self::get_lock_value( $lock ) > 1 ) { if ( self::get_lock_value( $lock ) > 1 ) {
...@@ -55,6 +66,10 @@ class Lock { ...@@ -55,6 +66,10 @@ class Lock {
/** /**
* Build cache key * Build cache key
*
* @param string $lock Lock name.
* @param string $type Key type, either lock or timestamp.
* @return string|bool
*/ */
private static function get_key( $lock, $type = 'lock' ) { private static function get_key( $lock, $type = 'lock' ) {
switch ( $type ) { switch ( $type ) {
...@@ -72,6 +87,10 @@ class Lock { ...@@ -72,6 +87,10 @@ class Lock {
/** /**
* Ensure lock entries are initially set * Ensure lock entries are initially set
*
* @param string $lock Lock name.
* @param int $expires Lock expiration timestamp.
* @return null
*/ */
public static function prime_lock( $lock, $expires = 0 ) { public static function prime_lock( $lock, $expires = 0 ) {
wp_cache_add( self::get_key( $lock ), 0, null, $expires ); wp_cache_add( self::get_key( $lock ), 0, null, $expires );
...@@ -82,6 +101,9 @@ class Lock { ...@@ -82,6 +101,9 @@ class Lock {
/** /**
* Retrieve a lock from cache * Retrieve a lock from cache
*
* @param string $lock Lock name.
* @return int
*/ */
public static function get_lock_value( $lock ) { public static function get_lock_value( $lock ) {
return (int) wp_cache_get( self::get_key( $lock ), null, true ); return (int) wp_cache_get( self::get_key( $lock ), null, true );
...@@ -89,6 +111,9 @@ class Lock { ...@@ -89,6 +111,9 @@ class Lock {
/** /**
* Retrieve a lock's timestamp * Retrieve a lock's timestamp
*
* @param string $lock Lock name.
* @return int
*/ */
public static function get_lock_timestamp( $lock ) { public static function get_lock_timestamp( $lock ) {
return (int) wp_cache_get( self::get_key( $lock, 'timestamp' ), null, true ); return (int) wp_cache_get( self::get_key( $lock, 'timestamp' ), null, true );
...@@ -96,6 +121,10 @@ class Lock { ...@@ -96,6 +121,10 @@ class Lock {
/** /**
* Clear a lock's current values, in order to free it * Clear a lock's current values, in order to free it
*
* @param string $lock Lock name.
* @param int $expires Lock expiration timestamp.
* @return bool
*/ */
public static function reset_lock( $lock, $expires = 0 ) { public static function reset_lock( $lock, $expires = 0 ) {
wp_cache_set( self::get_key( $lock ), 0, null, $expires ); wp_cache_set( self::get_key( $lock ), 0, null, $expires );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment