ArchivingStatus.php
5 months ago
Loader.php
4 months ago
LoaderLock.php
1 year ago
Parameters.php
6 months ago
PluginsArchiver.php
1 year ago
PluginsArchiverException.php
2 years ago
Record.php
3 months ago
RecordBuilder.php
3 months ago
Rules.php
3 months ago
LoaderLock.php
56 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\ArchiveProcessor; |
| 10 | |
| 11 | use Piwik\Db; |
| 12 | use Piwik\SettingsPiwik; |
| 13 | class LoaderLock |
| 14 | { |
| 15 | public const MAX_LEN_LOCK_NAME = 64; |
| 16 | public const MAX_LOCK_TIME = 60; |
| 17 | //in seconds |
| 18 | protected $id; |
| 19 | /** |
| 20 | * @param string $id |
| 21 | * @throws \Exception |
| 22 | */ |
| 23 | public function __construct($id) |
| 24 | { |
| 25 | // instanceId is needed for multi tenant database solution |
| 26 | $id = SettingsPiwik::getPiwikInstanceId() . $id; |
| 27 | if (mb_strlen($id) >= self::MAX_LEN_LOCK_NAME) { |
| 28 | //convert ot prefix and md5 full length |
| 29 | $id = mb_substr($id, 0, 32) . md5($id); |
| 30 | } |
| 31 | $this->id = $id; |
| 32 | } |
| 33 | public function setLock() |
| 34 | { |
| 35 | Db::fetchOne('SELECT GET_LOCK(?,?)', array($this->id, self::MAX_LOCK_TIME)); |
| 36 | } |
| 37 | public function unLock() |
| 38 | { |
| 39 | Db::query('SELECT RELEASE_LOCK(?)', array($this->id)); |
| 40 | } |
| 41 | public function getId() |
| 42 | { |
| 43 | return $this->id; |
| 44 | } |
| 45 | /** |
| 46 | * @description check if the lock is available to user |
| 47 | * @param string $key |
| 48 | * @return bool |
| 49 | * @throws \Exception |
| 50 | */ |
| 51 | public static function isLockAvailable($key) |
| 52 | { |
| 53 | return (bool) Db::fetchOne('SELECT IS_FREE_LOCK(?)', [$key]); |
| 54 | } |
| 55 | } |
| 56 |