OrchestraRequestClass.php
6.16 KB
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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* @class OrchestraRequestClass
* @brief Implementation of a Orchestra request
*/
class OrchestraRequestClass extends ProcessRequestClass
{
private $host = "";
private $port = "";
/*
* @brief Init a process request of type "run"
*/
public function init()
{
if (!isset($this->requestData))
return false;
$this->host = OrchestraConfigClass::getHost();
$this->port = OrchestraConfigClass::getPort();
if (empty($this->host)) {
$this->requestData->setLastErrorMessage('Missing orchestra host definition');
return false;
}
$orchestraEntryPoint = $this->host;
if (!empty($this->port)) {
$orchestraEntryPoint .= ":".$this->port;
}
$orchestraClient = new OrchestraClient($orchestraEntryPoint);
$params = $this->requestData->getParams();
switch ($this->requestData->getRequestType()) {
case OrchestraRequestTypeEnum::CONFIGINFO:
if (in_array(getenv("REMOTE_ADDR"), OrchestraConfigClass::getAdminIps())) {
$result = array(
'success' => TRUE,
'entrypoint' => $orchestraEntryPoint
);
}
else {
$result = array(
'success' => FALSE,
'message' => 'Forbidden'
);
}
$this->requestData->setOrchestraResult($result);
break;
case OrchestraRequestTypeEnum::MODULESLIST:
$result = $orchestraClient->getModules();
$this->requestData->setOrchestraResult($result);
break;
case OrchestraRequestTypeEnum::MODULEINFO:
if (!array_key_exists('id', $params)) {
$this->requestData->setLastErrorMessage('Missing module ID definition');
return false;
}
$result = $orchestraClient->getModuleInfo($params['id']);
$this->requestData->setOrchestraResult($result);
break;
case OrchestraRequestTypeEnum::GETRESULT:
// Nothing to do
return true;
case OrchestraRequestTypeEnum::PREDICTION:
case OrchestraRequestTypeEnum::FITTING:
if (!array_key_exists('moduleId', $params)) {
$this->requestData->setLastErrorMessage('Missing moduleId definition');
return false;
}
if (!array_key_exists('start', $params) || empty($params['start'])) {
$this->requestData->setLastErrorMessage('Missing start time definition');
return false;
}
if (!array_key_exists('stop', $params) || empty($params['stop'])) {
$this->requestData->setLastErrorMessage('Missing stop time definition');
return false;
}
return $this->initOrchestraProcess();
default:
$this->requestData->setLastErrorMessage('Unknown request type');
return false;
}
return true;
}
protected function initOrchestraProcess()
{
try {
$client = new ReflectionClass('OrchestraClient');
$clientPath = $client->getFileName();
}
catch (Exception $e) {
$this->requestData->setLastErrorMessage('Cannot retrieve OrchestraClient class');
return false;
}
$this->requestData->setCmd("php orchestra_cmd.php");
if (!parent::init()) {
return false;
}
$params = $this->requestData->getParams();
// Generate a PHP script to run request (to be able to "detach" process for the batch mode)
$php_script = '<?php'.PHP_EOL;
$php_script .= 'require_once("'.$clientPath.'");'.PHP_EOL;
$php_script .= '$client = new OrchestraClient("'.$this->host.':'.$this->port.'");'.PHP_EOL;
$php_script .= '$params = array(';
$first_param = TRUE;
foreach ($params as $key => $value) {
if ($key == 'moduleId')
continue;
if (!$first_param)
$php_script .= ',';
$first_param = false;
$php_script .= '"'.$key.'"=>"'.$value.'"';
}
$php_script .= ');'.PHP_EOL;
$php_script .= '$result = $client->runModule("'.$params['moduleId'].'",$params);'.PHP_EOL;
$php_script .= 'if ($result["success"]) {'.PHP_EOL;
$php_script .= ' $taskId = $result["result"]->task;'.PHP_EOL;
$php_script .= ' $result = $client->getTaskInfo($taskId);'.PHP_EOL;
$php_script .= ' while ($result["success"] && ($result["result"]->status == "running")) {'.PHP_EOL;
$php_script .= ' sleep(5);'.PHP_EOL;
$php_script .= ' $result = $client->getTaskInfo($taskId);'.PHP_EOL;
$php_script .= ' }'.PHP_EOL;
$php_script .= '}'.PHP_EOL;
$php_script .= 'file_put_contents("'.$this->requestData->getWorkingPath().'/'.$this->requestData->getOrchestraResult().'", json_encode($result));'.PHP_EOL;
$php_script .= 'if ($result["success"] && ($result["result"]->status == "done") && !empty($result["result"]->output)) {'.PHP_EOL;
$php_script .= ' $compressed_output = (count($result["result"]->output) > 1);'.PHP_EOL;
$php_script .= ' $output_file_path = "'.$this->requestData->getWorkingPath().'/";'.PHP_EOL;
$php_script .= ' $output_file_path .= $compressed_output ? "outputs.zip" : $result["result"]->output[0];'.PHP_EOL;
$php_script .= ' $result = $client->getTaskOutput($taskId, $output_file_path);'.PHP_EOL;
$php_script .= ' if ($result["success"]) {'.PHP_EOL;
$php_script .= ' if ($compressed_output) {'.PHP_EOL;
$php_script .= ' exec("cd '.$this->requestData->getWorkingPath().' && unzip outputs.zip");'.PHP_EOL;
$php_script .= ' }'.PHP_EOL;
$php_script .= ' }'.PHP_EOL;
$php_script .= '}'.PHP_EOL;
$php_script .= '?>';
file_put_contents($this->requestData->getWorkingPath() . "/orchestra_cmd.php", $php_script);
return true;
}
/*
* @brief Run a process request
*/
public function run()
{
if (!isset($this->requestData))
return false;
$this->requestData->setSuccess(false);
$this->requestData->setLastErrorMessage('Cannot run Orchestra request');
if (($this->requestData->getRequestType() == OrchestraRequestTypeEnum::PREDICTION) || ($this->requestData->getRequestType() == OrchestraRequestTypeEnum::FITTING)) {
$this->requestData->setType(ProcessTypeEnumClass::RUN);
return parent::run();
}
else if ($this->requestData->getRequestType() == OrchestraRequestTypeEnum::GETRESULT) {
// Nothing to do
$this->requestData->setSuccess(true);
return true;
}
else {
$result = $this->requestData->getOrchestraResult();
}
$this->requestData->setSuccess($result['success']);
if (!$result['success'])
$this->requestData->setLastErrorMessage($result['message']);
return ($result['success']);
}
}
?>