Ajax
1 year ago
BackgroundProcessing
1 year ago
Dto
1 year ago
Entity
1 year ago
Exceptions
1 year ago
Interfaces
2 years ago
Job
1 year ago
Request
2 years ago
Service
1 year ago
Storage
2 years ago
Task
1 year ago
AfterRestore.php
3 years ago
BackupDeleter.php
1 year ago
BackupDownload.php
2 years ago
BackupFileIndex.php
1 year ago
BackupHeader.php
2 years ago
BackupProcessLock.php
2 years ago
BackupRepairer.php
3 years ago
BackupRetentionHandler.php
2 years ago
BackupScheduler.php
1 year ago
BackupServiceProvider.php
2 years ago
BackupValidator.php
1 year ago
FileHeader.php
1 year ago
FileHeaderAttribute.php
2 years ago
WithBackupIdentifier.php
1 year ago
wpstgBackupHeader.txt
3 years ago
BackupProcessLock.php
88 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Framework\Traits\ResourceTrait; |
| 7 | use WPStaging\Backup\Exceptions\ProcessLockedException; |
| 8 | use WPStaging\Framework\Adapter\Directory; |
| 9 | |
| 10 | class BackupProcessLock |
| 11 | { |
| 12 | use ResourceTrait; |
| 13 | |
| 14 | const LOCK_FILE_NAME = '.wpstg_backup_process_locked'; |
| 15 | |
| 16 | private $lockFile; |
| 17 | |
| 18 | public function __construct() |
| 19 | { |
| 20 | $this->lockFile = trailingslashit(WPStaging::getContentDir()) . self::LOCK_FILE_NAME; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * @throws ProcessLockedException When the process is locked. |
| 25 | */ |
| 26 | public function lockProcess() |
| 27 | { |
| 28 | $this->checkProcessLocked(); |
| 29 | file_put_contents($this->lockFile, time()); |
| 30 | } |
| 31 | |
| 32 | public function unlockProcess() |
| 33 | { |
| 34 | if (!file_exists($this->lockFile)) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | unlink($this->lockFile); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @param null $timeout The timeout, in seconds, to lock the process. Leave null to automatically set one. |
| 43 | * |
| 44 | * @throws ProcessLockedException When the process is locked. |
| 45 | */ |
| 46 | public function checkProcessLocked($timeout = null) |
| 47 | { |
| 48 | if (is_null($timeout)) { |
| 49 | $timeout = min(120, $this->getTimeLimit()); |
| 50 | } |
| 51 | |
| 52 | if (!file_exists($this->lockFile)) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | $processLocked = file_get_contents($this->lockFile); |
| 57 | |
| 58 | if (!$processLocked) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | if (!is_numeric($processLocked)) { |
| 63 | $this->unlockProcess(); |
| 64 | |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Something is locking the process. |
| 70 | * |
| 71 | * Let's make sure the lock was placed in the last couple minutes, or else we unstuck it, |
| 72 | * as a task is not supposed to run for this long (at least not in web requests). |
| 73 | * |
| 74 | * A process can get stuck when a Job fails to shutdown gracefully, for instance. |
| 75 | */ |
| 76 | if ($processLocked < time() - $timeout) { |
| 77 | $this->unlockProcess(); |
| 78 | |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | // Process is locked. |
| 83 | $timeLeft = absint($timeout - (time() - $processLocked)); |
| 84 | |
| 85 | throw ProcessLockedException::processAlreadyLocked($timeLeft); |
| 86 | } |
| 87 | } |
| 88 |