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))
mySchedules = []
last_next_pholus_schedule = schedule.next() mySchedules.append(serviceSchedule("pholus", pholusSchedule, pholusSchedule.next(), False))
last_next_pholus_schedule_used = 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,43 +3112,50 @@ 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
#-------------------------------------------------------------------------------
class serviceSchedule:
def __init__(self, service, scheduleObject, last_next_schedule, was_last_schedule_used, last_run = 0):
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):
global last_next_pholus_schedule result = False
global last_pholus_scheduled_run
global last_next_pholus_schedule_used
result = False # Initialize the last run time if never run before
if self.last_run == 0:
self.last_run = (datetime.datetime.now(tz) - timedelta(days=365)).replace(microsecond=0)
# Initialize the last run time if never run before # get the current time with the currently specified timezone
if last_pholus_scheduled_run == 0: nowTime = datetime.datetime.now(tz).replace(microsecond=0)
last_pholus_scheduled_run = (datetime.datetime.now(tz) - timedelta(days=365)).replace(microsecond=0)
# get the current time with the currently specified timezone # # DEBUG
nowTime = datetime.datetime.now(tz).replace(microsecond=0) # file_print("now : ", nowTime.isoformat())
# file_print("last_pholus_scheduled_run: ", last_pholus_scheduled_run.isoformat())
# file_print("last_next_pholus_schedule: ", last_next_pholus_schedule.isoformat())
# file_print("nowTime > last_next_pholus_schedule: ", nowTime > last_next_pholus_schedule)
# file_print("last_pholus_scheduled_run < last_next_pholus_schedule: ", last_pholus_scheduled_run < last_next_pholus_schedule)
# # DEBUG # Run the schedule if the current time is past the schedule time we saved last time and
# file_print("now : ", nowTime.isoformat()) # (maybe the following check is unnecessary:)
# file_print("last_pholus_scheduled_run: ", last_pholus_scheduled_run.isoformat()) # if the last run is past the last time we run a scheduled Pholus scan
# file_print("last_next_pholus_schedule: ", last_next_pholus_schedule.isoformat()) if nowTime > self.last_next_schedule and self.last_run < self.last_next_schedule:
# file_print("nowTime > last_next_pholus_schedule: ", nowTime > last_next_pholus_schedule) print_log("Scheduler run: YES")
# file_print("last_pholus_scheduled_run < last_next_pholus_schedule: ", last_pholus_scheduled_run < last_next_pholus_schedule) self.was_last_schedule_used = True
result = True
else:
print_log("Scheduler run: NO")
if self.was_last_schedule_used:
self.was_last_schedule_used = False
self.last_next_schedule = self.scheduleObject.next()
# Run the schedule if the current time is past the schedule time we saved last time and return result
# (maybe the following check is unnecessary:)
# 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:
print_log("Scheduler run: YES")
last_next_pholus_schedule_used = True
result = True
else:
print_log("Scheduler run: NO")
if last_next_pholus_schedule_used:
last_next_pholus_schedule_used = False
last_next_pholus_schedule = schedule.next()
return result # print("Hello my name is " + self.scheduleObject)
#=============================================================================== #===============================================================================
# BEGIN # BEGIN