PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.3.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.3.0
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / ArchiveProcessor / ArchivingStatus.php
matomo / app / core / ArchiveProcessor Last commit date
ArchivingStatus.php 5 years ago Loader.php 5 years ago Parameters.php 5 years ago PluginsArchiver.php 5 years ago PluginsArchiverException.php 5 years ago Rules.php 5 years ago
ArchivingStatus.php
126 lines
1 <?php
2 /**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9
10 namespace Piwik\ArchiveProcessor;
11
12 use Piwik\Common;
13 use Piwik\Concurrency\Lock;
14 use Piwik\Concurrency\LockBackend;
15 use Piwik\Container\StaticContainer;
16 use Piwik\SettingsPiwik;
17
18 class ArchivingStatus
19 {
20 const LOCK_KEY_PREFIX = 'Archiving';
21 const DEFAULT_ARCHIVING_TTL = 7200; // 2 hours
22
23 /**
24 * @var LockBackend
25 */
26 private $lockBackend;
27
28 /**
29 * @var int
30 */
31 private $archivingTTLSecs;
32
33 /**
34 * @var Lock[]
35 */
36 private $lockStack = [];
37
38 public function __construct(LockBackend $lockBackend, $archivingTTLSecs = self::DEFAULT_ARCHIVING_TTL)
39 {
40 $this->lockBackend = $lockBackend;
41 $this->archivingTTLSecs = $archivingTTLSecs;
42 }
43
44 public function archiveStarted(Parameters $params)
45 {
46 $lock = $this->makeArchivingLock($params);
47 $locked = $lock->acquireLock('', $this->archivingTTLSecs);
48 if ($locked) {
49 array_push($this->lockStack, $lock);
50 }
51 return $locked;
52 }
53
54 /**
55 * Try to acquire the lock that is acquired before starting archiving. If it is acquired, it
56 * means archiving is not ongoing. If it is not acquired, then archiving is ongoing.
57 *
58 * @param Parameters $params
59 * @param $doneFlag
60 * @return Lock
61 */
62 public function acquireArchiveInProgressLock($idSite, $date1, $date2, $period, $doneFlag)
63 {
64 $lock = $this->makeArchivingLockFromDoneFlag($idSite, $date1, $date2, $period, $doneFlag);
65 $lock->acquireLock('', $ttl = 1);
66 return $lock;
67 }
68
69 public function archiveFinished()
70 {
71 $lock = array_pop($this->lockStack);
72 $lock->unlock();
73 }
74
75 public function getCurrentArchivingLock()
76 {
77 if (empty($this->lockStack)) {
78 return null;
79 }
80 return end($this->lockStack);
81 }
82
83 public function getSitesCurrentlyArchiving()
84 {
85 $lockMeta = new Lock($this->lockBackend, self::LOCK_KEY_PREFIX . '.');
86 $acquiredLocks = $lockMeta->getAllAcquiredLockKeys();
87
88 $sitesCurrentlyArchiving = [];
89 foreach ($acquiredLocks as $lockKey) {
90 $parts = explode('.', $lockKey);
91 if (!isset($parts[1])) {
92 continue;
93 }
94 $sitesCurrentlyArchiving[] = (int) $parts[1];
95 }
96 $sitesCurrentlyArchiving = array_unique($sitesCurrentlyArchiving);
97 $sitesCurrentlyArchiving = array_values($sitesCurrentlyArchiving);
98
99 return $sitesCurrentlyArchiving;
100 }
101
102 /**
103 * @return Lock
104 */
105 private function makeArchivingLock(Parameters $params)
106 {
107 $doneFlag = Rules::getDoneStringFlagFor([$params->getSite()->getId()], $params->getSegment(),
108 $params->getPeriod()->getLabel(), $params->getRequestedPlugin());
109 return $this->makeArchivingLockFromDoneFlag($params->getSite()->getId(), $params->getSite()->getId(), $params->getPeriod()->getDateStart()->toString(),
110 $params->getPeriod()->getDateEnd()->toString(), $doneFlag);
111 }
112
113 private function makeArchivingLockFromDoneFlag($idSite, $date1, $date2, $period, $doneFlag)
114 {
115 $lockKeyParts = [
116 self::LOCK_KEY_PREFIX,
117 $idSite,
118
119 // md5 to keep it within the 70 char limit in the table
120 md5($period . $date1 . ',' . $date2 . $doneFlag),
121 ];
122
123 $lockKeyPrefix = implode('.', $lockKeyParts);
124 return new Lock(StaticContainer::get(LockBackend::class), $lockKeyPrefix, $this->archivingTTLSecs);
125 }
126 }