Blame view

src/Request/ProcessRequestImpl/Process/ProcessClass.php 7.55 KB
22521f1c   Benjamin Renard   First commit
1
<?php
0a5bb843   Menouard AZIB   Refactoring Reque...
2

22521f1c   Benjamin Renard   First commit
3
4
5
6
7
8
9
10
11
/**
 * @class ProcessClass
 * @brief Definition of a process
 * @details
 */
class ProcessClass
{
	private $command;
	private $postProcessCmd;
e4eba677   Benjamin Renard   Get error message...
12
	private $getErrorMsgCmd;
22521f1c   Benjamin Renard   First commit
13
14
15
16

	private $outputFile;
	private $exitCodeFile;
	private $processFile;
ba82a624   Benjamin Renard   Get kernel execut...
17
	private $execTimeFile;
e4eba677   Benjamin Renard   Get error message...
18
	private $errorMsgFile;
22521f1c   Benjamin Renard   First commit
19
20
21
22

	private $pID;
	private $runningPath;
	private $runningStart;
2c928626   Benjamin Renard   Add info to know ...
23
	private $fromWS;
a5413d3a   Benjamin Renard   Add user name in ...
24
	private $user;
22521f1c   Benjamin Renard   First commit
25
26
27
28

	/*
	 * @brief Constructor
	*/
a5413d3a   Benjamin Renard   Add user name in ...
29
	function __construct($command, $postProcessCmd = "", $getErrorMsgCmd = "", $user = "", $fromWS = FALSE)
22521f1c   Benjamin Renard   First commit
30
31
32
	{
		$this->command        = $command;
		$this->postProcessCmd = $postProcessCmd;
e4eba677   Benjamin Renard   Get error message...
33
		$this->getErrorMsgCmd = $getErrorMsgCmd;
22521f1c   Benjamin Renard   First commit
34
		$this->pID            = 0;
2c928626   Benjamin Renard   Add info to know ...
35
		$this->fromWS         = $fromWS;
a5413d3a   Benjamin Renard   Add user name in ...
36
		$this->user           = $user;
22521f1c   Benjamin Renard   First commit
37
38
39
40
41
	}

	/*
	 * @brief Init an existing process
	*/
e4eba677   Benjamin Renard   Get error message...
42
	public function init($outputFile, $exitCodeFile, $processFile, $execTimeFile, $errorMsgFile, $pid, $runningPath, $runningStart)
22521f1c   Benjamin Renard   First commit
43
44
45
46
	{
		$this->outputFile   = $outputFile;
		$this->exitCodeFile = $exitCodeFile;
		$this->processFile  = $processFile;
ba82a624   Benjamin Renard   Get kernel execut...
47
		$this->execTimeFile = $execTimeFile;
e4eba677   Benjamin Renard   Get error message...
48
		$this->errorMsgFile = $errorMsgFile;
22521f1c   Benjamin Renard   First commit
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
		$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;
	}

	/*
ba82a624   Benjamin Renard   Get kernel execut...
87
88
89
90
91
92
93
94
	 * @brief Get execution time of a process
	 */
	public function getExecTimeFile()
	{
		return $this->execTimeFile;
	}

	/*
e4eba677   Benjamin Renard   Get error message...
95
96
97
98
99
100
101
102
	 * @brief Get the error msg file
	 */
	public function getErrorMsgFile()
	{
		return $this->errorMsgFile;
	}

	/*
22521f1c   Benjamin Renard   First commit
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
	 * @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);
	}

e4eba677   Benjamin Renard   Get error message...
139
140
	public function getErrorMsg()
	{
0a5bb843   Menouard AZIB   Refactoring Reque...
141
		switch ($this->getExitCode()) {
e4eba677   Benjamin Renard   Get error message...
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
			case 0:
				return ""; //No error
			case -1000:
				return "No process Id";
			case -1001:
				return "Request is running.";
			case -1002:
				return "Cannot access to the request directory";
			case -1003:
				return "Cannot retrieve exit code file";
			case -1004:
				return "Cannot read exit code file";
		}
		if (!file_exists($this->errorMsgFile)) {
			return "Cannot retrieve error message file";
		}
		$result = file_get_contents($this->errorMsgFile);

		if ($result === false)
			return "Cannot read error message file";

		$result = trim($result);
		if (empty($result))
			return "Unknown error";

		return $result;
	}

22521f1c   Benjamin Renard   First commit
170
	/*
ba82a624   Benjamin Renard   Get kernel execut...
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
	 * @brief Get execution time of the process
	 */
	public function getExecTime()
	{
		if ($this->getExitCode() != 0)
			return 0;
		$result = file_get_contents($this->execTimeFile);

		if ($result === false)
			return 0;

		return intval($result);
	}

