1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Funivan\Console\SingleState; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Command status |
9
|
|
|
* According to name we check if command is running |
10
|
|
|
* |
11
|
|
|
* @author Ivan Shcherbak <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class CommandState { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $pidFile; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Unique file name pid file name. |
23
|
|
|
* |
24
|
|
|
* @param Command $command |
25
|
|
|
* @param null|string $tmpDirectory |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Command $command, $tmpDirectory = null) { |
28
|
|
|
$tmpDirectory = $tmpDirectory ? $tmpDirectory : sys_get_temp_dir(); |
29
|
|
|
|
30
|
|
|
if (!is_dir($tmpDirectory) or !is_writable($tmpDirectory)) { |
31
|
|
|
throw new \RuntimeException(sprintf('The directory "%s" is not writable.', $tmpDirectory), 0, null, $tmpDirectory); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$partName = preg_replace('/[^a-z0-9\._-]+/i', '-', $command->getName()); |
35
|
|
|
$partName = substr($partName, 0, 40); |
36
|
|
|
|
37
|
|
|
$this->pidFile = sprintf('%s/atl.%s.%s.pid', $tmpDirectory, $partName, hash('sha256', $command->getName())); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return bool |
43
|
|
|
* @throws \Exception |
44
|
|
|
*/ |
45
|
|
|
public function isAlive() { |
46
|
|
|
$pid = $this->getPid(); |
47
|
|
|
|
48
|
|
|
if ($pid === null) { |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$result = exec('ps aux | awk \'{print $2}\' | grep "^' . $pid . '$"'); |
53
|
|
|
|
54
|
|
|
return !empty($result); |
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $pid |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
|
|
public function setPid($pid) { |
64
|
|
|
|
65
|
|
|
if (!is_int($pid)) { |
66
|
|
|
throw new \InvalidArgumentException('Invalid pid format. Expect only digits. Pid:' . $pid); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
file_put_contents($this->pidFile, $pid); |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return null|int |
76
|
|
|
* @throws \Exception |
77
|
|
|
*/ |
78
|
|
|
public function getPid() { |
79
|
|
|
if (!is_file($this->pidFile)) { |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$data = @file_get_contents($this->pidFile); |
84
|
|
|
if (empty($data)) { |
85
|
|
|
return null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$data = trim($data); |
89
|
|
|
$pid = (int) $data; |
90
|
|
|
if ($pid != $data) { |
91
|
|
|
throw new \RuntimeException('File corrupted. Invalid pid format. File path:' . $this->getPidFilePath()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $pid; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Remove pid file |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public function unlink() { |
104
|
|
|
if (is_file($this->pidFile)) { |
105
|
|
|
unlink($this->pidFile); |
106
|
|
|
} |
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function getPidFilePath() { |
115
|
|
|
return $this->pidFile; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|