Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for lock expiry. #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions classes/lock/redis_lock_factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}

/**
Expand Down