	/*
22521f1c   Benjamin Renard   First commit
186
187
188
189
190
191
192
193
	 * @brief Get the PID of the process
	*/
	public function getPID()
	{
		return $this->pID;
	}

	/*
2c928626   Benjamin Renard   Add info to know ...
194
195
196
197
198
199
200
201
	 * @brief To know if a process is running from WebService
	 */
	public function getFromWS()
	{
		return $this->fromWS;
	}

	/*
a5413d3a   Benjamin Renard   Add user name in ...
202
203
204
205
206
207
208
209
	 * @brief Get user that run the request. Empty if not known.
	 */
	public function getUser()
	{
		return $this->user;
	}

	/*
22521f1c   Benjamin Renard   First commit
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
	 * @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;

0a5bb843   Menouard AZIB   Refactoring Reque...
225
		$cmd = 'nohup ./' . $this->processFile . ' > /dev/null 2>&1 & echo $!';
e4eba677   Benjamin Renard   Get error message...
226

0a5bb843   Menouard AZIB   Refactoring Reque...
227
		exec($cmd, $op);
22521f1c   Benjamin Renard   First commit
228
229
230
231
232
233
234
235
236
237
238
		$this->pID = (int)$op[0];
		$this->runningStart = time();

		return ($this->pID > 0);
	}

	/*
	 * @brief Wait end of the process execution
	*/
	public function waitEndOfProcess($callback, $batchEnabled)
	{
0a5bb843   Menouard AZIB   Refactoring Reque...
239
240
		while ($this->isRunning()) {
			if (!call_user_func($callback, $this, $batchEnabled))
22521f1c   Benjamin Renard   First commit
241
				return false;
d68c15bf   Benjamin Renard   Reduce sleep time...
242
			usleep(100000);
22521f1c   Benjamin Renard   First commit
243
244
245
246
247
248
249
250
251
252
253
		}
		return true;
	}

	/*
	 * @brief Test if the process is running
	*/
	public function isRunning()
	{
		if ($this->pID == 0)
			return false;
0a5bb843   Menouard AZIB   Refactoring Reque...
254
255
		$cmd = 'ps -p ' . $this->pID;
		exec($cmd, $op);
22521f1c   Benjamin Renard   First commit
256
257
258
259
260
261
262
263
		return isset($op[1]);
	}

	/*
	 * @brief Stop the process execution
	*/
	public function stop()
	{
bda99a72   Benjamin Renard   Add kill plot req...
264
265
266
		//Kill process and all child processes
		$cmd = "pkill -TERM -P $this->pID";
		//$cmd = 'kill '.$this->pID;
22521f1c   Benjamin Renard   First commit
267
		exec($cmd);
ef39e50d   Benjamin Renard   Fix kill request ...
268
		return TRUE;
22521f1c   Benjamin Renard   First commit
269
270
271
272
273
	}

	/*
	 * @brief Delete the process
	*/
2c0e1b9a   Benjamin Renard   Remove process in...
274
	public function delete($keep_log)
22521f1c   Benjamin Renard   First commit
275
276
277
278
279
280
	{
		if (!$this->stop())
			return false;

		if (!chdir($this->runningPath))
			return false;
0a5bb843   Menouard AZIB   Refactoring Reque...
281

acc7504d   Benjamin Renard   Revert some unexp...
282
283
		if (!$keep_log && file_exists($this->outputFile))
			unlink($this->outputFile);
22521f1c   Benjamin Renard   First commit
284
285
286
287
288
289

		if (file_exists($this->exitCodeFile))
			unlink($this->exitCodeFile);

		if (file_exists($this->processFile))
			unlink($this->processFile);
ba82a624   Benjamin Renard   Get kernel execut...
290
291
292

		if (file_exists($this->execTimeFile))
			unlink($this->execTimeFile);
e4eba677   Benjamin Renard   Get error message...
293
294
295

		if (file_exists($this->errorMsgFile))
			unlink($this->errorMsgFile);
0a5bb843   Menouard AZIB   Refactoring Reque...
296

22521f1c   Benjamin Renard   First commit
297
298
299
300
301
302
303
304
		return true;
	}

