void ProcessMonitor::waitForProcess(){
	//this function waits for process closing and restart process if needed
	do{
		WaitForSingleObject(pHandle, INFINITE);//wait for process closing

		if (getLastAction() == LastAction::START || (getLastAction() == LastAction::RESTART && getStatus() != ProcessStatus::RESTARTING)){
		//if (getLastAction() != LastAction::STOP){
			//must restart process only if it was closed by itself
			logMessage(L"Process closed by itself", true);
			
			setProcessStatus(ProcessStatus::STOPPED);//update status

			crashCallbackMutex.lock();
			if(onProcCrash)std::thread(onProcCrash).detach();//execute callback
			crashCallbackMutex.unlock();
			
			if (!runProc()){
				break;//if we can't start process back we leave
			}
			else {
				setProcessStatus(ProcessStatus::IS_WORKING);//if we can, update status
			}
		}
		else{
			break;//we closed that process 
		}
	} while (true);
}
ProcessMonitor::ProcessMonitor(unsigned long pid, Logger* logger){
	//Initialization
	this->logger = logger;
	this->commandLine = getCommandLineForProcessByPID(pid);
	previousActionFinished = true;
	//If command line wasn't extracted
	if (this->commandLine == NULL){
		
		std::wstringstream message;
		message << "Process ["<<pid<<"] not found. Error = " << GetLastError();		
		logMessage(message.str().c_str());

		setLastAction(LastAction::STOP);
		setProcessStatus(ProcessStatus::STOPPED);
		this->pid = 0;
		this->pHandle = NULL;
		this->tHandle = NULL;
	}
	else{
		//if command line was extracted;
		this->pid = pid;
		this->pHandle = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
		this->tHandle = NULL;//just for unification with ProcessInformation structure
		setLastAction(LastAction::START);
		setProcessStatus(ProcessStatus::IS_WORKING);
		logMessage(L"Watching process", true);
		waitingThread = std::thread(&ProcessMonitor::waitForProcess, this);//wait for process closing in another thread
	}
}
bool ProcessMonitor::start(){	
	//interface for starting a process
	std::unique_lock <std::mutex> uniqueOperationLock(operationMutex);
	while (!previousActionFinished) actionFinishedCondition.wait(uniqueOperationLock);//wait until another action(stop, restart) finish
	previousActionFinished = false;
	

	if (getStatus() == ProcessStatus::STOPPED){
		//start a new process only if current is stopped
		setLastAction(LastAction::START);
		if (runProc()){
			//if process was started
			setProcessStatus(ProcessStatus::IS_WORKING);//update status
			if (waitingThread.joinable()) waitingThread.join();			
			waitingThread = std::thread(&ProcessMonitor::waitForProcess, this);//wait for process closing in another thread
			
			previousActionFinished = true;
			actionFinishedCondition.notify_one();//next action can start working
			
			return true;//process was successfully started
		}		
	}
	
	previousActionFinished = true;
	actionFinishedCondition.notify_one();
	return false;//Process was already running or can't start a new process
}
Пример #4
0
void rspfHistogramWriter::abort()
{
   //---
   // Call base abort. This sets the status to PROCESS_STATUS_ABORT_REQUEST so the processor
   // knows to abort.
   //---
   rspfProcessInterface::abort();

   // Propagate to histo source.
   if (theHistogramSource)
   {
      rspfImageHistogramSource* histoSource =
         PTR_CAST(rspfImageHistogramSource, theHistogramSource);
      if (histoSource)
      {
         histoSource->abort();
      }
   }

   //---
   // Now set status to "ABORTED" so rspfProcessInterface::isAborted returns true so the
   // writeHistogram method doesn't write the file if the process was aborted.
   //---
   setProcessStatus(PROCESS_STATUS_ABORTED);
}
bool ProcessMonitor::restart(){
	std::unique_lock <std::mutex> uniqueOperationLock(operationMutex);
	while (!previousActionFinished) actionFinishedCondition.wait(uniqueOperationLock);//wait until another action(start, stort) finish
	previousActionFinished = false;

	if (getStatus() == ProcessStatus::IS_WORKING){
		//restart process only if it's running
		setLastAction(LastAction::RESTART);
		setProcessStatus(ProcessStatus::RESTARTING);//update status
		if (restartProc()){
			//if process was successfully restarted
			setProcessStatus(ProcessStatus::IS_WORKING);//update status
			if(waitingThread.joinable()) waitingThread.join();//wait for previous call of waitFunction to finish
			waitingThread = std::thread(&ProcessMonitor::waitForProcess, this);//wait for process closing in another thread			
				
			previousActionFinished = true;
			actionFinishedCondition.notify_one();//next action can start working
			
			return true;
		}
		else{
			//if something gone wrong
			DWORD exitCode;
			if (GetExitCodeProcess(pi.hProcess, &exitCode)){
				//checking exit code
				if (exitCode == 259) setProcessStatus(ProcessStatus::IS_WORKING);//259==RUNNING
				else setProcessStatus(ProcessStatus::STOPPED);//any another exit code indicate crash or closing
			}
			else{
				//can't get exit code
				setProcessStatus(ProcessStatus::STOPPED);
			}
		}				
	}

	previousActionFinished = true;
	actionFinishedCondition.notify_one();//next action can start working
	return false;//Process wasn't restarted or process wasn't restarted correct
}
bool ProcessMonitor::stop(){
	//interface for stopping a process
	std::unique_lock <std::mutex> uniqueOperationLock(operationMutex);
	while (!previousActionFinished) actionFinishedCondition.wait(uniqueOperationLock);//wait until another action(start, restart) finish
	previousActionFinished = false;
	
	if (getStatus() == ProcessStatus::IS_WORKING){
		//stop process only if it's running
		setLastAction(LastAction::STOP);
		if (stopProc()){
			//if process was successfully stopped
			setProcessStatus(ProcessStatus::STOPPED);//update status
			previousActionFinished = true;
			actionFinishedCondition.notify_one();//next action can start working

			return true;//process was successfuly stopped
		}
	}

	previousActionFinished = true;
	actionFinishedCondition.notify_one();//next action can start working
	return false;//process was not running or can't stop process
}
Пример #7
0
G15LCDEngineHelper::~G15LCDEngineHelper() {
	setProcessStatus(false);
}
Пример #8
0
G15LCDEngineLGLCD::~G15LCDEngineLGLCD() {
    setProcessStatus(false);
}
Пример #9
0
bool ossimImageFileWriter::execute()
{
   static const char MODULE[] = "ossimImageFileWriter::execute";
   
   if (!theInputConnection) // Must have a sequencer...
   {
      if (traceDebug())
      {
         ossimNotify(ossimNotifyLevel_DEBUG)
            << MODULE << " DEBUG:\ntheInputConnection is NULL!" << endl;
      }
      return false;
   }
   
   if (theFilename == "") // Must have a filename...
   {
      if (traceDebug())
      {
         ossimNotify(ossimNotifyLevel_DEBUG)
            << MODULE << " DEBUG:\ntheFilename is empty!" << endl;
      }
      return false;
   }
   
   
   // make sure we have a region of interest
   if(theAreaOfInterest.hasNans())
   {
      theInputConnection->initialize();
      theAreaOfInterest = theInputConnection->getAreaOfInterest();
   }
   else
   {
      theInputConnection->setAreaOfInterest(theAreaOfInterest);
   }

   if(theAreaOfInterest.hasNans()) // Must have an area of interest...
   {
      if (traceDebug())
      {
         ossimNotify(ossimNotifyLevel_DEBUG)
            << MODULE << " ERROR:  Area of interest has nans!"
            << "Area of interest:  "
            << theAreaOfInterest
            << "\nReturning..." << endl;
      }

      return false;
   }

   if (traceDebug())
   {
      ossimNotify(ossimNotifyLevel_DEBUG)
         << MODULE << " DEBUG:\n"
         << "Area of interest:  " << theAreaOfInterest << endl;
   }
   
   ossimRefPtr<ossimImageSource> savedInput;
   if (theScaleToEightBitFlag)
   {
      if(theInputConnection->getOutputScalarType() != OSSIM_UINT8)
      {
         savedInput = new ossimScalarRemapper;
         
         savedInput->connectMyInputTo(0, theInputConnection->getInput(0));
         theInputConnection->connectMyInputTo(0, savedInput.get());
         theInputConnection->initialize();
      }
   }

   ossimFilename path = theFilename.path();
   if (!path.empty() && !path.exists() )
   {
      path.createDirectory(true);
   }
   setProcessStatus(ossimProcessInterface::PROCESS_STATUS_EXECUTING);
   //setPercentComplete(0.0); let writeFile set the precent complete
   bool wroteFile = true;
   bool result    = true;
   if (theWriteImageFlag)
   {
      wroteFile = writeFile();
   }
  
   /*
   if(needsAborting())
   {
      setProcessStatus(ossimProcessInterface::PROCESS_STATUS_ABORTED);
   }
   else
   {
      setProcessStatus(ossimProcessInterface::PROCESS_STATUS_NOT_EXECUTING);
   }
   */
   if (theWriteOverviewFlag&&!needsAborting())
   {
      // Writing overviews has been mpi'd so do on all processes.
      if (!writeOverviewFile(theOverviewCompressType,
                             theOverviewJpegCompressQuality))
      {
         if(traceDebug())
         {
            ossimNotify(ossimNotifyLevel_WARN)
            << "Write of overview file failed!" << endl;
         }
      }
   }

   if(!needsAborting())
   {
      // Do these only on the master process. Note left to right precedence!
      if (getSequencer() && getSequencer()->isMaster())
      {
         if (wroteFile)
         {
            if (writeMetaDataFiles() == false)
            {
               if(traceDebug())
               {
                  ossimNotify(ossimNotifyLevel_WARN)
                  << "Write of metadata file failed!" << endl;
               }
               result = false;
            }
         }
         
      } // End of "if (getSequencer() && getSequencer()->isMaster()))
   }

   if(savedInput.valid())
   {
      ossimConnectableObject* obj = theInputConnection->getInput(0);
      if(obj)
      {
         theInputConnection->connectMyInputTo(0, obj->getInput(0));
      }
   }
   if(!needsAborting())
   {
      setProcessStatus(ossimProcessInterface::PROCESS_STATUS_NOT_EXECUTING);
   }
   else 
   {
      setProcessStatus(ossimProcessInterface::PROCESS_STATUS_ABORTED);   
   }

   savedInput = 0;
   return result;
}