Blame view

src/Request/ProcessRequestImpl/Process/ProcessManagerClass.php 12.4 KB
22521f1c   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
/**
 * @class ProcessManagerClass
 * @brief Process manager
 * @details
 */
class ProcessManagerClass
{
	private $processManagerFilePath;

	/*
	 * @brief Constructor
	*/
	function __construct($processManagerFilePath)
	{
		$this->processManagerFilePath = $processManagerFilePath;
	}

	/*
	 * @brief Run a process
	*/
a5413d3a   Benjamin Renard   Add user name in ...
22
	public function runProcess($cmd, $runningPath, $envArray, $postProcessCmd, $getErrorMsgCmd, $batchEnabled, $user, $fromWS = FALSE)
22521f1c   Benjamin Renard   First commit
23
	{
a5413d3a   Benjamin Renard   Add user name in ...
24
		$process = new ProcessClass($cmd, $postProcessCmd, $getErrorMsgCmd, $user, $fromWS);
22521f1c   Benjamin Renard   First commit
25
26

		if (!$process->run($runningPath, $envArray))
0b6b2080   Elena.Budnik   TT download
27
			return array("success" => false, "message" => "Cannot run the process");
22521f1c   Benjamin Renard   First commit
28
29
30
31
32
33
34
35

		$res = $this->concurrentAccessProcessManagerFile(array($this,'addProcessInProcessManagerFile'),$process);

		if (!$res['success'])
		{
			$process->stop();
			return $res;
		}
bda99a72   Benjamin Renard   Add kill plot req...
36
37
38
39
		
		//Save process id in running path
		chdir($runningPath);
		file_put_contents("process_id", $res['result']['id'].PHP_EOL);
22521f1c   Benjamin Renard   First commit
40
41
42
43
44

		$process->waitEndOfProcess(array($this,'waitEndOfProcess'),$batchEnabled);

		return $this->getProcessInfo($res['result']['id'],true);
	}
bda99a72   Benjamin Renard   Add kill plot req...
45
46
47
48
49
50
51
52
53
54
	
	/*
	 * @brief Kill a process
	 */
	public function killProcess($id)
	{
		$res = $this->concurrentAccessProcessManagerFile(array($this,'killProcessFromId'),$id);
		return $res;
	}
	
22521f1c   Benjamin Renard   First commit
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
	/*
	 * @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);
a5413d3a   Benjamin Renard   Add user name in ...
126
127
				if ($res) {
					$this->cleanupProcessManagerFile($dom);
22521f1c   Benjamin Renard   First commit
128
					$func_res = call_user_func($callback,$dom,$additionalParams);
a5413d3a   Benjamin Renard   Add user name in ...
129
				}
22521f1c   Benjamin Renard   First commit
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
			}
		}
		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);
	}

	/*
a5413d3a   Benjamin Renard   Add user name in ...
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
	 * @brief Method used to be sure that the process file is always clean
	 */
	private function cleanupProcessManagerFile($dom) {
		$processNodes = $dom->documentElement->getElementsByTagName("process");
		$processNode = $processNodes->item(0);
		$to_remove = array();
		while ($processNode)
		{
			$pathNodes = $processNode->getElementsByTagName("runningpath");
			if (($pathNodes->length == 0) || empty($pathNodes->item(0)->nodeValue) ||
				(!is_dir($pathNodes->item(0)->nodeValue))) {
				$to_remove[] = $processNode;
			}
			$processNode = $this->fNextEltSibling($processNode);
		}
		foreach ($to_remove as $processNode) {
			$dom->documentElement->removeChild($processNode);
		}
		$dom->save($this->processManagerFilePath);
	}

