ProcessManagerClass.php
9.88 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
/**
* @class ProcessManagerClass
* @brief Process manager
* @details
*/
class ProcessManagerClass
{
private $processManagerFilePath;
/*
* @brief Constructor
*/
function __construct($processManagerFilePath)
{
$this->processManagerFilePath = $processManagerFilePath;
}
/*
* @brief Run a process
*/
public function runProcess($cmd, $runningPath, $envArray, $postProcessCmd, $batchEnabled)
{
$process = new ProcessClass($cmd, $postProcessCmd);
if (!$process->run($runningPath, $envArray))
return array("success" => false, "message" => "Cannot run the process");
$res = $this->concurrentAccessProcessManagerFile(array($this,'addProcessInProcessManagerFile'),$process);
if (!$res['success'])
{
$process->stop();
return $res;
}
//Save process id in running path
chdir($runningPath);
file_put_contents("process_id", $res['result']['id'].PHP_EOL);
$process->waitEndOfProcess(array($this,'waitEndOfProcess'),$batchEnabled);
return $this->getProcessInfo($res['result']['id'],true);
}
/*
* @brief Kill a process
*/
public function killProcess($id)
{
$res = $this->concurrentAccessProcessManagerFile(array($this,'killProcessFromId'),$id);
return $res;
}
/*
* @brief Get info about a process
*/
public function getProcessInfo($id,$update)
{
$res = $this->concurrentAccessProcessManagerFile(array($this,'getProcessInfoFromId'),array('id' => $id, 'update' => $update));
if (!$res['success'])
return $res;
return $res['result'];
}
/*
* @brief Delete a process
*/
public function deleteProcess($id)
{
$res = $this->concurrentAccessProcessManagerFile(array($this,'deleteProcessFromId'),$id);
return $res;
}
/*
* @brief Wait the end of the execution (or the timeout) of a process.
*/
public function waitEndOfProcess($process, $batchEnabled)
{
if ($batchEnabled)
if (time() - $process->getRunningStart() > KernelConfigClass::getTimeToBatchMode())
return false;
return true;
}
/*
* @brief Add a process in the manager file
*/
private function addProcessInProcessManagerFile($dom, $process) {
$processId = "process_".CommonClass::generateRandomString(6).'_'.time().'_'.$process->getPID();
$processNode = $dom->createElement("process");
$processNode->setAttribute('xml:id', $processId);
$dom->documentElement->appendChild($processNode);
$this->updateProcessInProcessNode($dom, $processNode, $process);
$dom->save($this->processManagerFilePath);
return $this->getProcessInfoFromNode($processNode);
}
/*
* @brief Protection for concurrent access to the manager file
*/
private function concurrentAccessProcessManagerFile($callback, $additionalParams)
{
$lockFile = $this->processManagerFilePath.".lockfile";
$fp = fopen($lockFile, "w+");
if ($fp === false)
return array('success' => false, 'message' => 'Cannot open process manager lock file');
$res = true;
if (flock($fp, LOCK_EX))
{
if (!file_exists($this->processManagerFilePath))
$res = $this->createProcessManagerFile();
if ($res)
{
$dom = new DOMDocument("1.0","UTF-8");
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$res = $dom->load($this->processManagerFilePath);
if ($res)
$func_res = call_user_func($callback,$dom,$additionalParams);
}
}
else
$res = false;
fclose($fp);
if ($res)
return array('success' => true, 'result' => $func_res);
return array('success' => false, 'message' => 'Error during the concurrent access of the process manager file');
}
/*
* @brief Create a new manager file
*/
private function createProcessManagerFile() {
$dom = new DOMDocument("1.0","UTF-8");
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$rootNode = $dom->createElement("processlist");
$dom->appendChild($rootNode);
return $dom->save($this->processManagerFilePath);
}
/*
* @brief Update process info if a node of the manager file
*/
private function updateProcessInProcessNode($dom, $processNode, $process) {
$this->updateProcessStatusToNode($dom,$processNode,"cmd",$process->getCommand());
$this->updateProcessStatusToNode($dom,$processNode,"outputfile",$process->getOutputFile());
$this->updateProcessStatusToNode($dom,$processNode,"exitcodefile",$process->getExitCodeFile());
$this->updateProcessStatusToNode($dom,$processNode,"processfile",$process->getProcessFile());
$this->updateProcessStatusToNode($dom,$processNode,"exitcode",$process->getExitCode());
$this->updateProcessStatusToNode($dom,$processNode,"PID",$process->getPID());
$this->updateProcessStatusToNode($dom,$processNode,"runningpath",$process->getRunningPath());
$this->updateProcessStatusToNode($dom,$processNode,"runningstart",$process->getRunningStart());
$this->updateProcessStatusToNode($dom,$processNode,"isrunning",$process->isRunning() ? "true" : "false");
$this->updateProcessStatusToNode($dom,$processNode,"lastupdate",time());
}
/*
* @brief Extract process info from a node
*/
private function getProcessInfoFromNode($processNode)
{
return array(
"id" => $processNode->getAttribute("xml:id"),
"cmd" => $this->getNodeValueFromNode($processNode, "cmd"),
"outputfile" => $this->getNodeValueFromNode($processNode, "outputfile"),
"exitcodefile" => $this->getNodeValueFromNode($processNode, "exitcodefile"),
"processfile" => $this->getNodeValueFromNode($processNode, "processfile"),
"exitcode" => $this->getNodeValueFromNode($processNode, "exitcode"),
"PID" => $this->getNodeValueFromNode($processNode, "PID"),
"runningpath" => $this->getNodeValueFromNode($processNode, "runningpath"),
"runningstart" => $this->getNodeValueFromNode($processNode, "runningstart"),
"isrunning" => ($this->getNodeValueFromNode($processNode, "isrunning") == "true"),
"iskilled" => ($this->getNodeValueFromNode($processNode, "iskilled") == "true"),
"lastupdate" => $this->getNodeValueFromNode($processNode, "lastupdate")
);
}
/*
* @brief Get a node by tag name and by a given parent node
*/
private function getNodeFromNode($parentNode, $name) {
$nodes = $parentNode->getElementsByTagName($name);
if ($nodes->length > 0)
return $nodes->item(0);
return NULL;
}
/*
* @brief Get the value of a node by tag name and by a given parent node
*/
private function getNodeValueFromNode($parentNode, $name) {
$node = $this->getNodeFromNode($parentNode, $name);
if ($node == NULL)
return NULL;
return $node->nodeValue;
}
/*
* @brief Update a process info in a process node
*/
private function updateProcessStatusToNode($dom, $parentNode, $name, $value) {
$node = $this->getNodeFromNode($parentNode, $name);
if ($node == NULL) {
$node = $dom->createElement($name);
$parentNode->appendChild($node);
}
$node->nodeValue = htmlspecialchars($value);
return $node;
}
/*
* @brief Create a Process from node
*/
private function getProcessFromNode($processNode) {
$cmd = $this->getNodeValueFromNode($processNode, "cmd");
if ($cmd == NULL)
return NULL;
$process = new ProcessClass($cmd);
if ($process === false)
return NULL;
$processInfo = $this->getProcessInfoFromNode($processNode);
$outputFile = $processInfo["outputfile"];
$processFile = $processInfo["processfile"];
$exitCodeFile = $processInfo["exitcodefile"];
$pid = $processInfo["PID"];
$runningPath = $processInfo["runningpath"];
$runningStart = $processInfo["runningstart"];
$process->init($outputFile, $exitCodeFile, $processFile, $pid, $runningPath, $runningStart);
return $process;
}
/*
* @brief Get information about a process
*/
private function getProcessInfoFromId($dom, $args)
{
$id = $args['id'];
$update = $args['update'];
$processNodes = $dom->documentElement->getElementsByTagName("process");
foreach ($processNodes as $processNode)
{
if ($processNode->getAttribute('xml:id') == $id)
{
if ($update)
{
$process = $this->getProcessFromNode($processNode);
if (!isset($process))
return array('success' => false, 'message' => 'Error to retrieve process info');
$this->updateProcessInProcessNode($dom, $processNode, $process);
$dom->save($this->processManagerFilePath);
}
$processInfo = $this->getProcessInfoFromNode($processNode);
return array('success' => true, 'result' => $processInfo);
}
}
return array('success' => false, 'message' => 'Cannot get process from id');
}
/*
* @brief Delete a process
*/
private function deleteProcessFromId($dom, $id)
{
$processNodes = $dom->documentElement->getElementsByTagName("process");
foreach ($processNodes as $processNode)
{
if ($processNode->getAttribute('xml:id') == $id)
{
$process = $this->getProcessFromNode($processNode);
if (!isset($process))
return array('success' => false, 'message' => 'Error to retrieve process info');
$process->delete();
$dom->documentElement->removeChild($processNode);
$dom->save($this->processManagerFilePath);
return array('success' => true);
}
}
return array('success' => false, 'message' => 'Cannot get process from id');
}
/*
* @brief Kill a process
*/
private function killProcessFromId($dom, $id)
{
$processNodes = $dom->documentElement->getElementsByTagName("process");
foreach ($processNodes as $processNode)
{
if ($processNode->getAttribute('xml:id') == $id)
{
$process = $this->getProcessFromNode($processNode);
if (!isset($process))
return array('success' => false, 'message' => 'Error to retrieve process info');
$this->updateProcessStatusToNode($dom,$processNode,"isrunning","false");
$this->updateProcessStatusToNode($dom,$processNode,"iskilled","true");
if ($process->stop())
{
$dom->save($this->processManagerFilePath);
return array('success' => true);
}
return array('success' => false, 'message' => 'Cannot process from id');
}
}
return array('success' => false, 'message' => 'Cannot kill process from id '.$id);
}
}
?>