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