'bkgSearch-treeRootNode', 'request' => 'bkgPlot-treeRootNode', 'download' => 'bkgDown-treeRootNode', 'statistics' => 'bkgStatistics-treeRootNode'); protected $resRootNode = array('condition' => 'resSearch-treeRootNode', 'request' => 'resPlot-treeRootNode', 'download' => 'resDown-treeRootNode', 'statistics' => 'resStatistics-treeRootNode'); /* * @brief Constructor */ function __construct() { } /* * @brief Load jobs file and create it if needed */ private function init() { $this->jobXmlName = IHMConfigClass::getUserJobsFile(); $this->jobXml = new DomDocument("1.0"); if (!file_exists($this->jobXmlName)) { $res = $this->createJobsFile(); if (!$res['success']) return $res; } $res = $this->jobXml->load($this->jobXmlName); if (!$res) return array( "success" => false, "message" => "Cannot load jobs file"); return array("success" => true); } /* * @brief Create a new jobs file */ private function createJobsFile() { $rootElement = $this->jobXml->createElement('jobs'); $jobsInProgress = $this->jobXml->createElement('jobsInProgress'); foreach ($this->bkgRootNode as $key => $value) { $element = $this->jobXml->createElement("$key"); $element->setAttribute('xml:id',$value); $jobsInProgress->appendChild($element); } $jobsFinished = $this->jobXml->createElement('jobsFinished'); foreach ($this->resRootNode as $key => $value) { $element = $this->jobXml->createElement("$key"); $element->setAttribute('xml:id',"$value"); $jobsFinished->appendChild($element); } $rootElement->appendChild($jobsInProgress); $rootElement->appendChild($jobsFinished); $this->jobXml->appendChild($rootElement); $res = $this->jobXml->save($this->jobXmlName); if (!$res) return array( "success" => false, "message" => "Cannot create new jobs file"); return array("success" => true); } /* * @brief Get the path of the request object file */ protected function getRequestObjectFilePath($id) { return IHMConfigClass::getUserJobsPath().$id.".request"; } /* * @brief Save a request object file */ protected function saveRequestObjectFile($obj, $id) { $file = fopen($this->getRequestObjectFilePath($id), 'w'); fwrite($file, json_encode($obj)); fclose($file); } /* * @brief Decode a request object file */ public function getRequestObjectFile($id) { if (!file_exists($this->getRequestObjectFilePath($id))) return NULL; return json_decode(file_get_contents($this->getRequestObjectFilePath($id))); } /* * @brief Delete a request object file */ protected function deleteRequestObjectFile($id) { if (file_exists($this->getRequestObjectFilePath($id))) unlink($this->getRequestObjectFilePath($id)); } /* * @brief get a job status from process info */ protected function getJobStatus($running,$exitcode) { if ($running) return 'in_progress'; if ($exitcode == 0) return 'done'; return 'error'; } /* * @brief delete a job */ public function deleteJob($id) { $res = $this->init(); if (!$res['success']) return $res; $job = $this->jobXml->getElementById($id); //delete job if (!$job) return array('success' => false, 'message' => "Job not reachable"); $folder = $job->getAttribute('folder'); //be sure that it's an AMDA working dir before deletion... $fullFolderPath = IHMConfigClass::getRequestPath().$folder.'/'; if ((isset($folder)) && ($folder != "") && is_dir($fullFolderPath) && (preg_match("/DD[0-9A-Za-z]*_/",$folder) || preg_match("/Plot[0-9]*_/",$folder))) { foreach (glob($fullFolderPath.'*') as $filename) { if (is_dir($filename) && (basename($filename) == 'params')) { //recursive deletion only for "params" dir (a full recursive deletion is probably too dangerous...) foreach (glob($filename.'/*') as $paramname) unlink($paramname); rmdir($filename); } else unlink($filename); } rmdir($fullFolderPath); } $this->deleteRequestObjectFile($id); $job->parentNode->removeChild($job); $res = $this->jobXml->save($this->jobXmlName); if (!$res) return array( 'success' => false, 'message' => "Cannot save jobs file"); return array('success' => true, 'id' => $id); } /* * @brief get job info about a job */ public function getJobInfo($id) { $res = $this->init(); if (!$res['success']) return $res; $job = $this->jobXml->getElementById($id); $attributes = []; foreach( $job->attributes as $attrName => $attrNode) { $attributes[$attrName] = $attrNode->nodeValue; } $format = 'unknown'; $compression = 'unknown'; if($job) { $name = $job->getAttribute('name'); $status = $job->getAttribute('status'); $jobType = $job->getAttribute('jobType'); $info = $job->getAttribute('info'); $start = $job->getAttribute('start'); $stop = $job->getAttribute('stop'); $result = $job->getAttribute('result'); $folder = $job->getAttribute('folder'); $request_obj = $this->getRequestObjectFile($id); if (isset($request_obj)) { if (isset($request_obj->format)) { $format = strtolower($request_obj->format); if (($format == "pdf") || ($format == "ps")) //auto compression for plot request $compression = ".tar.gz"; } if (isset($request_obj->compression)) $compression = strtolower($request_obj->compression); } $sendToSamp = $job->getAttribute('sendToSamp'); if (empty($sendToSamp)) { $sendToSamp = false; } else { $sendToSamp = ($sendToSamp == "true"); } } return array( 'success' => true, 'id' => $id, 'name' => $name, 'status' => $status, 'jobType' => $jobType, 'info' => $info, 'start' => $start, 'stop' => $stop, 'folder' => $folder, 'result' => $result, 'format' => $format, 'compression' => $compression, 'sendToSamp' => $sendToSamp, ); } /* * @brief Add a new job */ public function addJob($obj, $id, $folder, $running, $start, $result, $exitcode) { $obj = get_object_vars($obj); // Allow access to elements where the key is in dash-separated form. $res = $this->init(); if (!$res['success']) return $res; // $f = fopen('/AMDA/AMDA_Integration/debug/serialise.json', 'w') or die("Unable to open file!"); // fwrite($f, json_encode($obj, JSON_PRETTY_PRINT)); // fclose($f); $infos = []; switch ($obj['nodeType']) { // Data mining case 'condition': $name = 'datamining_'.time(); $infos['Name'] = $obj['name']; $infos['Condition'] = $obj['expression']; $infos['Start date'] = $obj['startDate']; $infos['Stop date'] = $obj['stopDate']; $infos['Duration'] = $obj['durationDay'] . 'd ' . $obj['durationHour'] . 'h ' . $obj['durationMin'] . 'm ' . $obj['durationSec'] . 's'; break; case 'statistics': $name = 'statistics_'.time(); $infos['Name'] = $obj['name']; $infos['Start date'] = $obj['startDate']; $infos['Stop date'] = $obj['stopDate']; $infos['Duration'] = $obj['durationDay'] . 'd ' . $obj['durationHour'] . 'h ' . $obj['durationMin'] . 'm ' . $obj['durationSec'] . 's'; if(array_key_exists('expression', $obj)) { $infos['Expression'] = $obj['expression']; } $params = []; foreach ($obj['parameter'] as $param) { $params[] = $param->function . '(' . $param->paramid . ')'; } if(count($params) == 1) { $infos['Parameter'] = $params[0]; } else { $infos['Parameters'] = '