	/*
	 * @brief Init the execution of a process
	*/
	private function initRun($envArray)
	{
0a5bb843   Menouard AZIB   Refactoring Reque...
305
		if ($this->isRunning())
22521f1c   Benjamin Renard   First commit
306
307
308
309
310
311
312
313
			return false;

		if (!chdir($this->runningPath))
			return false;

		$this->outputFile   = "cmd_output";
		$this->exitCodeFile = "cmd_exitcode";
		$this->processFile  = "cmd_process";
ba82a624   Benjamin Renard   Get kernel execut...
314
		$this->execTimeFile = "cmd_exec_time";
e4eba677   Benjamin Renard   Get error message...
315
		$this->errorMsgFile = "cmd_errormsg";
22521f1c   Benjamin Renard   First commit
316
317
318
319
320
321
322
323
324
325

		if (file_exists($this->outputFile))
			unlink($this->outputFile);

		if (file_exists($this->exitCodeFile))
			unlink($this->exitCodeFile);

		if (file_exists($this->processFile))
			unlink($this->processFile);

ba82a624   Benjamin Renard   Get kernel execut...
326
327
328
		if (file_exists($this->execTimeFile))
			unlink($this->execTimeFile);

e4eba677   Benjamin Renard   Get error message...
329
330
331
		if (file_exists($this->errorMsgFile))
			unlink($this->errorMsgFile);

0a5bb843   Menouard AZIB   Refactoring Reque...
332
		file_put_contents($this->processFile, "#!/bin/bash" . PHP_EOL);
22521f1c   Benjamin Renard   First commit
333
		foreach ($envArray as $key => $value)
0a5bb843   Menouard AZIB   Refactoring Reque...
334
335
336
337
338
339
340
341
342
343
			file_put_contents($this->processFile, "export " . $key . "=" . $value . PHP_EOL, FILE_APPEND);
		file_put_contents($this->processFile, "CMD_START=`echo $(($(date +%s%N)/1000000))`" . PHP_EOL, FILE_APPEND);
		file_put_contents($this->processFile, $this->command . " > " . $this->outputFile . " 2>&1" . PHP_EOL, FILE_APPEND);
		file_put_contents($this->processFile, "ERROR_CODE=$?" . PHP_EOL, FILE_APPEND);
		file_put_contents($this->processFile, "echo \$ERROR_CODE > " . $this->exitCodeFile . PHP_EOL, FILE_APPEND);
		file_put_contents($this->processFile, "if [ \$ERROR_CODE -ne 0 ] && [ \"" . $this->getErrorMsgCmd . "\" != \"\" ]; then" . PHP_EOL, FILE_APPEND);
		file_put_contents($this->processFile, "    " . $this->getErrorMsgCmd . " > " . $this->errorMsgFile . PHP_EOL, FILE_APPEND);
		file_put_contents($this->processFile, "fi;" . PHP_EOL, FILE_APPEND);
		file_put_contents($this->processFile, "CMD_STOP=`echo $(($(date +%s%N)/1000000))`" . PHP_EOL, FILE_APPEND);
		file_put_contents($this->processFile, "echo $((\$CMD_STOP-\$CMD_START)) > " . $this->execTimeFile . PHP_EOL, FILE_APPEND);
22521f1c   Benjamin Renard   First commit
344
		if ($this->postProcessCmd != "")
0a5bb843   Menouard AZIB   Refactoring Reque...
345
			file_put_contents($this->processFile, $this->postProcessCmd . PHP_EOL, FILE_APPEND);
22521f1c   Benjamin Renard   First commit
346
347
348
349
350
351

		chmod($this->processFile, 0755);

		return true;
	}
}