JobsMgr.js 5.42 KB
/** 
 * Project  : AMDA-NG4
 * Name     : JobsMgr.js
 * @class   amdaDesktop.JobsMgr 
 * @extends Ext.AbstractManager 
 * @brief    Manages Jobs In Progress
 * @author  elena
 * @version $Id: JobsMgr.js 2759 2015-02-19 12:32:31Z elena $
 * @todo
 *******************************************************************************
 *    FT Id     :   Date   : Name - Description
 *******************************************************************************
 *  :           :28/01/2011: 
 
 */
Ext.define('amdaDesktop.JobsMgr', {
     
     extend: 'Ext.AbstractManager',
     singleton: true,
     first : true,
     jobsInProgress : 0,
     jobsFinished : 0, 
     jobsError : 0,      
     updateStatus : null,
     interval : 60000,   //   //msec 60000
    
     jobTree : null,
     
     
//TODO first => at login !!!
     
     getStatus :  function(){  
             	   
	     AmdaAction.getJobs(function(res,e){
//TODO Errors processing	       
             if (e.status) {        
		 if (res.success) {
// Update from Server Jobs Running and Finished	 				 
		      if (res.nFinished > 0 ) {
			   Ext.each(res.jobsFinished, function(item, index, all){
                             if (item.status != 'old') {
                                switch (item.jobType) {
                                case 'condition' : var type = 'Data Mining '; break;
                                case 'request' : var type = 'Plot '; break;   
                                case 'download' : var type = 'Download '; break;
                                default: var type = 'unknown';    
                                }
                                var message = Ext.Date.format(new Date(), 'd-m-Y H:i:s: ') + ': '+ type + ' '+item.name + ' completed';
                                myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.explorer.id, true, function (module) {
                                	module.addLogMessage(message);
                                });
                             }
			   }); 
			   
		      }
			   
 		      if (amdaDesktop.JobsMgr.first && (res.nInProgress > 0 || res.nFinished > 0)) {			  
			myDesktopApp.infoMsg('You have  '+  res.nFinished  + ' new finished jobs and ' + res.nInProgress + ' jobs in progress'); 		      
		      } 
	     		 
		      amdaDesktop.JobsMgr.jobsInProgress = res.nInProgress;
		      amdaDesktop.JobsMgr.jobsFinished = res.nFinished;
		      amdaDesktop.JobsMgr.jobsError = res.nError;  
	        
		      if (res.nInProgress == 0 && amdaDesktop.JobsMgr.updateStatus)
			 {				  			 				 
			            Ext.TaskManager.stop(amdaDesktop.JobsMgr.updateStatus);
				    amdaDesktop.JobsMgr.updateStatus = null;		  				  
		       }
		      if (res.nInProgress > 0 && !amdaDesktop.JobsMgr.updateStatus)
		        {
				   amdaDesktop.JobsMgr.updateStatus =
				    Ext.TaskManager.start({
				       run : amdaDesktop.JobsMgr.getStatus,
				       interval : amdaDesktop.JobsMgr.interval 
				    });			  	    				  
			} 
			
		      if (res.nFinished > 0 && !amdaDesktop.JobsMgr.first)  {
			 if (!amdaDesktop.JobsMgr.jobTree) 
			      amdaDesktop.JobsMgr.jobTree = Ext.getCmp(amdaUI.ExplorerUI.JOB_TAB.TREE_ID);
			  
			  Ext.each(res.jobsFinished, function(job) {
                         
			   switch (job.jobType) {
			     case 'condition':	
//TODO get root nodes depending on conditions			       
				  var resRootNode = 
				     amdaDesktop.JobsMgr.jobTree.getRootNode().findChild('id',amdaModel.BkgJobNode.RES_ROOT_NODE.CONDITION, true);
				  var jobRootNode = 
				      amdaDesktop.JobsMgr.jobTree.getRootNode().findChild('id',amdaModel.BkgJobNode.JOB_ROOT_NODE.CONDITION, true);
			          break;
			      case 'request' :
				  var resRootNode = 
				     amdaDesktop.JobsMgr.jobTree.getRootNode().findChild('id',amdaModel.BkgJobNode.RES_ROOT_NODE.PLOT, true);
				  var jobRootNode = 
				      amdaDesktop.JobsMgr.jobTree.getRootNode().findChild('id',amdaModel.BkgJobNode.JOB_ROOT_NODE.PLOT, true);
			       break;
			      case 'download' :
				  var resRootNode = 
				     amdaDesktop.JobsMgr.jobTree.getRootNode().findChild('id',amdaModel.BkgJobNode.RES_ROOT_NODE.DOWNLOAD, true);
				  var jobRootNode = 
				      amdaDesktop.JobsMgr.jobTree.getRootNode().findChild('id',amdaModel.BkgJobNode.JOB_ROOT_NODE.DOWNLOAD, true);
			       break; 
			      default :
			   }
//TODO check: job sub tree is always expanded - after the first job is launched - there cannot be too many unfinished jobs on login
//  so callback is not needed			   
				  if (!resRootNode.parentNode.isExpanded()) resRootNode.parentNode.expand(false);				
				  if (!jobRootNode.parentNode.isExpanded()) jobRootNode.parentNode.expand(false);
				  if (!jobRootNode.isExpanded()) jobRootNode.expand(false);
				  
				  //AKKA - use processid to find job
				  var nodeToMove = jobRootNode.findChild('id', job.id, false);
				  nodeToMove.set('status',job.status);
//TODO check if really job.status				  
				  nodeToMove.set('iconCls',job.status);
				  jobRootNode.removeChild(nodeToMove); 
			 	  
				  if (!resRootNode.isExpanded()) {				      
				     resRootNode.expand(false, function(){				   
				       if (!resRootNode.findChild('id', nodeToMove.get('processId'))) 
					    resRootNode.appendChild(nodeToMove);
				    });				      
				  }  
				  else resRootNode.appendChild(nodeToMove); 				   				  			       
			}); 
		      } 
		 }  
            }
            amdaDesktop.JobsMgr.first = false;
        });        
    }                    
})