PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.13.3
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.13.3
5.12.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 4 years ago Loader.php 3 years ago LoaderLock.php 4 years ago Parameters.php 4 years ago PluginsArchiver.php 3 years ago PluginsArchiverException.php 5 years ago Rules.php 4 years ago
ArchivingStatus.php
124 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\Concurrency\Lock;
13 use Piwik\Concurrency\LockBackend;
14 use Piwik\Container\StaticContainer;
15
16 class ArchivingStatus
17 {
18 const LOCK_KEY_PREFIX = 'Archiving';
19 const DEFAULT_ARCHIVING_TTL = 7200; // 2 hours
20
21 /**
22 * @var LockBackend
23 */
24 private $lockBackend;
25
26 /**
27 * @var int
28 */
29 private $archivingTTLSecs;
30
31 /**
32 * @var Lock[]
33 */
34 private $lockStack = [];
35
36 public function __construct(LockBackend $lockBackend, $archivingTTLSecs = self::DEFAULT_ARCHIVING_TTL)
37 {
38 $this->lockBackend = $lockBackend;
39 $this->archivingTTLSecs = $archivingTTLSecs;
40 }
41
42 public function archiveStarted(Parameters $params)
43 {
44 $lock = $this->makeArchivingLock($params);
45 $locked = $lock->acquireLock('', $this->archivingTTLSecs);
46 if ($locked) {
47 array_push($this->lockStack, $lock);
48 }
49 return $locked;
50 }
51
52 /**
53 * Try to acquire the lock that is acquired before starting archiving. If it is acquired, it
54 * means archiving is not ongoing. If it is not acquired, then archiving is ongoing.
55 *
56 * @param Parameters $params
57 * @param $doneFlag
58 * @return Lock
59 */
60 public function acquireArchiveInProgressLock($idSite, $date1, $date2, $period, $doneFlag)
61 {
62 $lock = $this->makeArchivingLockFromDoneFlag($idSite, $date1, $date2, $period, $doneFlag);
63 $lock->acquireLock('', $ttl = 1);
64 return $lock;
65 }
66
67 public function archiveFinished()
68 {
69 $lock = array_pop($this->lockStack);
70 $lock->unlock();
71 }
72
73 public function getCurrentArchivingLock()
74 {
75 if (empty($this->lockStack)) {
76 return null;
77 }
78 return end($this->lockStack);
79 }
80
81 public function getSitesCurrentlyArchiving()
82 {
83 $lockMeta = new Lock($this->lockBackend, self::LOCK_KEY_PREFIX . '.');
84 $acquiredLocks = $lockMeta->getAllAcquiredLockKeys();
85
86 $sitesCurrentlyArchiving = [];
87 foreach ($acquiredLocks as $lockKey) {
88 $parts = explode('.', $lockKey);
89 if (!isset($parts[1])) {
90 continue;
91 }
92 $sitesCurrentlyArchiving[] = (int) $parts[1];
93 }
94 $sitesCurrentlyArchiving = array_unique($sitesCurrentlyArchiving);
95 $sitesCurrentlyArchiving = array_values($sitesCurrentlyArchiving);
96
97 return $sitesCurrentlyArchiving;
98 }
99
100 /**
101 * @return Lock
102 */
103 private function makeArchivingLock(Parameters $params)
104 {
105 $doneFlag = Rules::getDoneStringFlagFor([$params->getSite()->getId()], $params->getSegment(),
106 $params->getPeriod()->getLabel(), $params->getRequestedPlugin());
107 return $this->makeArchivingLockFromDoneFlag($params->getSite()->getId(), $params->getSite()->getId(), $params->getPeriod()->getDateStart()->toString(),
108 $params->getPeriod()->getDateEnd()->toString(), $doneFlag);
109 }
110
111 private function makeArchivingLockFromDoneFlag($idSite, $date1, $date2, $period, $doneFlag)
112 {
113 $lockKeyParts = [
114 self::LOCK_KEY_PREFIX,
115 $idSite,
116
117 // md5 to keep it within the 70 char limit in the table
118 md5($period . $date1 . ',' . $date2 . $doneFlag),
119 ];
120
121 $lockKeyPrefix = implode('.', $lockKeyParts);
122 return new Lock(StaticContainer::get(LockBackend::class), $lockKeyPrefix, $this->archivingTTLSecs);
123 }
124 }