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()
# Update scheduler
global schedule, tz, last_next_pholus_schedule, last_next_pholus_schedule_used
global tz, mySchedules
tz = timezone(TIMEZONE)
cron = Cron(PHOLUS_RUN_SCHD)
schedule = cron.schedule(start_date=datetime.datetime.now(tz))
pholusSchedule = Cron(PHOLUS_RUN_SCHD).schedule(start_date=datetime.datetime.now(tz))
last_next_pholus_schedule = schedule.next()
last_next_pholus_schedule_used = False
mySchedules = []
mySchedules.append(serviceSchedule("pholus", pholusSchedule, pholusSchedule.next(), False))
# Format and prepare the list of subnets
updateSubnets()
@@ -613,7 +612,7 @@ def main ():
while True:
# update NOW time
# update time started
time_started = datetime.datetime.now()
# re-load user configuration
@@ -656,8 +655,8 @@ def main ():
# run if overdue scheduled time
if (not runPholus) and (PHOLUS_RUN == "schedule"):
# cron_instance.from_string(PHOLUS_RUN_SCHD)
runPholus = runSchedule()
runPholus = [sch for sch in mySchedules if sch.service == "pholus"][0].runScheduleCheck()
# runPholus = mySchedules[0].runScheduleCheck()
if runPholus:
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)
commitDB ()
#-------------------------------------------------------------------------------
def cleanResult(str):
# 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]}'
#-------------------------------------------------------------------------------
def runSchedule():
global last_next_pholus_schedule
global last_pholus_scheduled_run
global last_next_pholus_schedule_used
# 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):
result = False
# Initialize the last run time if never run before
if last_pholus_scheduled_run == 0:
last_pholus_scheduled_run = (datetime.datetime.now(tz) - timedelta(days=365)).replace(microsecond=0)
if self.last_run == 0:
self.last_run = (datetime.datetime.now(tz) - timedelta(days=365)).replace(microsecond=0)
# get the current time with the currently specified timezone
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
# (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:
if nowTime > self.last_next_schedule and self.last_run < self.last_next_schedule:
print_log("Scheduler run: YES")
last_next_pholus_schedule_used = True
self.was_last_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()
if self.was_last_schedule_used:
self.was_last_schedule_used = False
self.last_next_schedule = self.scheduleObject.next()
return result
# print("Hello my name is " + self.scheduleObject)
#===============================================================================
# BEGIN
#===============================================================================