Commit bda99a720426c9a493fa91ede3695c79f651f72b

Authored by Benjamin Renard
1 parent ea91395b

Add kill plot request

src/InputOutput/IHMImpl/IHMInputOutputClass.php
... ... @@ -53,7 +53,11 @@ class IHMInputOutputClass implements InputOutputInterface
53 53 case 'statistics' :
54 54 //catalog generation
55 55 $this->inputOutput = new IHMInputOutputParamsStatisticsClass();
56   - break;
  56 + break;
  57 + case 'killplot' :
  58 + $requestId = "Plot";
  59 + $this->inputOutput = new IHMInputOutputParamsKillPlotClass();
  60 + break;
57 61 default :
58 62 throw new Exception('Params request type '.$input_request->nodeType.' not implemented for this client.');
59 63 }
... ...
src/InputOutput/IHMImpl/Params/PlotImpl/IHMInputOutputParamsKillPlotClass.php 0 → 100644
... ... @@ -0,0 +1,48 @@
  1 +<?php
  2 +
  3 +/**
  4 + * @class IHMInputOutputParamsKillPlotClass
  5 + * @brief Implementation of IHMInputOutputParamsKillPlotClass to kill plot request
  6 + * @details
  7 +*/
  8 +class IHMInputOutputParamsKillPlotClass extends IHMInputOutputParamsAbstractClass
  9 +{
  10 + /*
  11 + * @brief method to unmarshall a plot request
  12 + */
  13 + protected function unmarshallRequest($input)
  14 + {
  15 +
  16 + $this->paramsData->setType(ProcessTypeEnumClass::KILL);
  17 +
  18 + $requestIdFilePath = $this->getWorkingPath().'process_id';
  19 +
  20 + if (!file_exists($requestIdFilePath))
  21 + throw new Exception('Cannot retrieve current running plot request.');
  22 +
  23 + $process_id = file_get_contents($requestIdFilePath);
  24 +
  25 + if ($process_id === false)
  26 + throw new Exception('Cannot retrieve current running plot request.');
  27 +
  28 + $process_id = str_replace(array("\r", "\n"), '', $process_id);
  29 +
  30 + $this->paramsData->setId($process_id);
  31 +
  32 + return $this->paramsData;
  33 + }
  34 +
  35 + /*
  36 + * @brief method to marshall the result of a kill plot request
  37 + */
  38 + protected function marshallResult($data)
  39 + {
  40 + if (!$data->getSuccess())
  41 + return array(
  42 + 'success' => false,
  43 + 'message' => $data->getLastErrorMessage());
  44 +
  45 + return array('success' => true);
  46 + }
  47 +}
  48 +?>
0 49 \ No newline at end of file
... ...
src/InputOutput/IHMImpl/Params/PlotImpl/IHMInputOutputParamsPlotClass.php
... ... @@ -836,7 +836,11 @@ class IHMInputOutputParamsPlotClass extends IHMInputOutputParamsAbstractClass
836 836 'id' => $data->getId(),
837 837 'folder' => $this->getWorkingDirName(),
838 838 'result' => $result
839   - );
  839 + );
  840 + case ProcessStatusEnumClass::KILLED :
  841 + return array(
  842 + 'success' => true,
  843 + 'killed' => true);
840 844 default :
841 845 return array(
842 846 'success' => false,
... ...
src/Request/ParamsRequestImpl/ParamsRequestClass.php
... ... @@ -14,6 +14,9 @@ class ParamsRequestClass extends ProcessRequestClass
14 14 if (!isset($this->requestData))
15 15 return false;
16 16  
  17 + if ($this->requestData->getType(ProcessTypeEnumClass::KILL))
  18 + return true;
  19 +
17 20 $this->requestData->setType(ProcessTypeEnumClass::RUN);
18 21  
19 22 switch ($this->requestData->getRequestType())
... ...
src/Request/ProcessRequestImpl/Process/ProcessClass.php
... ... @@ -172,7 +172,9 @@ class ProcessClass
172 172 */
173 173 public function stop()
174 174 {
175   - $cmd = 'kill '.$this->pID;
  175 + //Kill process and all child processes
  176 + $cmd = "pkill -TERM -P $this->pID";
  177 + //$cmd = 'kill '.$this->pID;
176 178 exec($cmd);
177 179 return !$this->isRunning();
178 180 }
... ...
src/Request/ProcessRequestImpl/Process/ProcessManagerClass.php
... ... @@ -33,12 +33,25 @@ class ProcessManagerClass
33 33 $process->stop();
34 34 return $res;
35 35 }
  36 +
  37 + //Save process id in running path
  38 + chdir($runningPath);
  39 + file_put_contents("process_id", $res['result']['id'].PHP_EOL);
36 40  
37 41 $process->waitEndOfProcess(array($this,'waitEndOfProcess'),$batchEnabled);
38 42  
39 43 return $this->getProcessInfo($res['result']['id'],true);
40 44 }
41   -
  45 +
  46 + /*
  47 + * @brief Kill a process
  48 + */
  49 + public function killProcess($id)
  50 + {
  51 + $res = $this->concurrentAccessProcessManagerFile(array($this,'killProcessFromId'),$id);
  52 + return $res;
  53 + }
  54 +
