@ -1,6 +1,7 @@
import subprocess , json , psutil , time , os , win32security , sys , base64 , logging , ctypes , copy #Get input argument
import subprocess , json , psutil , time , os , win32security , sys , base64 , logging , ctypes , copy #Get input argument
import pickle
import pickle
import inspect
import inspect
import schedule
from partd import Server
from partd import Server
from . import Server
from . import Server
@ -471,6 +472,38 @@ def OrchestratorLoggerGet():
"""
"""
return GSettingsGet ( ) . get ( " Logger " , None )
return GSettingsGet ( ) . get ( " Logger " , None )
def OrchestratorScheduleGet ( ) :
"""
Get the schedule ( schedule . readthedocs . io ) from the Orchestrator
Fro example you can use :
. . code - block : : python
# One schedule threaded
Orchestrator . OrchestratorScheduleGet ( ) . every ( 5 ) . seconds . do ( lProcess . StatusCheckStart )
#New schedule thread # See def description Orchestrator.OrchestratorThreadStart
Orchestrator . OrchestratorScheduleGet ( ) . every ( 5 ) . seconds . do ( Orchestrator . OrchestratorThreadStart , lProcess . StatusCheckStart )
: return : schedule module . Example see here https : / / schedule . readthedocs . io / en / stable / examples . html
"""
if GSettingsGet ( ) . get ( " SchedulerDict " , { } ) . get ( " Schedule " , None ) is None :
GSettingsGet ( ) [ " SchedulerDict " ] [ " Schedule " ] = schedule
return GSettingsGet ( ) . get ( " SchedulerDict " , { } ) . get ( " Schedule " , None )
def OrchestratorThreadStart ( inDef , * inArgList , * * inArgDict ) :
"""
Execute def in new thread and pass some args with list and dict types
: param inDef : Python Def
: param inArgList : args as list
: param inArgDict : args as dict
: return : threading . Thread object
"""
lDefThread = threading . Thread ( target = inDef , args = inArgList , kwargs = inArgDict )
lDefThread . start ( )
return lDefThread
def OrchestratorIsAdmin ( ) :
def OrchestratorIsAdmin ( ) :
"""
"""
Check if Orchestrator process is running as administrator
Check if Orchestrator process is running as administrator
@ -2562,9 +2595,6 @@ def Orchestrator(inGSettings=None, inDumpRestoreBool = True, inRunAsAdministrato
ActivityItemDefAliasModulesLoad ( )
ActivityItemDefAliasModulesLoad ( )
#Инициализация настроечных параметров
#Инициализация настроечных параметров
lDaemonLoopSeconds = gSettingsDict [ " SchedulerDict " ] [ " CheckIntervalSecFloat " ]
lDaemonActivityLogDict = { } #Словарь отработанных активностей, ключ - кортеж (<activityType>, <datetime>, <processPath || processName>, <processArgs>)
lDaemonLastDateTime = datetime . datetime . now ( )
gSettingsDict [ " ServerDict " ] [ " WorkingDirectoryPathStr " ] = os . getcwd ( ) # Set working directory in g settings
gSettingsDict [ " ServerDict " ] [ " WorkingDirectoryPathStr " ] = os . getcwd ( ) # Set working directory in g settings
#Инициализация сервера (инициализация всех интерфейсов)
#Инициализация сервера (инициализация всех интерфейсов)
@ -2615,69 +2645,97 @@ def Orchestrator(inGSettings=None, inDumpRestoreBool = True, inRunAsAdministrato
lProcessorMonitorThread . start ( ) # Start the thread execution.
lProcessorMonitorThread . start ( ) # Start the thread execution.
if lL : lL . info ( " Processor monitor has been started " ) #Logging
if lL : lL . info ( " Processor monitor has been started " ) #Logging
if lL : lL . info ( " Scheduler loop start " ) #Logging
# Scheduler loop
gDaemonActivityLogDictRefreshSecInt = 10 # The second period for clear lDaemonActivityLogDict from old items
lSchedulerThread = threading . Thread ( target = __deprecated_orchestrator_loop__ )
gDaemonActivityLogDictLastTime = time . time ( ) # The second perioad for clean lDaemonActivityLogDict from old items
lSchedulerThread . daemon = True # Run the thread in daemon mode.
lSchedulerThread . start ( ) # Start the thread execution.
if lL : lL . info ( " Scheduler (old) loop start " ) #Logging
while True :
try :
# Schedule (new) loop
lCurrentDateTime = datetime . datetime . now ( )
lScheduleThread = threading . Thread ( target = __schedule_loop__ )
#Циклический обход правил
lScheduleThread . daemon = True # Run the thread in daemon mode.
lFlagSearchActivityType = True
lScheduleThread . start ( ) # Start the thread execution.
# Periodically clear the lDaemonActivityLogDict
if lL : lL . info ( " Schedule module (new) loop start " ) #Logging
if time . time ( ) - gDaemonActivityLogDictLastTime > = gDaemonActivityLogDictRefreshSecInt :
gDaemonActivityLogDictLastTime = time . time ( ) # Update the time
for lIndex , lItem in enumerate ( lDaemonActivityLogDict ) :
def __schedule_loop__ ( ) :
if lItem [ " ActivityEndDateTime " ] and lCurrentDateTime < = lItem [ " ActivityEndDateTime " ] :
while True :
pass
schedule . run_pending ( )
# Activity is actual - do not delete now
time . sleep ( 3 )
else :
# remove the activity - not actual
# Backward compatibility below to 1.2.7
lDaemonActivityLogDict . pop ( lIndex , None )
def __deprecated_orchestrator_loop__ ( ) :
lIterationLastDateTime = lDaemonLastDateTime # Get current datetime before iterator (need for iterate all activities in loop)
lL = OrchestratorLoggerGet ( )
# Iterate throught the activity list
inGSettings = GSettingsGet ( )
for lIndex , lItem in enumerate ( gSettingsDict [ " SchedulerDict " ] [ " ActivityTimeList " ] ) :
lDaemonLoopSeconds = gSettingsDict [ " SchedulerDict " ] [ " CheckIntervalSecFloat " ]
try :
lDaemonActivityLogDict = { } # Словарь отработанных активностей, ключ - кортеж (<activityType>, <datetime>, <processPath || processName>, <processArgs>)
# Prepare GUID of the activity
lDaemonLastDateTime = datetime . datetime . now ( )
lGUID = None
gDaemonActivityLogDictRefreshSecInt = 10 # The second period for clear lDaemonActivityLogDict from old items
if " GUID " in lItem and lItem [ " GUID " ] :
gDaemonActivityLogDictLastTime = time . time ( ) # The second perioad for clean lDaemonActivityLogDict from old items
lGUID = lItem [ " GUID " ]
while True :
else :
try :
lGUID = str ( uuid . uuid4 ( ) )
lCurrentDateTime = datetime . datetime . now ( )
lItem [ " GUID " ] = lGUID
# Циклический обход правил
lFlagSearchActivityType = True
#Проверка дней недели, в рамках которых можно запускать активность
# Periodically clear the lDaemonActivityLogDict
lItemWeekdayList = lItem . get ( " WeekdayList " , [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] )
if time . time ( ) - gDaemonActivityLogDictLastTime > = gDaemonActivityLogDictRefreshSecInt :
if lCurrentDateTime . weekday ( ) in lItemWeekdayList :
gDaemonActivityLogDictLastTime = time . time ( ) # Update the time
if lFlagSearchActivityType :
for lIndex , lItem in enumerate ( lDaemonActivityLogDict ) :
#######################################################################
if lItem [ " ActivityEndDateTime " ] and lCurrentDateTime < = lItem [ " ActivityEndDateTime " ] :
#Branch 1 - if has TimeHH:MM
pass
#######################################################################
# Activity is actual - do not delete now
if " TimeHH:MMStr " in lItem :
else :
#Вид активности - запуск процесса
# remove the activity - not actual
#Сформировать временной штамп, относительно которого надо будет проверять время
lDaemonActivityLogDict . pop ( lIndex , None )
#часовой пояс пока не учитываем
lIterationLastDateTime = lDaemonLastDateTime # Get current datetime before iterator (need for iterate all activities in loop)
lActivityDateTime = datetime . datetime . strptime ( lItem [ " TimeHH:MMStr " ] , " % H: % M " )
# Iterate throught the activity list
lActivityDateTime = lActivityDateTime . replace ( year = lCurrentDateTime . year , month = lCurrentDateTime . month , day = lCurrentDateTime . day )
for lIndex , lItem in enumerate ( gSettingsDict [ " SchedulerDict " ] [ " ActivityTimeList " ] ) :
#Убедиться в том, что время наступило
try :
if (
# Prepare GUID of the activity
lActivityDateTime > = lDaemonLastDateTime and
lGUID = None
lCurrentDateTime > = lActivityDateTime ) :
if " GUID " in lItem and lItem [ " GUID " ] :
# Log info about activity
lGUID = lItem [ " GUID " ]
if lL : lL . info ( f " Scheduler:: Activity list is started in new thread. Parameters are not available to see. " ) #Logging
else :
# Do the activity
lGUID = str ( uuid . uuid4 ( ) )
lThread = threading . Thread ( target = Processor . ActivityListExecute , kwargs = { " inGSettings " : inGSettings , " inActivityList " : lItem [ " ActivityList " ] } )
lItem [ " GUID " ] = lGUID
lThread . start ( )
lIterationLastDateTime = datetime . datetime . now ( ) # Set the new datetime for the new processor activity
# Проверка дней недели, в рамках которых можно запускать активность
except Exception as e :
lItemWeekdayList = lItem . get ( " WeekdayList " , [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] )
if lL : lL . exception ( f " Scheduler: Exception has been catched in Scheduler module when activity time item was initialising. ActivityTimeItem is { lItem } " )
if lCurrentDateTime . weekday ( ) in lItemWeekdayList :
lDaemonLastDateTime = lIterationLastDateTime # Set the new datetime for the new processor activity
if lFlagSearchActivityType :
#Уснуть до следующего прогона
#######################################################################
time . sleep ( lDaemonLoopSeconds )
# Branch 1 - if has TimeHH:MM
except Exception as e :
#######################################################################
if lL : lL . exception ( f " Scheduler: Exception has been catched in Scheduler module. Global error " )
if " TimeHH:MMStr " in lItem :
# Вид активности - запуск процесса
# Сформировать временной штамп, относительно которого надо будет проверять время
# часовой пояс пока не учитываем
lActivityDateTime = datetime . datetime . strptime ( lItem [ " TimeHH:MMStr " ] , " % H: % M " )
lActivityDateTime = lActivityDateTime . replace ( year = lCurrentDateTime . year ,
month = lCurrentDateTime . month ,
day = lCurrentDateTime . day )
# Убедиться в том, что время наступило
if (
lActivityDateTime > = lDaemonLastDateTime and
lCurrentDateTime > = lActivityDateTime ) :
# Log info about activity
if lL : lL . info (
f " Scheduler:: Activity list is started in new thread. Parameters are not available to see. " ) # Logging
# Do the activity
lThread = threading . Thread ( target = Processor . ActivityListExecute ,
kwargs = { " inGSettings " : inGSettings ,
" inActivityList " : lItem [ " ActivityList " ] } )
lThread . start ( )
lIterationLastDateTime = datetime . datetime . now ( ) # Set the new datetime for the new processor activity
except Exception as e :
if lL : lL . exception (
f " Scheduler: Exception has been catched in Scheduler module when activity time item was initialising. ActivityTimeItem is { lItem } " )
lDaemonLastDateTime = lIterationLastDateTime # Set the new datetime for the new processor activity
# Уснуть до следующего прогона
time . sleep ( lDaemonLoopSeconds )
except Exception as e :
if lL : lL . exception ( f " Scheduler: Exception has been catched in Scheduler module. Global error " )
# Backward compatibility below to 1.2.0
# Backward compatibility below to 1.2.0
def __deprecated_orchestrator_start__ ( ) :
def __deprecated_orchestrator_start__ ( ) :