PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.5.0
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.5.0
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Backup / BackupProcessLock.php
wp-staging / Backup Last commit date
Ajax 2 years ago BackgroundProcessing 2 years ago Dto 2 years ago Entity 2 years ago Exceptions 2 years ago Job 2 years ago Request 2 years ago Service 2 years ago Storage 2 years ago Task 2 years ago AfterRestore.php 3 years ago BackupDeleter.php 3 years ago BackupDownload.php 2 years ago BackupFileIndex.php 2 years ago BackupProcessLock.php 2 years ago BackupRepairer.php 3 years ago BackupRetentionHandler.php 2 years ago BackupScheduler.php 2 years ago BackupServiceProvider.php 2 years ago BackupValidator.php 2 years ago WithBackupIdentifier.php 2 years 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