42 55 /*
43 56 * @brief Get info about a process
44 57 */
... ... @@ -149,7 +162,7 @@ class ProcessManagerClass
149 162 $this->updateProcessStatusToNode($dom,$processNode,"PID",$process->getPID());
150 163 $this->updateProcessStatusToNode($dom,$processNode,"runningpath",$process->getRunningPath());
151 164 $this->updateProcessStatusToNode($dom,$processNode,"runningstart",$process->getRunningStart());
152   - $this->updateProcessStatusToNode($dom,$processNode,"isrunning",$process->isRunning());
  165 + $this->updateProcessStatusToNode($dom,$processNode,"isrunning",$process->isRunning() ? "true" : "false");
153 166 $this->updateProcessStatusToNode($dom,$processNode,"lastupdate",time());
154 167 }
155 168  
... ... @@ -168,7 +181,8 @@ class ProcessManagerClass
168 181 "PID" => $this->getNodeValueFromNode($processNode, "PID"),
169 182 "runningpath" => $this->getNodeValueFromNode($processNode, "runningpath"),
170 183 "runningstart" => $this->getNodeValueFromNode($processNode, "runningstart"),
171   - "isrunning" => $this->getNodeValueFromNode($processNode, "isrunning"),
  184 + "isrunning" => ($this->getNodeValueFromNode($processNode, "isrunning") == "true"),
  185 + "iskilled" => ($this->getNodeValueFromNode($processNode, "iskilled") == "true"),
172 186 "lastupdate" => $this->getNodeValueFromNode($processNode, "lastupdate")
173 187 );
174 188 }
... ... @@ -284,6 +298,36 @@ class ProcessManagerClass
284 298  
285 299 return array('success' => false, 'message' => 'Cannot get process from id');
286 300 }
  301 +
  302 + /*
  303 + * @brief Kill a process
  304 + */
  305 + private function killProcessFromId($dom, $id)
  306 + {
  307 + $processNodes = $dom->documentElement->getElementsByTagName("process");
  308 + foreach ($processNodes as $processNode)
  309 + {
  310 + if ($processNode->getAttribute('xml:id') == $id)
  311 + {
  312 + $process = $this->getProcessFromNode($processNode);
  313 + if (!isset($process))
  314 + return array('success' => false, 'message' => 'Error to retrieve process info');
  315 +
  316 + $this->updateProcessStatusToNode($dom,$processNode,"isrunning","false");
  317 + $this->updateProcessStatusToNode($dom,$processNode,"iskilled","true");
  318 +
  319 + if ($process->stop())
  320 + {
  321 + $dom->save($this->processManagerFilePath);
  322 +
  323 + return array('success' => true);
  324 + }
  325 + return array('success' => false, 'message' => 'Cannot process from id');
  326 + }
  327 + }
  328 +
  329 + return array('success' => false, 'message' => 'Cannot kill process from id '.$id);
  330 + }
287 331 }
288 332  
289 333 ?>
290 334 \ No newline at end of file
... ...
src/Request/ProcessRequestImpl/ProcessRequestClass.php
... ... @@ -81,7 +81,9 @@ class ProcessRequestClass extends RequestAbstractClass
81 81 $this->requestData->setStatus(ProcessStatusEnumClass::RUNNING);
82 82 else
83 83 {
84   - if (!isset($processInfo['exitcode']) || ($processInfo['exitcode'] != 0))
  84 + if (isset($processInfo['iskilled']) && $processInfo['iskilled'])
  85 + $this->requestData->setStatus(ProcessStatusEnumClass::KILLED);
  86 + else if (!isset($processInfo['exitcode']) || ($processInfo['exitcode'] != 0))
85 87 $this->requestData->setStatus(ProcessStatusEnumClass::ERROR);
86 88 else
87 89 $this->requestData->setStatus(ProcessStatusEnumClass::DONE);
... ... @@ -130,6 +132,11 @@ class ProcessRequestClass extends RequestAbstractClass
130 132 if ($result['success'])
131 133 $this->updateProcess($result['result']);
132 134 break;
  135 + case ProcessTypeEnumClass::KILL :
  136 + $result = $processMgr->killProcess($this->requestData->getId());
  137 + if ($result['success'])
  138 + $result = $result['result'];
  139 + break;
133 140 default :
134 141 $this->requestData->setLastErrorMessage("Process method not implemented");
135 142 return false;
... ...
src/Request/ProcessRequestImpl/ProcessRequestDataClass.php
... ... @@ -11,6 +11,7 @@ abstract class ProcessTypeEnumClass
11 11 const RUN = "run";
12 12 const DELETE = "delete";
13 13 const INFO = "info";
  14 + const KILL = "kill";
14 15 }
15 16  
16 17 /**
... ... @@ -24,6 +25,7 @@ abstract class ProcessStatusEnumClass
24 25 const ERROR = "error";
25 26 const RUNNING = "run";
26 27 const DONE = "done";
  28 + const KILLED = "killed";
27 29 }
28 30  
29 31 /**
... ...