ProcessClass.php
4.59 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/**
* @class ProcessClass
* @brief Definition of a process
* @details
*/
class ProcessClass
{
private $command;
private $postProcessCmd;
private $outputFile;
private $exitCodeFile;
private $processFile;
private $pID;
private $runningPath;
private $runningStart;
/*
* @brief Constructor
*/
function __construct($command, $postProcessCmd = "")
{
$this->command = $command;
$this->postProcessCmd = $postProcessCmd;
$this->pID = 0;
}
/*
* @brief Init an existing process
*/
public function init($outputFile, $exitCodeFile, $processFile, $pid, $runningPath, $runningStart)
{
$this->outputFile = $outputFile;
$this->exitCodeFile = $exitCodeFile;
$this->processFile = $processFile;
$this->pID = $pid;
$this->runningPath = $runningPath;
$this->runningStart = $runningStart;
}
/*
* @brief Get the command of the process
*/
public function getCommand()
{
return $this->command;
}
/*
* @brief Get the output file of the process
*/
public function getOutputFile()
{
return $this->outputFile;
}
/*
* @brief Get the exit code file of a process
*/
public function getExitCodeFile()
{
return $this->exitCodeFile;
}
/*
* @brief Get the process file
*/
public function getProcessFile()
{
return $this->processFile;
}
/*
* @brief Get the running path of the request
*/
public function getRunningPath()
{
return $this->runningPath;
}
/*
* @brief Get the start date of a process
*/
public function getRunningStart()
{
return $this->runningStart;
}
/*
* @brief Get the exit code of the process
*/
public function getExitCode()
{
if ($this->pID == 0)
return -1000;
if ($this->isRunning())
return -1001;
if (!chdir($this->runningPath))
return -1002;
if (!file_exists($this->exitCodeFile))
return -1003;
$result = file_get_contents($this->exitCodeFile);
if ($result === false)
return -1004;
return intval($result);
}
/*
* @brief Get the PID of the process
*/
public function getPID()
{
return $this->pID;
}
/*
* @brief Run the process
*/
public function run($runningPath, $envArray)
{
if ($this->isRunning())
return false;
if (!is_dir($runningPath))
return false;
$this->runningPath = $runningPath;
if (!$this->initRun($envArray))
return false;
$cmd = 'nohup ./'.$this->processFile.' > /dev/null 2>&1 & echo $!';
exec($cmd,$op);
$this->pID = (int)$op[0];
$this->runningStart = time();
return ($this->pID > 0);
}
/*
* @brief Wait end of the process execution
*/
public function waitEndOfProcess($callback, $batchEnabled)
{
while ($this->isRunning())
{
if (!call_user_func($callback,$this,$batchEnabled))
return false;
sleep(2);
}
return true;
}
/*
* @brief Test if the process is running
*/
public function isRunning()
{
if ($this->pID == 0)
return false;
$cmd = 'ps -p '.$this->pID;
exec($cmd,$op);
return isset($op[1]);
}
/*
* @brief Stop the process execution
*/
public function stop()
{
//Kill process and all child processes
$cmd = "pkill -TERM -P $this->pID";
//$cmd = 'kill '.$this->pID;
exec($cmd);
return !$this->isRunning();
}
/*
* @brief Delete the process
*/
public function delete()
{
if (!$this->stop())
return false;
if (!chdir($this->runningPath))
return false;
if (file_exists($this->outputFile))
unlink($this->outputFile);
if (file_exists($this->exitCodeFile))
unlink($this->exitCodeFile);
if (file_exists($this->processFile))
unlink($this->processFile);
return true;
}
/*
* @brief Init the execution of a process
*/
private function initRun($envArray)
{
if($this->isRunning())
return false;
if (!chdir($this->runningPath))
return false;
$this->outputFile = "cmd_output";
$this->exitCodeFile = "cmd_exitcode";
$this->processFile = "cmd_process";
if (file_exists($this->outputFile))
unlink($this->outputFile);
if (file_exists($this->exitCodeFile))
unlink($this->exitCodeFile);
if (file_exists($this->processFile))
unlink($this->processFile);
file_put_contents($this->processFile, "#!/bin/bash".PHP_EOL);
foreach ($envArray as $key => $value)
file_put_contents($this->processFile,"export ".$key."=".$value.PHP_EOL, FILE_APPEND);
file_put_contents($this->processFile, $this->command." > ".$this->outputFile." 2>&1".PHP_EOL, FILE_APPEND);
file_put_contents($this->processFile, "echo $? > ".$this->exitCodeFile.PHP_EOL, FILE_APPEND);
if ($this->postProcessCmd != "")
file_put_contents($this->processFile, $this->postProcessCmd.PHP_EOL, FILE_APPEND);
chmod($this->processFile, 0755);
return true;
}
}
?>