Rewrite scheduler

This commit is contained in:
Jokob-sk
2023-01-02 11:12:35 +11:00
parent 66e6f9b3f7
commit 856e12bdc7

View File

@@ -557,14 +557,13 @@ def importConfig ():
commitDB() commitDB()
# Update scheduler # Update scheduler
global schedule, tz, last_next_pholus_schedule, last_next_pholus_schedule_used global tz, mySchedules
tz = timezone(TIMEZONE) tz = timezone(TIMEZONE)
cron = Cron(PHOLUS_RUN_SCHD) pholusSchedule = Cron(PHOLUS_RUN_SCHD).schedule(start_date=datetime.datetime.now(tz))
schedule = cron.schedule(start_date=datetime.datetime.now(tz))
last_next_pholus_schedule = schedule.next() mySchedules = []
last_next_pholus_schedule_used = False mySchedules.append(serviceSchedule("pholus", pholusSchedule, pholusSchedule.next(), False))
# Format and prepare the list of subnets # Format and prepare the list of subnets
updateSubnets() updateSubnets()
@@ -613,7 +612,7 @@ def main ():
while True: while True:
# update NOW time # update time started
time_started = datetime.datetime.now() time_started = datetime.datetime.now()
# re-load user configuration # re-load user configuration
@@ -656,8 +655,8 @@ def main ():
# run if overdue scheduled time # run if overdue scheduled time
if (not runPholus) and (PHOLUS_RUN == "schedule"): if (not runPholus) and (PHOLUS_RUN == "schedule"):
# cron_instance.from_string(PHOLUS_RUN_SCHD) runPholus = [sch for sch in mySchedules if sch.service == "pholus"][0].runScheduleCheck()
runPholus = runSchedule() # runPholus = mySchedules[0].runScheduleCheck()
if runPholus: if runPholus:
last_pholus_scheduled_run = datetime.datetime.now(tz).replace(microsecond=0) last_pholus_scheduled_run = datetime.datetime.now(tz).replace(microsecond=0)
@@ -1807,9 +1806,6 @@ def performPholusScan (timeoutSec):
sql.executemany ("""INSERT INTO Pholus_Scan ("Info", "Time", "MAC", "IP_v4_or_v6", "Record_Type", "Value", "Extra") VALUES (?, ?, ?, ?, ?, ?, ?)""", params) sql.executemany ("""INSERT INTO Pholus_Scan ("Info", "Time", "MAC", "IP_v4_or_v6", "Record_Type", "Value", "Extra") VALUES (?, ?, ?, ?, ?, ?, ?)""", params)
commitDB () commitDB ()
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def cleanResult(str): def cleanResult(str):
# alternative str.split('.')[0] # alternative str.split('.')[0]
@@ -3116,17 +3112,22 @@ def hide_email(email):
return f'{m[0][0]}{"*"*(len(m[0])-2)}{m[0][-1] if len(m[0]) > 1 else ""}@{m[1]}' return f'{m[0][0]}{"*"*(len(m[0])-2)}{m[0][-1] if len(m[0]) > 1 else ""}@{m[1]}'
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def runSchedule(): # Cron-like Scheduling
#-------------------------------------------------------------------------------
global last_next_pholus_schedule class serviceSchedule:
global last_pholus_scheduled_run def __init__(self, service, scheduleObject, last_next_schedule, was_last_schedule_used, last_run = 0):
global last_next_pholus_schedule_used self.service = service
self.scheduleObject = scheduleObject
self.last_next_schedule = last_next_schedule
self.last_run = last_run
self.was_last_schedule_used = was_last_schedule_used
def runScheduleCheck(self):
result = False result = False
# Initialize the last run time if never run before # Initialize the last run time if never run before
if last_pholus_scheduled_run == 0: if self.last_run == 0:
last_pholus_scheduled_run = (datetime.datetime.now(tz) - timedelta(days=365)).replace(microsecond=0) self.last_run = (datetime.datetime.now(tz) - timedelta(days=365)).replace(microsecond=0)
# get the current time with the currently specified timezone # get the current time with the currently specified timezone
nowTime = datetime.datetime.now(tz).replace(microsecond=0) nowTime = datetime.datetime.now(tz).replace(microsecond=0)
@@ -3141,19 +3142,21 @@ def runSchedule():
# Run the schedule if the current time is past the schedule time we saved last time and # Run the schedule if the current time is past the schedule time we saved last time and
# (maybe the following check is unnecessary:) # (maybe the following check is unnecessary:)
# if the last run is past the last time we run a scheduled Pholus scan # if the last run is past the last time we run a scheduled Pholus scan
if nowTime > last_next_pholus_schedule and last_pholus_scheduled_run < last_next_pholus_schedule: if nowTime > self.last_next_schedule and self.last_run < self.last_next_schedule:
print_log("Scheduler run: YES") print_log("Scheduler run: YES")
last_next_pholus_schedule_used = True self.was_last_schedule_used = True
result = True result = True
else: else:
print_log("Scheduler run: NO") print_log("Scheduler run: NO")
if last_next_pholus_schedule_used: if self.was_last_schedule_used:
last_next_pholus_schedule_used = False self.was_last_schedule_used = False
last_next_pholus_schedule = schedule.next() self.last_next_schedule = self.scheduleObject.next()
return result return result
# print("Hello my name is " + self.scheduleObject)
#=============================================================================== #===============================================================================
# BEGIN # BEGIN
#=============================================================================== #===============================================================================