Blame view

src/Request/ProcessRequestImpl/Process/ProcessClass.php 7.5 KB
22521f1c   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
<?php
/**
 * @class ProcessClass
 * @brief Definition of a process
 * @details
 */
class ProcessClass
{
	private $command;
	private $postProcessCmd;
e4eba677   Benjamin Renard   Get error message...
11
	private $getErrorMsgCmd;
22521f1c   Benjamin Renard   First commit
12
13
14
15

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

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

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

	/*
	 * @brief Init an existing process
	*/
e4eba677   Benjamin Renard   Get error message...
41
	public function init($outputFile, $exitCodeFile, $processFile, $execTimeFile, $errorMsgFile, $pid, $runningPath, $runningStart)
22521f1c   Benjamin Renard   First commit
42
43
44
45
	{
		$this->outputFile   = $outputFile;
		$this->exitCodeFile = $exitCodeFile;
		$this->processFile  = $processFile;
ba82a624   Benjamin Renard   Get kernel execut...
46
		$this->execTimeFile = $execTimeFile;
e4eba677   Benjamin Renard   Get error message...
47
		$this->errorMsgFile = $errorMsgFile;
22521f1c   Benjamin Renard   First commit
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
		$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...
86
87
88
89
90
91
92
93
	 * @brief Get execution time of a process
	 */
	public function getExecTimeFile()
	{
		return $this->execTimeFile;
	}

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

	/*
22521f1c   Benjamin Renard   First commit
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
	 * @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...
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
	public function getErrorMsg()
	{
		switch ($this->getExitCode())
		{
			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
225
	 * @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 $!';
e4eba677   Benjamin Renard   Get error message...
226

22521f1c   Benjamin Renard   First commit
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
		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;
d68c15bf   Benjamin Renard   Reduce sleep time...
243
			usleep(100000);
22521f1c   Benjamin Renard   First commit
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
		}
		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()
	{
bda99a72   Benjamin Renard   Add kill plot req...
265
266
267
		//Kill process and all child processes
		$cmd = "pkill -TERM -P $this->pID";
		//$cmd = 'kill '.$this->pID;
22521f1c   Benjamin Renard   First commit
268
		exec($cmd);
ef39e50d   Benjamin Renard   Fix kill request ...
269
		return TRUE;
22521f1c   Benjamin Renard   First commit
270
271
272
273
274
	}

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

		if (!chdir($this->runningPath))
			return false;
		 
91e7647e   Erdogan Furkan   Integration is no...
283
284
		//if (!$keep_log && file_exists($this->outputFile))
		//	unlink($this->outputFile);
22521f1c   Benjamin Renard   First commit
285
286
287
288
289
290

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

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

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

		if (file_exists($this->errorMsgFile))
			unlink($this->errorMsgFile);
		
22521f1c   Benjamin Renard   First commit
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
		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";
ba82a624   Benjamin Renard   Get kernel execut...
315
		$this->execTimeFile = "cmd_exec_time";
e4eba677   Benjamin Renard   Get error message...
316
		$this->errorMsgFile = "cmd_errormsg";
22521f1c   Benjamin Renard   First commit
317
318
319
320
321
322
323
324
325
326

		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...
327
328
329
		if (file_exists($this->execTimeFile))
			unlink($this->execTimeFile);

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

22521f1c   Benjamin Renard   First commit
333
334
335
		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);
ba82a624   Benjamin Renard   Get kernel execut...
336
		file_put_contents($this->processFile, "CMD_START=`echo $(($(date +%s%N)/1000000))`".PHP_EOL, FILE_APPEND);
22521f1c   Benjamin Renard   First commit
337
		file_put_contents($this->processFile, $this->command." > ".$this->outputFile." 2>&1".PHP_EOL, FILE_APPEND);
e4eba677   Benjamin Renard   Get error message...
338
339
340
341
342
		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);
ba82a624   Benjamin Renard   Get kernel execut...
343
344
		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
345
346
347
348
349
350
351
352
353
		if ($this->postProcessCmd != "")
			file_put_contents($this->processFile, $this->postProcessCmd.PHP_EOL, FILE_APPEND);

		chmod($this->processFile, 0755);

		return true;
	}
}

2c0e1b9a   Benjamin Renard   Remove process in...
354
?>