Verified Commit e57396de authored by Erick Hitter's avatar Erick Hitter
Browse files

When checking a lock, allow a deadlock timeout that's specific to the lock

parent 82f6448d
......@@ -206,7 +206,7 @@ class Events extends Singleton {
}
// Limit to one concurrent execution of a specific action
if ( ! Lock::check_lock( $this->get_lock_key_for_event_action( $event ), 1 ) ) {
if ( ! Lock::check_lock( $this->get_lock_key_for_event_action( $event ), 1, JOB_LOCK_EXPIRY_IN_MINUTES ) ) {
return false;
}
......
......@@ -6,9 +6,16 @@ class Lock {
/**
* Set a lock and limit how many concurrent jobs are permitted
*/
public static function check_lock( $lock, $limit = null ) {
// Prevent deadlock
if ( self::get_lock_timestamp( $lock ) < time() - JOB_TIMEOUT_IN_MINUTES * MINUTE_IN_SECONDS ) {
public static function check_lock( $lock, $limit = null, $timeout_in_minutes = null ) {
// Timeout, should a process die before its lock is freed
if ( is_numeric( $timeout_in_minutes ) ) {
$timeout = $timeout_in_minutes * \MINUTE_IN_SECONDS;
} else {
$timeout = LOCK_DEFULT_TIMEOUT_IN_MINUTES * \MINUTE_IN_SECONDS;
}
// Check for, and recover from, deadlock
if ( self::get_lock_timestamp( $lock ) < time() - $timeout ) {
self::reset_lock( $lock );
return true;
}
......
......@@ -19,4 +19,5 @@ const JOB_CREATION_CONCURRENCY_LIMIT = 5;
/**
* Locks
*/
const LOCK_DEFAULT_LIMIT = 10;
const LOCK_DEFAULT_LIMIT = 10;
const LOCK_DEFULT_TIMEOUT_IN_MINUTES = 10;
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