-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLock.php
More file actions
111 lines (93 loc) · 2.87 KB
/
Copy pathCommandLock.php
File metadata and controls
111 lines (93 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace ADT\CommandLock;
use ADT\Utils\FileSystem;
use Exception;
trait CommandLock
{
abstract public function getName();
private string $lockPath;
/**
* @param string $path Directory with locks
* @param string $format Should only be file name, any instances of `$cmd$` are replaced with $commandName
* in subsequent getPath and getFolder calls
* @throws Exception
*/
public function setCommandLockPath(string $path, string $format = '$cmd$.lock')
{
if ($path[strlen($path) - 1] !== '/' && $path[strlen($path) - 1] !== '\\') {
$path .= '/';
}
if (strpos($path, '$cmd$') !== false) {
throw new Exception("Path can't include \$cmd\$");
}
if (strpos($format, '$cmd$') === false) {
$this->lockPath = $path . '.$cmd$.lock';
}
else {
$this->lockPath = $path . $format;
}
}
/**
* Tries to create a lock file with its process id.
*
* @param string|null $identifier If set, adds a "-$identifier" suffix to the name of the lock file.
* @return bool true in case of success, false otherwise
* @throws Exception
*/
protected function tryLock(?string $identifier = null): bool
{
// folder containg all the locks
$folderName = $this->getFolder($identifier);
FileSystem::createDirAtomically($folderName);
$pathName = $this->getPath($identifier);
$pidFilePath = $pathName . '/pid';
if (file_exists($pathName)) {
// If pgid can be retrieved, the process that owned the lock is still running
if (posix_getpgid((int) file_get_contents($pidFilePath)) !== false) {
return false;
}
rmdir($pathName);
}
if (FileSystem::createDirAtomically($pathName)) {
if (file_put_contents($pidFilePath, getmypid())) {
return true;
}
rmdir($pathName);
}
throw new Exception('CommandLock: Failed to create lock');
}
/**
* Tries to unlock a lock.
*
* @param string|null $identifier If set, adds a "-$identifier" suffix to the name of the lock file
* @return bool true in case of success, false otherwise
* @throws Exception
*/
protected function tryUnlock(?string $identifier = null): bool
{
$pathName = $this->getPath($identifier);
if (!file_exists($pathName) || rmdir($pathName)) {
return true;
}
throw new Exception('CommandLock: Failed to remove lock.');
}
private function getPath($identifier = null)
{
$fullName = static::getFullName($this->getName(), $identifier);
return $this->getFormattedLockPath($fullName);
}
private function getFolder($identifier = null)
{
$path = $this->getPath($identifier);
return substr($path, 0, strripos($path, '/') + 1);
}
private function getFormattedLockPath(string $commandName = '')
{
$commandName = preg_replace('/[^-a-zA-Z0-9]/', '-', $commandName);
return str_replace('$cmd$', $commandName, $this->lockPath);
}
private static function getFullName($name, $identifier): string
{
return $name . (is_string($identifier) ? "-$identifier" : '');
}
}