forked from matomo-org/plugin-QueuedTracking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.php
110 lines (88 loc) · 2.42 KB
/
Queue.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
101
102
103
104
105
106
107
108
109
110
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\QueuedTracking;
use Piwik\Plugins\QueuedTracking\Queue\Backend;
use Piwik\Tracker\RequestSet;
use Piwik\Plugins\QueuedTracking\Queue\Backend\Redis;
class Queue
{
const PREFIX = 'trackingQueueV1';
/**
* @var Redis
*/
private $backend;
private $key;
private $id;
private $numRequestsToProcessInBulk = 50;
public function __construct(Backend $backend, $id)
{
$this->backend = $backend;
$this->id = $id;
$this->key = self::PREFIX;
if (!empty($id)) {
$this->key .= '_' . $id;
}
}
public function getId()
{
return $this->id;
}
public function getKey()
{
return $this->key;
}
public function setNumberOfRequestsToProcessAtSameTime($numRequests)
{
$this->numRequestsToProcessInBulk = $numRequests;
}
public function getNumberOfRequestsToProcessAtSameTime()
{
return $this->numRequestsToProcessInBulk;
}
public function getNumberOfRequestSetsInQueue()
{
return $this->backend->getNumValuesInList($this->key);
}
public function addRequestSet(RequestSet $requests)
{
if (!$requests->hasRequests()) {
return;
}
$value = $requests->getState();
$value = json_encode($value);
$this->backend->appendValuesToList($this->key, array($value));
}
public function delete()
{
return $this->backend->delete($this->key);
}
/**
* @return RequestSet[]
*/
public function getRequestSetsToProcess()
{
$values = $this->backend->getFirstXValuesFromList($this->key, $this->numRequestsToProcessInBulk);
$requests = array();
foreach ($values as $value) {
$params = json_decode($value, true);
$request = new RequestSet();
$request->restoreState($params);
$requests[] = $request;
}
return $requests;
}
public function shouldProcess()
{
return $this->backend->hasAtLeastXRequestsQueued($this->key, $this->numRequestsToProcessInBulk);
}
public function markRequestSetsAsProcessed()
{
$this->backend->removeFirstXValuesFromList($this->key, $this->numRequestsToProcessInBulk);
}
}