From 5b9ade987cf26b3d1e2152cb4af7ee1e7b1e99e7 Mon Sep 17 00:00:00 2001 From: Fabian Freyer Date: Fri, 22 Jan 2021 10:36:37 +0100 Subject: [PATCH] Add support for lock expiry. Discussed-with: Nils Adermann --- classes/lock/redis_lock_factory.php | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/classes/lock/redis_lock_factory.php b/classes/lock/redis_lock_factory.php index c4465a8..82509a0 100644 --- a/classes/lock/redis_lock_factory.php +++ b/classes/lock/redis_lock_factory.php @@ -187,7 +187,7 @@ public function get_lock($resource, $timeout, $maxlifetime = 86400) { do { $now = time(); try { - $locked = $this->redis->setnx($resource, $this->get_lock_value()); + $locked = $this->redis->set($resource, $this->get_lock_value(), ['nx', 'ex' => $maxlifetime]); $exception = false; } catch (\RedisException $e) { // If there has been a redis exception, we will try to reconnect. @@ -303,7 +303,26 @@ public function release_lock(lock $lock) { * @return boolean True if the lock was extended. */ public function extend_lock(lock $lock, $maxlifetime = 86400) { - return false; + $resource = $lock->get_key(); + + if ($this->shareconnection) { + // Re-get the Redis shared connection in case it's be cleared or recreated elsewhere. + $this->redis = $this->bootstrap_redis(); + } + + try { + $extended = $this->redis->expire($resource, $maxlifetime); + if ($extended) { + $this->log('Extended '.$resource.' lock'); + } else { + $this->log('Failed to extend '.$resource.' lock'); + } + return $extended; + } catch (\RedisException $e) { + $this->log("Got exception while trying to extend lock: {$e->getMessage()}"); + + return false; + } } /**