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

	protected $jobXml, $jobXmlName;

	protected $bkgRootNode = array('plot' => 'bkgPlot-treeRootNode',
			'download' => 'bkgDown-treeRootNode',
			'orbits' => 'bkgOrb-treeRootNode');

	protected $resRootNode = array('plot' => 'resPlot-treeRootNode',
			'download' => 'resDown-treeRootNode',
			'orbits' => 'resOrb-treeRootNode');

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

	/*
	 * @brief Load jobs file and create it if needed
	*/
	private function init()
	{
		$this->jobXmlName = WSConfigClass::getWsJobsFile();
		$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 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->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);
			}
		}
		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 'download' :
					$name = "download_".time();
					$info = '';
					foreach ($obj->list as $param)
					{
						$info = $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('folder', $folder);

		$newJob->setAttribute('result', $result);

		$newJob->setAttribute('start', date('d-m-Y H:i:s', $start));

		$newJob->setAttribute('result', $result);

		//to know if know if it's an immediate job or not
		$newJob->setAttribute('immediate', !$running);

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

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

		return  $this->getJobInfo($id);
	}

	/*
	 * @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 Get all jobs to clean (immediate result jobs)
	*/
// 	public function getJobsToClean()
// 	{
// 		$res = $this->init();
// 		if (!$res['success'])
// 			return $res;
// 
// 		// Get immediate jobs
// 		$xp = new domxpath($this->jobXml);
// 		$jobs = $xp->query("//job[@immediate='1']");
// 
// 		$jobsId = array();
// 		foreach ($jobs as $job)
// 			$jobsId[] = $job->getAttribute('xml:id');
// 
// 		return array("success" => true, "jobs" => $jobsId);
// 	}
}
?>