-
Notifications
You must be signed in to change notification settings - Fork 34
/
Lock.php
100 lines (81 loc) · 2.72 KB
/
Lock.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\QueuedTracking\Queue;
use Piwik\Common;
use Piwik\Plugins\QueuedTracking\Queue\Backend;
use Piwik\Tracker;
class Lock
{
/**
* @var Backend
*/
private $backend;
private $lockKeyStart = 'QueuedTrackingLock';
private $lockKey = null;
private $lockValue = null;
public function __construct(Backend $backend)
{
$this->backend = $backend;
$this->lockKey = $this->lockKeyStart;
}
public function getNumberOfAcquiredLocks()
{
return count($this->getAllAcquiredLockKeys());
}
public function getAllAcquiredLockKeys()
{
return $this->backend->getKeysMatchingPattern($this->lockKeyStart . '*');
}
public function acquireLock($id)
{
$this->lockKey = $this->lockKeyStart . $id;
$lockValue = substr(Common::generateUniqId(), 0, 12);
$locked = $this->backend->setIfNotExists($this->lockKey, $lockValue, $ttlInSeconds = 60);
if ($locked) {
$this->lockValue = $lockValue;
}
return $locked;
}
public function isLocked()
{
if (!$this->lockValue) {
return false;
}
return $this->lockValue === $this->backend->get($this->lockKey);
}
public function unlock()
{
if ($this->lockValue) {
$this->backend->deleteIfKeyHasValue($this->lockKey, $this->lockValue);
$this->lockValue = null;
}
}
public function expireLock($ttlInSeconds)
{
if ($ttlInSeconds > 0 && $this->lockValue) {
$success = $this->backend->expireIfKeyHasValue($this->lockKey, $this->lockValue, $ttlInSeconds);
if (!$success) {
$value = $this->backend->get($this->lockKey);
$message = sprintf('Failed to expire key %s (%s / %s).', $this->lockKey, $this->lockValue, (string) $value);
if ($value === false) {
Common::printDebug($message . ' It seems like the key already expired as it no longer exists.');
} elseif (!empty($value) && $value == $this->lockValue) {
Common::printDebug($message . ' We still have the lock but for some reason it did not expire.');
} elseif (!empty($value)) {
Common::printDebug($message . ' It seems to be locked by another queue.');
} else {
Common::printDebug($message . ' Failed to expire key.');
}
return false;
}
return true;
}
return false;
}
}