WSJobsManagerClass.php 7.53 KB
<?php
/**
 * @class WSJobsManagerClass
 * @brief Jobs manager
 * @details
 */
class WSJobsManagerClass extends IHMJobsManagerClass {

	protected $jobXml, $jobXmlName;

	protected $bkgRootNode = array(WSConfigClass::PLOT => 'bkgPlotRootNode',
			WSConfigClass::PARAMETER => 'bkgParamRootNode',
			WSConfigClass::ORBIT => 'bkgOrbRootNode',
			WSConfigClass::DATASET => 'bkgDatasetRootNode');

	protected $resRootNode = array(WSConfigClass::PLOT => 'resPlotRootNode',
			WSConfigClass::PARAMETER => 'resParamRootNode',
			WSConfigClass::ORBIT => 'resOrbRootNode',
			WSConfigClass::DATASET => 'resDatasetRootNode');

	/*
	 * @brief Constructor
	*/
	function __construct()
	{
	}

	/*
	 * @brief Load jobs file and create it if needed
	*/
	protected function init()
	{
		$this->jobXmlName = WSConfigClass::getWsJobsFile();
		$this->jobXml = new DomDocument("1.0");

		if (!file_exists($this->jobXmlName))
		{
			$res = $this->createJobsFile();
			if (!$res['success']) {
				error_log("WebServices Error : Cannot create WSjobs.xml", 1, email);
				return $res;
			}
		}
		$res = $this->jobXml->load($this->jobXmlName);
		if (!$res) {
			error_log("WebServices Error : Cannot load WSjobs.xml", 1, email);
			return array(
					"success" => false,
					"message" => "Cannot load jobs file");
			}

		return array("success" => true);
	}
 
	/*
	 * @brief get job info about a job
	*/
	public function getJobInfo($id)
	{
		$res = $this->init();
		if (!$res['success'])
			return $res;

		$job = $this->jobXml->getElementById($id);
		$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->fileformat))
				{
					$format = strtolower($request_obj->format);
					if (($format == "pdf") || ($format == "ps"))
						//auto compression for plot request
						$compression = ".tar.gz";
						
					// if ($format == "ascii") $format = 'txt';
				}
				if (isset($request_obj->compression))
					$compression = strtolower($request_obj->compression);
			}
		}
		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
		);
	}

	/*
	 * @brief Add a new job
	*/
	public function addJob($obj, $id, $folder,
			$running, $start, $result, $exitcode)
	{
		$res = $this->init();
		
		if (!$res['success'])
			return $res;
			
		$key = WSInputOutputClass::getService();
		
		$newJob = $this->jobXml->createElement('job');

		$newJob->setAttribute('xml:id', $id);
 		$newJob->setAttribute('jobType', $key);

// 		switch ($key)
// 		{
// 			case 'parameter' :
// 				$name = "download_".time();
// 				$info = '';
// 				foreach ($obj->list as $param)
// 				{
// 					$info .= ' '.$param->paramid; //data
// 				}
// 				break;
// 			case 'plot'	 :
// 				$name = "request_".time();
// 				$info = '';
// 				for ($i=0; $i < count($obj->children); $i++) {
// 					for ($j=0; $j < count($obj->children[$i]->children); $j++) {
// 						$info = $info.' '.$obj->children[$i]->children[$j]->paramid;
// 					}
// 				}
// 				break;
// 			default:
// 				$name = "unknown_".time();
// 				$info = '';
// 		}

// 		$newJob->setAttribute('name', $name);
// 		$newJob->setAttribute('info', $info);
		$newJob->setAttribute('result', $result);
		$newJob->setAttribute('folder', $folder);
		$newJob->setAttribute('start', date('d-m-Y H:i:s', $start));
		$newJob->setAttribute('user', IHMConfigClass::getUserName());
		$newJob->setAttribute('host', IHMConfigClass::getUserHost());
		//to know if know if it's an immediate job or not
// 		$newJob->setAttribute('immediate', !$running);

		if ($running)
		{
			$rootJobNode = $this->jobXml->getElementById($this->bkgRootNode[$key]);
			if (!$rootJobNode)
			{
				$rootJobNode =  $this->jobXml->createElement("$key");
				$rootJobNode->setAttribute('xml:id', $this->bkgRootNode[$key]);
				$jobsInProgress = $this->jobXml->getElementsByTagName('jobsInProgress')->item(0);
				$jobsInProgress->appendChild($rootJobNode);
			}
		}
		else
		{
			$rootJobNode = $this->jobXml->getElementById($this->resRootNode[$key]);
			if (!$rootJobNode)
			{
				$rootJobNode =  $this->jobXml->createElement("$key");
				$rootJobNode->setAttribute('xml:id', $this->resRootNode[$key]);
				$jobsFinished = $this->jobXml->getElementsByTagName('jobsFinished')->item(0);
				$jobsFinished->appendChild($rootJobNode);
			}
		}

		$rootJobNode->appendChild($newJob);
		 
		if (!$this->jobXml->save($this->jobXmlName))
			return  array("success" => false, "message" => "Cannot save job manager file");

		$this->saveRequestObjectFile($obj, $id);

		return  $this->updateJobStatus($id, $running, $exitcode);
	}

	/*
	 * @brief Update the status of a job
	*/
// 	public function updateJobStatus($id, $running, $exitcode)
// 	{
// 		$res = $this->init();
// 		if (!$res['success'])
// 			return $res;
// 
// 		$job = $this->jobXml->getElementById($id);
// 
// 		if (!isset($job))
// 			return array("success" => false, "message" => "Cannot found job");
// 			
// 		$jobstatus = $this->getJobStatus($running,$exitcode);
// 		$job->setAttribute('status', $jobstatus);
// 
// 		if ($running)
// 			$job->setAttribute('stop', 'unknown');
// 		else if ($job->getAttribute('stop') == '' || $job->getAttribute('stop') == 'unknown')
// 		{
// 			$job->setAttribute('stop', date('d-m-Y H:i:s', time()));
// 			$this->jobXml->getElementById($this->resRootNode[$job->getAttribute('jobType')])->appendChild($job);
// 		}
// 			
// 		$res = $this->jobXml->save($this->jobXmlName);
// 
// 		if (!$res)
// 			return array(
// 					'success' => false,
// 					'message' => "Cannot save jobs status file");
// 			
// 		return $this->getJobInfo($id);
// 	}
	
	/*
	 * @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);
			
		return array('success' => true, 'id' => $id);
	}
	
	public function getResultFromProcessId($id)
	{
		$res = $this->init();
		$job = $this->jobXml->getElementById($id);
		
		if (!$job)
			return array('success' => false, 'message' => "Job not reachable"); 
		
		return array('success' => true, 'result' => $job->getAttribute('result')); 
	}
}
?>