Blame view

src/Request/ProcessRequestImpl/ProcessRequestClass.php 3.93 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
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
<?php
/**
 * @class ProcessRequestClass
 * @brief Implementation of a RequestAbstractClass for process request
 * @details
 */
class ProcessRequestClass extends RequestAbstractClass
{
	/*
	 * @brief Init a process request of type "run"
	*/
	private function initRun()
	{
		if ($this->requestData->getCmd() == '')
		{
			$this->requestData->setLastErrorMessage('No process command defined');
			return false;
		}

		if ($this->requestData->getWorkingPath() == '')
		{
			$this->requestData->setLastErrorMessage('No working path defined');
			return false;
		}

		if (!is_dir($this->requestData->getWorkingPath()))
		{
			if (!mkdir($this->requestData->getWorkingPath(),0777) || !is_dir($this->requestData->getWorkingPath()))
			{
				$this->requestData->setLastErrorMessage('Cannot create working dir');
				return false;
			}
		}

		return true;
	}

	/*
	 * @brief Init a process request of type "delete" or "info"
	*/
	private function initDeleteOrInfo()
	{
		if ($this->requestData->getId() == "")
		{
			$this->requestData->setLastErrorMessage('No process id defined');
			return false;
		}
		return true;
	}

	/*
	 * @brief Init a process request
	*/
	public function init()
	{
		if (!isset($this->requestData))
			return false;

		$this->requestData->setSuccess(false);
		$this->requestData->setLastErrorMessage('Cannot init process request');

		switch ($this->requestData->getType())
		{
			case ProcessTypeEnumClass::RUN :
				return $this->initRun();
			case ProcessTypeEnumClass::DELETE :
			case ProcessTypeEnumClass::INFO :
				return $this->initDeleteOrInfo();
		}

		return false;
	}

	/*
	 * @brief Update data from process info
	*/
	private function updateProcess($processInfo)
	{
		$this->requestData->setId($processInfo['id']);
		if ($processInfo['isrunning'])
			$this->requestData->setStatus(ProcessStatusEnumClass::RUNNING);
		else
		{
bda99a72   Benjamin Renard   Add kill plot req...
84
85
86
			if (isset($processInfo['iskilled']) && $processInfo['iskilled'])
				$this->requestData->setStatus(ProcessStatusEnumClass::KILLED);
			else if (!isset($processInfo['exitcode']) || ($processInfo['exitcode'] != 0))
22521f1c   Benjamin Renard   First commit
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
				$this->requestData->setStatus(ProcessStatusEnumClass::ERROR);
			else
				$this->requestData->setStatus(ProcessStatusEnumClass::DONE);
			$this->requestData->setErrorCode($processInfo['exitcode']);
		}

		$this->requestData->setStart($processInfo['runningstart']);
	}

	/*
	 * @brief Run a process request
	*/
	public function run()
	{
		if (!isset($this->requestData))
			return false;

		$this->requestData->setSuccess(false);
		$this->requestData->setLastErrorMessage('Cannot run process request');

		//create the process manager
		$processMgr = new ProcessManagerClass($this->requestData->getManagerFilePath());
			
		switch ($this->requestData->getType())
		{
			case ProcessTypeEnumClass::RUN :
				//run process
				$result = $processMgr->runProcess(
				$this->requestData->getCmd(),
				$this->requestData->getWorkingPath(),
				$this->requestData->getEnvVars(),
				$this->requestData->getPostCmd(),
				$this->requestData->getBatchEnable());
				if ($result['success'])
					$this->updateProcess($result['result']);
				break;
			case ProcessTypeEnumClass::DELETE :
				//delete process
				$result = $processMgr->deleteProcess($this->requestData->getId());
				if ($result['success'])
					$result = $result['result'];
				break;
			case ProcessTypeEnumClass::INFO :
				//get process info
				$result = $processMgr->getProcessInfo($this->requestData->getId(),true);
				if ($result['success'])
					$this->updateProcess($result['result']);
				break;
bda99a72   Benjamin Renard   Add kill plot req...
135
136
137
138
139
			case ProcessTypeEnumClass::KILL :
				$result = $processMgr->killProcess($this->requestData->getId());
				if ($result['success'])
					$result = $result['result'];
				break;
22521f1c   Benjamin Renard   First commit
140
141
142
143
144
145
146
147
148
149
150
151
152
153
			default :
				$this->requestData->setLastErrorMessage("Process method not implemented");
				return false;
		}

		$this->requestData->setSuccess($result['success']);

		if (!$result['success'])
			$this->requestData->setLastErrorMessage($result['message']);

		return ($result['success']);
	}
}
?>