ArchivingStatus.php
6 years ago
Loader.php
6 years ago
Parameters.php
6 years ago
PluginsArchiver.php
6 years ago
PluginsArchiverException.php
6 years ago
Rules.php
6 years ago
ArchivingStatus.php
111 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - 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 | private $pid; |
| 39 | |
| 40 | public function __construct(LockBackend $lockBackend, $archivingTTLSecs = self::DEFAULT_ARCHIVING_TTL) |
| 41 | { |
| 42 | $this->lockBackend = $lockBackend; |
| 43 | $this->archivingTTLSecs = $archivingTTLSecs; |
| 44 | $this->pid = Common::getProcessId(); |
| 45 | } |
| 46 | |
| 47 | public function archiveStarted(Parameters $params) |
| 48 | { |
| 49 | $lock = $this->makeArchivingLock($params); |
| 50 | $lock->acquireLock($this->getInstanceProcessId(), $this->archivingTTLSecs); |
| 51 | array_push($this->lockStack, $lock); |
| 52 | } |
| 53 | |
| 54 | public function archiveFinished() |
| 55 | { |
| 56 | $lock = array_pop($this->lockStack); |
| 57 | $lock->unlock(); |
| 58 | } |
| 59 | |
| 60 | public function getCurrentArchivingLock() |
| 61 | { |
| 62 | if (empty($this->lockStack)) { |
| 63 | return null; |
| 64 | } |
| 65 | return end($this->lockStack); |
| 66 | } |
| 67 | |
| 68 | public function getSitesCurrentlyArchiving() |
| 69 | { |
| 70 | $lockMeta = new Lock($this->lockBackend, self::LOCK_KEY_PREFIX . '.'); |
| 71 | $acquiredLocks = $lockMeta->getAllAcquiredLockKeys(); |
| 72 | |
| 73 | $sitesCurrentlyArchiving = []; |
| 74 | foreach ($acquiredLocks as $lockKey) { |
| 75 | $parts = explode('.', $lockKey); |
| 76 | if (!isset($parts[1])) { |
| 77 | continue; |
| 78 | } |
| 79 | $sitesCurrentlyArchiving[] = (int) $parts[1]; |
| 80 | } |
| 81 | $sitesCurrentlyArchiving = array_unique($sitesCurrentlyArchiving); |
| 82 | $sitesCurrentlyArchiving = array_values($sitesCurrentlyArchiving); |
| 83 | |
| 84 | return $sitesCurrentlyArchiving; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @return Lock |
| 89 | */ |
| 90 | private function makeArchivingLock(Parameters $params) |
| 91 | { |
| 92 | $doneFlag = Rules::getDoneStringFlagFor([$params->getSite()->getId()], $params->getSegment(), |
| 93 | $params->getPeriod()->getLabel(), $params->getRequestedPlugin()); |
| 94 | |
| 95 | $lockKeyParts = [ |
| 96 | self::LOCK_KEY_PREFIX, |
| 97 | $params->getSite()->getId(), |
| 98 | |
| 99 | // md5 to keep it within the 70 char limit in the table |
| 100 | md5($params->getPeriod()->getId() . $params->getPeriod()->getRangeString() . $doneFlag), |
| 101 | ]; |
| 102 | |
| 103 | $lockKeyPrefix = implode('.', $lockKeyParts); |
| 104 | return new Lock(StaticContainer::get(LockBackend::class), $lockKeyPrefix, $this->archivingTTLSecs); |
| 105 | } |
| 106 | |
| 107 | private function getInstanceProcessId() |
| 108 | { |
| 109 | return SettingsPiwik::getPiwikInstanceId() . '.' . $this->pid; |
| 110 | } |
| 111 | } |