	/*
22521f1c   Benjamin Renard   First commit
178
179
180
181
182
183
184
	 * @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());
ba82a624   Benjamin Renard   Get kernel execut...
185
		$this->updateProcessStatusToNode($dom,$processNode,"exectimefile",$process->getExecTimeFile());
e4eba677   Benjamin Renard   Get error message...
186
		$this->updateProcessStatusToNode($dom,$processNode,"errormsgfile",$process->getErrorMsgFile());
22521f1c   Benjamin Renard   First commit
187
		$this->updateProcessStatusToNode($dom,$processNode,"exitcode",$process->getExitCode());
ba82a624   Benjamin Renard   Get kernel execut...
188
		$this->updateProcessStatusToNode($dom,$processNode,"exectime",$process->getExecTime());
e4eba677   Benjamin Renard   Get error message...
189
		$this->updateProcessStatusToNode($dom,$processNode,"errormsg",$process->getErrorMsg());
22521f1c   Benjamin Renard   First commit
190
191
192
		$this->updateProcessStatusToNode($dom,$processNode,"PID",$process->getPID());
		$this->updateProcessStatusToNode($dom,$processNode,"runningpath",$process->getRunningPath());
		$this->updateProcessStatusToNode($dom,$processNode,"runningstart",$process->getRunningStart());
bda99a72   Benjamin Renard   Add kill plot req...
193
		$this->updateProcessStatusToNode($dom,$processNode,"isrunning",$process->isRunning() ? "true" : "false");
22521f1c   Benjamin Renard   First commit
194
		$this->updateProcessStatusToNode($dom,$processNode,"lastupdate",time());
a5413d3a   Benjamin Renard   Add user name in ...
195
		$this->updateProcessStatusToNode($dom,$processNode,"user", $process->getUser());
2c928626   Benjamin Renard   Add info to know ...
196
		$this->updateProcessStatusToNode($dom,$processNode,"fromws", $process->getFromWS() ? "true" : "false");
22521f1c   Benjamin Renard   First commit
197
198
199
200
201
202
203
204
205
206
207
208
209
	}

	/*
	 * @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"),
ba82a624   Benjamin Renard   Get kernel execut...
210
				"exectimefile" => $this->getNodeValueFromNode($processNode, "exectimefile"),
e4eba677   Benjamin Renard   Get error message...
211
				"errormsgfile" => $this->getNodeValueFromNode($processNode, "errormsgfile"),
22521f1c   Benjamin Renard   First commit
212
				"exitcode"     => $this->getNodeValueFromNode($processNode, "exitcode"),
ba82a624   Benjamin Renard   Get kernel execut...
213
				"exectime"     => $this->getNodeValueFromNode($processNode, "exectime"),
e4eba677   Benjamin Renard   Get error message...
214
				"errormsg"     => $this->getNodeValueFromNode($processNode, "errormsg"),
22521f1c   Benjamin Renard   First commit
215
216
217
				"PID"          => $this->getNodeValueFromNode($processNode, "PID"),
				"runningpath"  => $this->getNodeValueFromNode($processNode, "runningpath"),
				"runningstart" => $this->getNodeValueFromNode($processNode, "runningstart"),
bda99a72   Benjamin Renard   Add kill plot req...
218
219
				"isrunning"    => ($this->getNodeValueFromNode($processNode, "isrunning") == "true"),
				"iskilled"     => ($this->getNodeValueFromNode($processNode, "iskilled") == "true"),
2c928626   Benjamin Renard   Add info to know ...
220
				"lastupdate"   => $this->getNodeValueFromNode($processNode, "lastupdate"),
a5413d3a   Benjamin Renard   Add user name in ...
221
				"user"         => $this->getNodeValueFromNode($processNode, "user"),
2c928626   Benjamin Renard   Add info to know ...
222
				"fromws"       => ($this->getNodeValueFromNode($processNode, "fromws") == "true"),
22521f1c   Benjamin Renard   First commit
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
		);
	}

	/*
	 * @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);
		}
8c57155b   Benjamin Renard   Integration for t...
255
		$node->nodeValue = htmlspecialchars($value);
22521f1c   Benjamin Renard   First commit
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
		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"];
ba82a624   Benjamin Renard   Get kernel execut...
273
		$execTimeFile = $processInfo["exectimefile"];
e4eba677   Benjamin Renard   Get error message...
274
		$errorMsgFile = isset($processInfo["errormsgfile"]) ? $processInfo["errormsgfile"] : "";
22521f1c   Benjamin Renard   First commit
275
276
277
		$pid          = $processInfo["PID"];
		$runningPath  = $processInfo["runningpath"];
		$runningStart = $processInfo["runningstart"];
e4eba677   Benjamin Renard   Get error message...
278
		$process->init($outputFile, $exitCodeFile, $processFile, $execTimeFile, $errorMsgFile, $pid, $runningPath, $runningStart);
22521f1c   Benjamin Renard   First commit
279
280
281
		return $process;
	}

8814aa3a   Benjamin Renard   Improve iteration...
282
283
284
285
286
287
288
289
290
291
292
	// Next element sibling for DOMElement
	function fNextEltSibling($node) {
		while ($node && ($node = $node->nextSibling)) {
			if ($node instanceof DOMElement) {
				break;
			}
		}
		return $node;
	}


22521f1c   Benjamin Renard   First commit
293
294
295
296
297
298
299
300
301
	/*
	 * @brief Get information about a process
	*/
	private function getProcessInfoFromId($dom, $args)
	{
		$id = $args['id'];
		$update = $args['update'];
		 
		$processNodes = $dom->documentElement->getElementsByTagName("process");
8814aa3a   Benjamin Renard   Improve iteration...
302
303
		$processNode = $processNodes->item(0);
		while ($processNode)
22521f1c   Benjamin Renard   First commit
304
305
306
307
308
309
310
311
312
313
		{
			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);
8b8f98e3   Elena.Budnik   getStatus fixed
314
315
					
					$dom->save($this->processManagerFilePath);
22521f1c   Benjamin Renard   First commit
316
317
318
319
320
321
				}
					
