Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 47 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| ProcessLock | |
0.00% |
0 / 47 |
|
0.00% |
0 / 8 |
462 | |
0.00% |
0 / 1 |
| getRetries | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setRetries | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getRetryUsleep | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setRetryUsleep | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| aquire | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
42 | |||
| release | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| release_all | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| release_old | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | class ProcessLock { |
| 3 | |
| 4 | private $lockFilePath; |
| 5 | private $lockDirPath = "T:\\phptmp_vitex\\"; |
| 6 | private $retries = 10; |
| 7 | private $retry_usleep = 900000; |
| 8 | |
| 9 | /** |
| 10 | * @return int |
| 11 | */ |
| 12 | public function getRetries(): int |
| 13 | { |
| 14 | return $this->retries; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * @param int $retries |
| 19 | */ |
| 20 | public function setRetries(int $retries): void |
| 21 | { |
| 22 | $this->retries = $retries; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @return int |
| 27 | */ |
| 28 | public function getRetryUsleep(): int |
| 29 | { |
| 30 | return $this->retry_usleep; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @param int $retry_usleep |
| 35 | */ |
| 36 | public function setRetryUsleep(int $retry_usleep): void |
| 37 | { |
| 38 | $this->retry_usleep = $retry_usleep; |
| 39 | } |
| 40 | |
| 41 | function aquire($script) { |
| 42 | |
| 43 | $this->lockFilePath = $this->lockDirPath.$script.".lock"; |
| 44 | |
| 45 | $retries = 0; $fe = false; |
| 46 | do{ |
| 47 | $fe = file_exists($this->lockFilePath); |
| 48 | if($fe) |
| 49 | usleep($this->retry_usleep); // wait to reissue |
| 50 | }while($fe && $retries++ < $this->retries); |
| 51 | |
| 52 | if($fe) |
| 53 | return false; |
| 54 | |
| 55 | // Get the PID and place into the file... |
| 56 | $pid = getmypid(); |
| 57 | |
| 58 | // Hopefully our write is atomic. |
| 59 | @file_put_contents($this->lockFilePath, $pid); |
| 60 | |
| 61 | $fe = file_exists($this->lockFilePath); |
| 62 | |
| 63 | if (!$fe) |
| 64 | return false; // our file magically disapeared? |
| 65 | |
| 66 | $contents = file_get_contents($this->lockFilePath); |
| 67 | |
| 68 | $lockedPID = (int)$contents; |
| 69 | |
| 70 | if ($pid === $lockedPID) |
| 71 | return true; |
| 72 | |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | function release() { |
| 77 | try { |
| 78 | clearstatcache(); |
| 79 | if(file_exists($this->lockFilePath)) |
| 80 | @unlink($this->lockFilePath); |
| 81 | return true; |
| 82 | } catch(Throwable) { |
| 83 | return false; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | function release_all($olderThan = 1) { |
| 88 | deleteDirectory($this->lockDirPath, true, 'lock', $olderThan); |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | function release_old($dir = "T:\\phptmp_vitex\\", $max_age = 1) { |
| 93 | $dir = $this->lockDirPath; |
| 94 | $list = array(); |
| 95 | |
| 96 | $limit = time() - $max_age; |
| 97 | |
| 98 | $dir = realpath($dir); |
| 99 | |
| 100 | if (!is_dir($dir)) { |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | $dh = opendir($dir); |
| 105 | if ($dh === false) { |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | while (($file = readdir($dh)) !== false) { |
| 110 | if (strpos(".lock", $file) > 0 ) { |
| 111 | $file = $dir . '/' . $file; |
| 112 | if (!is_file($file)) { |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | if (filemtime($file) < $limit) { |
| 117 | $list[] = $file; |
| 118 | unlink($file); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | closedir($dh); |
| 123 | return $list; |
| 124 | |
| 125 | } |
| 126 | } |