Blame view

src/Request/ProcessRequestImpl/Process/ProcessManagerClass.php 10.7 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
22
23
24
25
26
<?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))
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
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
	/*
	 * @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());
ba82a624   Benjamin Renard   Get kernel execut...
161
		$this->updateProcessStatusToNode($dom,$processNode,"exectimefile",$process->getExecTimeFile());
22521f1c   Benjamin Renard   First commit
162
		$this->updateProcessStatusToNode($dom,$processNode,"exitcode",$process->getExitCode());
ba82a624   Benjamin Renard   Get kernel execut...
163
		$this->updateProcessStatusToNode($dom,$processNode,"exectime",$process->getExecTime());
22521f1c   Benjamin Renard   First commit
164
165
166
		$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...
167
		$this->updateProcessStatusToNode($dom,$processNode,"isrunning",$process->isRunning() ? "true" : "false");
22521f1c   Benjamin Renard   First commit
168
169
170
171
172
173
174
175
176
177
178
179
180
181
		$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"),
ba82a624   Benjamin Renard   Get kernel execut...
182
				"exectimefile" => $this->getNodeValueFromNode($processNode, "exectimefile"),
22521f1c   Benjamin Renard   First commit
183
				"exitcode"     => $this->getNodeValueFromNode($processNode, "exitcode"),
ba82a624   Benjamin Renard   Get kernel execut...
184
				"exectime"     => $this->getNodeValueFromNode($processNode, "exectime"),
22521f1c   Benjamin Renard   First commit
185
186
187
				"PID"          => $this->getNodeValueFromNode($processNode, "PID"),
				"runningpath"  => $this->getNodeValueFromNode($processNode, "runningpath"),
				"runningstart" => $this->getNodeValueFromNode($processNode, "runningstart"),
bda99a72   Benjamin Renard   Add kill plot req...
188
189
				"isrunning"    => ($this->getNodeValueFromNode($processNode, "isrunning") == "true"),
				"iskilled"     => ($this->getNodeValueFromNode($processNode, "iskilled") == "true"),
22521f1c   Benjamin Renard   First commit
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
				"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);
		}
8c57155b   Benjamin Renard   Integration for t...
223
		$node->nodeValue = htmlspecialchars($value);
22521f1c   Benjamin Renard   First commit
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
		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...
241
		$execTimeFile = $processInfo["exectimefile"];
22521f1c   Benjamin Renard   First commit
242
243
244
		$pid          = $processInfo["PID"];
		$runningPath  = $processInfo["runningpath"];
		$runningStart = $processInfo["runningstart"];
ba82a624   Benjamin Renard   Get kernel execut...
245
		$process->init($outputFile, $exitCodeFile, $processFile, $execTimeFile, $pid, $runningPath, $runningStart);
22521f1c   Benjamin Renard   First commit
246
247
248
		return $process;
	}

8814aa3a   Benjamin Renard   Improve iteration...
249
250
251
252
253
254
255
256
257
258
259
	// 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
260
261
262
263
264
265
266
267
268
	/*
	 * @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...
269
270
		$processNode = $processNodes->item(0);
		while ($processNode)
22521f1c   Benjamin Renard   First commit
271
272
273
274
275
276
277
278
279
280
		{
			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
281
282
					
					$dom->save($this->processManagerFilePath);
22521f1c   Benjamin Renard   First commit
283
284
285
286
287
288
				}
					
				$processInfo = $this->getProcessInfoFromNode($processNode);
					
				return array('success' => true, 'result' => $processInfo);
			}
8814aa3a   Benjamin Renard   Improve iteration...
289
			$processNode = $this->fNextEltSibling($processNode);
22521f1c   Benjamin Renard   First commit
290
291
292
293
294
295
296
297
298
299
300
		}

		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...
301
302
		$processNode = $processNodes->item(0);
		while ($processNode)
22521f1c   Benjamin Renard   First commit
303
304
305
306
307
308
309
		{
			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...
310
				$process->delete($process->getExitCode() != 0);
22521f1c   Benjamin Renard   First commit
311
312
313
314
315
316
317
					
				$dom->documentElement->removeChild($processNode);
					
				$dom->save($this->processManagerFilePath);

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

2c0e1b9a   Benjamin Renard   Remove process in...
321
322
		//Process not exists - Do not send any error
		return array('success' => true);
22521f1c   Benjamin Renard   First commit
323
	}
bda99a72   Benjamin Renard   Add kill plot req...
324
325
326
327
328
329
330
	
	/*
	 * @brief Kill a process
	*/
	private function killProcessFromId($dom, $id)
	{
		$processNodes = $dom->documentElement->getElementsByTagName("process");
8814aa3a   Benjamin Renard   Improve iteration...
331
332
		$processNode = $processNodes->item(0);
		while ($processNode)
bda99a72   Benjamin Renard   Add kill plot req...
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
		{
			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...
351
			$processNode = $this->fNextEltSibling($processNode);
bda99a72   Benjamin Renard   Add kill plot req...
352
353
354
355
		}
	
		return array('success' => false, 'message' => 'Cannot kill process from id '.$id);
	}
22521f1c   Benjamin Renard   First commit
356
357
}

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