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
LoaderLock.php
66 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 | namespace Piwik\ArchiveProcessor; |
| 10 | |
| 11 | use Piwik\Db; |
| 12 | use Piwik\SettingsPiwik; |
| 13 | |
| 14 | class LoaderLock |
| 15 | { |
| 16 | |
| 17 | const MAX_LEN_LOCK_NAME = 64; |
| 18 | const MAX_LOCK_TIME = 60; //in seconds |
| 19 | protected $id; |
| 20 | |
| 21 | /** |
| 22 | * @param string $id |
| 23 | * @throws \Exception |
| 24 | */ |
| 25 | public function __construct($id) |
| 26 | { |
| 27 | // instanceId is needed for multi tenant database solution |
| 28 | $id = SettingsPiwik::getPiwikInstanceId() . $id; |
| 29 | |
| 30 | if (mb_strlen($id) >= self::MAX_LEN_LOCK_NAME) { |
| 31 | //convert ot prefix and md5 full length |
| 32 | $id = mb_substr($id, 0, 32) . md5($id); |
| 33 | } |
| 34 | |
| 35 | $this->id = $id; |
| 36 | } |
| 37 | |
| 38 | public function setLock() |
| 39 | { |
| 40 | Db::fetchOne('SELECT GET_LOCK(?,?)', array($this->id, self::MAX_LOCK_TIME)); |
| 41 | } |
| 42 | |
| 43 | public function unLock() |
| 44 | { |
| 45 | Db::query('DO RELEASE_LOCK(?)', array($this->id)); |
| 46 | } |
| 47 | |
| 48 | public function getId() |
| 49 | { |
| 50 | return $this->id; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @description check if the lock is available to user |
| 55 | * @param string $key |
| 56 | * @return bool |
| 57 | * @throws \Exception |
| 58 | */ |
| 59 | public static function isLockAvailable($key) |
| 60 | { |
| 61 | return (bool)Db::fetchOne('SELECT IS_FREE_LOCK(?)', [$key]); |
| 62 | |
| 63 | } |
| 64 | |
| 65 | } |
| 66 |