				$processInfo = $this->getProcessInfoFromNode($processNode);
					
				return array('success' => true, 'result' => $processInfo);
			}
8814aa3a   Benjamin Renard   Improve iteration...
322
			$processNode = $this->fNextEltSibling($processNode);
22521f1c   Benjamin Renard   First commit
323
324
325
326
327
328
329
330
331
332
333
		}

		return array('success' => false, 'message' => 'Cannot get process from id');
	}

	/*
	 * @brief Delete a process
	*/
	private function deleteProcessFromId($dom, $id)
	{
		$processNodes = $dom->documentElement->getElementsByTagName("process");
8814aa3a   Benjamin Renard   Improve iteration...
334
335
		$processNode = $processNodes->item(0);
		while ($processNode)
22521f1c   Benjamin Renard   First commit
336
337
338
339
340
341
342
		{
			if ($processNode->getAttribute('xml:id') == $id)
			{
				$process = $this->getProcessFromNode($processNode);
				if (!isset($process))
					return array('success' => false, 'message' => 'Error to retrieve process info');

2c0e1b9a   Benjamin Renard   Remove process in...
343
				$process->delete($process->getExitCode() != 0);
22521f1c   Benjamin Renard   First commit
344
345
346
347
348
349
350
					
				$dom->documentElement->removeChild($processNode);
					
				$dom->save($this->processManagerFilePath);

				return array('success' => true);
			}
8814aa3a   Benjamin Renard   Improve iteration...
351
			$processNode = $this->fNextEltSibling($processNode);
22521f1c   Benjamin Renard   First commit
352
353
		}

2c0e1b9a   Benjamin Renard   Remove process in...
354
355
		//Process not exists - Do not send any error
		return array('success' => true);
22521f1c   Benjamin Renard   First commit
356
	}
bda99a72   Benjamin Renard   Add kill plot req...
357
358
359
360
361
362
363
	
	/*
	 * @brief Kill a process
	*/
	private function killProcessFromId($dom, $id)
	{
		$processNodes = $dom->documentElement->getElementsByTagName("process");
8814aa3a   Benjamin Renard   Improve iteration...
364
365
		$processNode = $processNodes->item(0);
		while ($processNode)
bda99a72   Benjamin Renard   Add kill plot req...
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
		{
			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');
			}
8814aa3a   Benjamin Renard   Improve iteration...
384
			$processNode = $this->fNextEltSibling($processNode);
bda99a72   Benjamin Renard   Add kill plot req...
385
386
387
388
		}
	
		return array('success' => false, 'message' => 'Cannot kill process from id '.$id);
	}
22521f1c   Benjamin Renard   First commit
389
390
}

8814aa3a   Benjamin Renard   Improve iteration...
391
?>