-
Notifications
You must be signed in to change notification settings - Fork 34
/
Processor.php
185 lines (151 loc) · 5.6 KB
/
Processor.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?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\Tracker;
use Piwik\Tracker\RequestSet;
use Piwik\Plugins\QueuedTracking\Queue;
use Piwik\Plugins\QueuedTracking\Queue\Processor\Handler;
use Exception;
/**
* Processes all queued tracking requests. You need to acquire a lock before calling process() and unlock it afterwards!
*
* eg
* $processor = new Processor($backend);
* if ($processor->acquireLock()) {
* try {
* $processor->process($queue);
* } catch (Exception $e) {}
* $processor->unlock();
* }
*
* It will process until there are not enough tracking requests in the queue anymore.
*/
class Processor
{
/**
* @var Handler
*/
private $handler;
/**
* @var Queue\Manager
*/
private $queueManager;
/**
* The number of batches to process before self-terminating.
* If this value is 250, and one has configured to insert 25 requests in one batch, 250 * 25 requests will be
* inserted. This way we prevent eg possible memory problems for when running too long.
* @var int
*/
private $numMaxBatchesToProcess = 250;
public function __construct(Queue\Manager $queueManager)
{
$this->queueManager = $queueManager;
$this->handler = new Handler();
}
public function setNumberOfMaxBatchesToProcess($numBatches)
{
$this->numMaxBatchesToProcess = (int) $numBatches;
}
public function process(Tracker $tracker = null)
{
$tracker = $tracker ?: new Tracker();
if (!$tracker->shouldRecordStatistics()) {
return $tracker;
}
$request = new RequestSet();
$request->rememberEnvironment();
$loops = 0;
try {
while ($queue = $this->queueManager->lockNext()) {
if ($loops > $this->numMaxBatchesToProcess) {
Common::printDebug('This worker processed ' . $loops . ' times, stopping now.');
break;
} else {
$loops++;
}
$queuedRequestSets = $queue->getRequestSetsToProcess();
if (!empty($queuedRequestSets)) {
$requestSetsToRetry = $this->processRequestSets($tracker, $queuedRequestSets);
$this->processRequestSets($tracker, $requestSetsToRetry);
$queue->markRequestSetsAsProcessed();
// TODO if markR..() fails, we would process them again later
}
$this->queueManager->unlock();
}
} catch (Exception $e) {
Common::printDebug('Failed to process a request set: ' . $e->getMessage());
$this->queueManager->unlock();
$request->restoreEnvironment();
throw $e;
}
$request->restoreEnvironment();
return $tracker;
}
/**
* @param Tracker $tracker
* @param RequestSet[] $queuedRequestSets
* @return RequestSet[]
* @throws Exception
*/
protected function processRequestSets(Tracker $tracker, $queuedRequestSets)
{
if (empty($queuedRequestSets)) {
return array();
}
$this->handler->init($tracker);
foreach ($queuedRequestSets as $index => $requestSet) {
if (!$this->extendLockExpireToMakeSureWeCanProcessARequestSet($requestSet)) {
$this->forceRollbackAndThrowExceptionAsAnotherProcessMightProcessSameRequestSets($tracker);
}
try {
$this->handler->process($tracker, $requestSet);
} catch (\Exception $e) {
Common::printDebug('Failed to process a queued request set' . $e->getMessage());
$this->handler->onException($requestSet, $e);
}
}
if (!$this->extendLockExpireToMakeSureWeCanFinishQueuedRequests($queuedRequestSets)) {
$this->forceRollbackAndThrowExceptionAsAnotherProcessMightProcessSameRequestSets($tracker);
}
if ($this->handler->hasErrors()) {
$this->handler->rollBack($tracker);
} else {
$this->handler->commit();
}
return $this->handler->getRequestSetsToRetry();
}
/**
* @param $queuedRequests
* @return bool true if we still have the lock and if expire was set successfully
*/
private function extendLockExpireToMakeSureWeCanFinishQueuedRequests($queuedRequests)
{
$ttl = count($queuedRequests) * 2;
// in case there are 50 queued requests it gives us 100 seconds to commit/rollback and to start new batch
$ttl = max($ttl, 20); // lock at least for 20 seconds
return $this->queueManager->expireLock($ttl);
}
/**
* @param RequestSet $requestSet
* @return bool true if we still have the lock and if expire was set successfully
*/
private function extendLockExpireToMakeSureWeCanProcessARequestSet(RequestSet $requestSet)
{
// 2 seconds per tracking request should give it enough time to process it
$ttl = $requestSet->getNumberOfRequests() * 2;
$ttl = max($ttl, 20); // lock for at least 20 seconds
return $this->queueManager->expireLock($ttl);
}
private function forceRollbackAndThrowExceptionAsAnotherProcessMightProcessSameRequestSets(Tracker $tracker)
{
$this->handler->rollBack($tracker);
throw new LockExpiredException('Rolled back as we no longer have lock or the lock was never acquired');
}
}