#from pyOpenRPA.Orchestrator import Managers from .. import __Orchestrator__ import os class Process(): """ Manager process, which is need to be started / stopped / restarted With Process instance you can automate your process activity. Use schedule package to set interval when process should be active and when not. Process instance has the following statuses: - 0_STOPPED - 1_STOPPED_MANUAL - 2_STOP_SAFE - 3_STOP_SAFE_MANUAL - 4_STARTED - 5_STARTED_MANUAL """ mAgentHostNameStr = None mAgentUserNameStr = None mStartPathStr = None mStartCMDStr = None mProcessNameWOExeStr = None mStopSafeTimeoutSecFloat = None mStatusStr = None # 0_STOPPED 1_STOPPED_MANUAL 2_STOP_SAFE 3_STOP_SAFE_MANUAL 4_STARTED 5_STARTED_MANUAL def __init__(self, inAgentHostNameStr, inAgentUserNameStr, inProcessNameWOExeStr, inStartPathStr=None, inStartCMDStr = None, inStopSafeTimeoutSecFloat=120): self.mAgentHostNameStr = inAgentHostNameStr self.mAgentUserNameStr = inAgentUserNameStr self.mStartPathStr = inStartPathStr self.mStartCMDStr = inStartCMDStr self.mProcessNameWOExeStr = inProcessNameWOExeStr self.mStopSafeTimeoutSecFloat = inStopSafeTimeoutSecFloat __Orchestrator__.GSettingsGet()["ManagersProcessDict"][inProcessNameWOExeStr.upper()]=self self.StatusCheck() def Manual2Auto(self): """ Remove Manual flag from process (if exists) - it will allow the schedule operations via def StatusCheckStart(self): def StatusCheckStorForce(self): def StatusCheckStopSafe(self): :return: """ if self.mStatusStr=="1_STOPPED_MANUAL": self.mStatusStr = "0_STOPPED" if self.mStatusStr=="3_STOP_SAFE_MANUAL": self.mStatusStr = "2_STOP_SAFE" if self.mStatusStr=="5_STARTED_MANUAL": self.mStatusStr = "4_STARTED" def Start(self, inIsManualBool = True): """ Manual/Auto start. Manual start will block scheduling execution. To return schedule execution use def Manual2Auto :param inIsManualBool: Default is True - Mark this operation as manual - StatusCheckStart/Stop will be blocked - only StatusCheck will be working. False - Auto operation :return: """ # Send activity item to agent - wait result if self.mStartPathStr is not None: lCMDStr = f"start {os.path.abspath(self.mStartPathStr)}" elif self.mStartCMDStr is not None: lCMDStr = f"start {self.mStartCMDStr}" #import pdb #pdb.set_trace() lActivityItemStart = __Orchestrator__.ProcessorActivityItemCreate(inDef="OSCMD", inArgDict={"inCMDStr":lCMDStr,"inSendOutputToOrchestratorLogsBool":False}, inArgGSettingsStr="inGSettings") lGUIDStr = __Orchestrator__.AgentActivityItemAdd(inHostNameStr=self.mAgentHostNameStr, inUserStr=self.mAgentUserNameStr, inActivityItemDict=lActivityItemStart) lStartResult = __Orchestrator__.AgentActivityItemReturnGet(inGUIDStr=lGUIDStr) if inIsManualBool==True: self.mStatusStr = "5_STARTED_MANUAL" else: self.mStatusStr = "4_STARTED" # Log info about process lL = __Orchestrator__.OrchestratorLoggerGet() lL.info(f"Managers.Process: {self.mProcessNameWOExeStr} change status to {self.mStatusStr})") def StopSafe(self, inIsManualBool = True): """ Manual/Auto stop safe. Stop safe is the operation which send signal to process to terminate own work (send term signal to process). Managers.Process wait for the mStopSafeTimeoutSecFloat seconds. After that, if process is not terminated - self will StopForce it. Manual stop safe will block scheduling execution. To return schedule execution use def Manual2Auto :param inIsManualBool: Default is True - Mark this operation as manual - StatusCheckStart/Stop will be blocked - only StatusCheck will be working. False - Auto operation :return: """ pass def StopForce(self, inIsManualBool = True): """ Manual/Auto stop force. Force stop dont wait process termination - it just terminate process now. Manual stop safe will block scheduling execution. To return schedule execution use def Manual2Auto :param inIsManualBool: Default is True - Mark this operation as manual - StatusCheckStart/Stop will be blocked - only StatusCheck will be working. False - Auto operation :return: """ # Send activity item to agent - wait result lCMDStr = f'taskkill /im "{self.mProcessNameWOExeStr}.exe" /fi "username eq %USERNAME%"' lActivityItemStart = __Orchestrator__.ProcessorActivityItemCreate( inDef="OSCMD",inArgDict={"inCMDStr": lCMDStr,"inSendOutputToOrchestratorLogsBool":False},inArgGSettingsStr="inGSettings") lGUIDStr = __Orchestrator__.AgentActivityItemAdd(inHostNameStr=self.mAgentHostNameStr, inUserStr=self.mAgentUserNameStr, inActivityItemDict=lActivityItemStart) lStartResult = __Orchestrator__.AgentActivityItemReturnGet(inGUIDStr=lGUIDStr) if inIsManualBool==True: self.mStatusStr = "1_STOPPED_MANUAL" else: self.mStatusStr = "0_STOPPED" # Log info about process lL = __Orchestrator__.OrchestratorLoggerGet() lL.info(f"Managers.Process: {self.mProcessNameWOExeStr} change status to {self.mStatusStr})") def RestartSafe(self, inIsManualBool = True): """ Manual/Auto restart safe. Restart safe is the operation which send signal to process to terminate own work (send term signal to process). Then it run process. Managers.Process wait for the mStopSafeTimeoutSecFloat seconds. After that, if process is not terminated - self will StopForce it. Manual stop safe will block scheduling execution. To return schedule execution use def Manual2Auto :param inIsManualBool: Default is True - Mark this operation as manual - StatusCheckStart/Stop will be blocked - only StatusCheck will be working. False - Auto operation :return: """ pass def RestartForce(self, inIsManualBool = True): """ Manual/Auto restart force. Force restart dont wait process termination - it just terminate process now ant then start it. Manual restart will block scheduling execution. To return schedule execution use def Manual2Auto :param inIsManualBool: Default is True - Mark this operation as manual - StatusCheckStart/Stop will be blocked - only StatusCheck will be working. False - Auto operation :return: """ pass def StatusCheck(self): """ Check if process is alive. The def will save the manual flag is exists. :return: """ # Send activity item to agent - wait result lActivityItemUserProcessList = __Orchestrator__.ProcessorActivityItemCreate(inDef="ProcessWOExeUpperUserListGet") lGUIDStr = __Orchestrator__.AgentActivityItemAdd(inHostNameStr=self.mAgentHostNameStr,inUserStr=self.mAgentUserNameStr,inActivityItemDict=lActivityItemUserProcessList) lUserProcessList = __Orchestrator__.AgentActivityItemReturnGet(inGUIDStr=lGUIDStr) if self.mProcessNameWOExeStr.upper() in lUserProcessList: if self.mStatusStr == "1_STOPPED_MANUAL": self.mStatusStr = "5_STARTED_MANUAL" if self.mStatusStr == "0_STOPPED": self.mStatusStr = "4_STARTED" else: if self.mStatusStr == "5_STARTED_MANUAL": self.mStatusStr = "1_STOPPED_MANUAL" if self.mStatusStr == "4_STARTED": self.mStatusStr = "0_STOPPED" def StatusCheckStart(self): """ Check process status and run it if auto stopped self.mStatusStr is "0_STOPPED" :return: """ pass def StatusCheckStopForce(self): """ Check process status and auto stop force it if self.mStatusStr is 4_STARTED :return: """ pass def StatusCheckStopSafe(self): """ Check process status and auto stop safe it if self.mStatusStr is 4_STARTED :return: """ pass def ScheduleWeekDay(self): """ Some template def to work with schedule package. Configure schedule to start. Stop process in auto mode in all sele. :return: """ pass def ProcessGet(inProcessNameWOExeStr: str) -> Process: """ Return the process instance by the inProcessNameWOExeStr :param inProcessNameWOExeStr: The process name without extension .exe (the key of the Process instance). Any case - will be processed to the upper case :return: Process instance (if exists) Else None """ return __Orchestrator__.GSettingsGet()["ManagersProcessDict"].get(inProcessNameWOExeStr.upper(),None) def ProcessStatusStrGet(inProcessNameWOExeStr): """ Get the status of the Process instance. :param inProcessNameWOExeStr: The process name without extension .exe (the key of the Process instance). Any case - will be processed to the upper case :return: Str Process instance has the following statuses: - 0_STOPPED - 1_STOPPED_MANUAL - 2_STOP_SAFE - 3_STOP_SAFE_MANUAL - 4_STARTED - 5_STARTED_MANUAL - None (if Process instance not exists) """ lProcess = ProcessGet(inProcessNameWOExeStr=inProcessNameWOExeStr) if lProcess is not None: return lProcess.mStatusStr else: return None def ProcessStart(inProcessNameWOExeStr: str, inIsManualBool: bool = True) -> None: """ Manual/Auto start. Manual start will block scheduling execution. To return schedule execution use def Manual2Auto :param inProcessNameWOExeStr: The process name without extension .exe (the key of the Process instance). Any case - will be processed to the upper case :param inIsManualBool: Default is True - Mark this operation as manual - StatusCheckStart/Stop will be blocked - only StatusCheck will be working. False - Auto operation :return: """ lProcess = ProcessGet(inProcessNameWOExeStr=inProcessNameWOExeStr) if lProcess is not None: return lProcess.Start(inIsManualBool=inIsManualBool) else: return None def ProcessStopSafe(inProcessNameWOExeStr: str, inIsManualBool: bool = True) -> None: """ Manual/Auto stop safe. Stop safe is the operation which send signal to process to terminate own work (send term signal to process). Managers.Process wait for the mStopSafeTimeoutSecFloat seconds. After that, if process is not terminated - self will StopForce it. Manual stop safe will block scheduling execution. To return schedule execution use def Manual2Auto :param inProcessNameWOExeStr: The process name without extension .exe (the key of the Process instance). Any case - will be processed to the upper case :param inIsManualBool: Default is True - Mark this operation as manual - StatusCheckStart/Stop will be blocked - only StatusCheck will be working. False - Auto operation :return: """ lProcess = ProcessGet(inProcessNameWOExeStr=inProcessNameWOExeStr) if lProcess is not None: return lProcess.StopSafe(inIsManualBool=inIsManualBool) else: return None def ProcessStopForce(inProcessNameWOExeStr: str, inIsManualBool: bool = True) -> None: """ Manual/Auto stop force. Force stop dont wait process termination - it just terminate process now. Manual stop safe will block scheduling execution. To return schedule execution use def Manual2Auto :param inProcessNameWOExeStr: The process name without extension .exe (the key of the Process instance). Any case - will be processed to the upper case :param inIsManualBool: Default is True - Mark this operation as manual - StatusCheckStart/Stop will be blocked - only StatusCheck will be working. False - Auto operation :return: """ lProcess = ProcessGet(inProcessNameWOExeStr=inProcessNameWOExeStr) if lProcess is not None: return lProcess.StopForce(inIsManualBool=inIsManualBool) else: return None def ProcessStatusCheck(inProcessNameWOExeStr: str) -> str: """ Check if process is alive. The def will save the manual flag is exists. :param inProcessNameWOExeStr: The process name without extension .exe (the key of the Process instance). Any case - will be processed to the upper case :return: str: Check process status and return it """ lProcess = ProcessGet(inProcessNameWOExeStr=inProcessNameWOExeStr) if lProcess is not None: lProcess.StatusCheck() return lProcess.mStatusStr else: return None def ProcessManual2Auto(inProcessNameWOExeStr: str) -> None: """ Remove Manual flag from process (if exists) - it will allow the schedule operations via def StatusCheckStart(self): def StatusCheckStorForce(self): def StatusCheckStopSafe(self): :param inProcessNameWOExeStr: The process name without extension .exe (the key of the Process instance). Any case - will be processed to the upper case :return: None """ lProcess = ProcessGet(inProcessNameWOExeStr=inProcessNameWOExeStr) if lProcess is not None: lProcess.Manual2Auto()