add backgroundscheduler init function

This commit is contained in:
shenyafeng 2022-10-21 14:07:51 +08:00
parent 1c940402db
commit 594d05bfe8
1 changed files with 21 additions and 14 deletions

View File

@ -217,19 +217,17 @@ def init():
if not os.path.exists(NOTIFICATION_PIPE):
os.mkfifo(NOTIFICATION_PIPE)
def get_random_time(time_interval):
def get_random_time(stime,random_range):
now = datetime.datetime.now()
delta = random.randint(0,random_range)
actual_time = now + datetime.timedelta(minutes=delta)
try:
start_time = datetime.datetime.strptime(time_interval.split("-")[0],"%H:%M")
end_time = datetime.datetime.strptime(time_interval.split("-")[1],"%H:%M")
now = datetime.datetime.now()
start_time = datetime.datetime.strptime(stime,"%H:%M")
start=datetime.datetime(now.year,now.month,now.day,start_time.hour,start_time.minute,0,0)
end=datetime.datetime(now.year,now.month,now.day,end_time.hour,end_time.minute,0,0)
time_diff = int((end-start).total_seconds())
delta = random.randint(0,time_diff)
actual_time = start+datetime.timedelta(seconds=delta)
return actual_time
actual_time = start+datetime.timedelta(minutes=delta)
except Exception as e:
logging.error(e)
return actual_time
def task(task):
env = copy.copy(os.environ)
@ -251,16 +249,15 @@ def background_scheduler_init(background_scheduler):
background_scheduler.start()
random_time = get_random_time(autoupgradepolicy.GetOptionValue('downloadTime'))
random_time = get_random_time(autoupgradepolicy.GetOptionValue('downloadTime'),DOWNLOAD_RANDOM)
background_scheduler.add_job(task,'cron', args=['download'],id='download', \
hour = random_time.hour,minute = random_time.minute,replace_existing=True)
instime = autoupgradepolicy.GetOptionValue('installTime')
random_time = get_random_time("%s-%s"%(instime,instime))
random_time = random_time + datetime.timedelta(minutes=INTERVAL_DOWN_INSTALL)
background_scheduler.add_job(task,'cron', args=['install'],id='install', \
hour = random_time.hour,minute = random_time.minute,replace_existing=True)
random_time = get_random_time(autoupgradepolicy.GetOptionValue('preDownloadTime'))
random_time = get_random_time(autoupgradepolicy.GetOptionValue('downloadTime'))
background_scheduler.add_job(task,'cron', args=['predownload'],id='predownload', \
hour = random_time.hour,minute = random_time.minute,replace_existing=True)
@ -428,7 +425,17 @@ class AutoUpgradePolicy():
return self.autoupgradepolicy[option]
except Exception:
return ''
def reload_config(self):
if os.path.exists(UNATTENDED_UPGRADE_POLICY_FILE_PATH):
config=configparser.ConfigParser(allow_no_value=True)
config.optionxform = str
config.read(UNATTENDED_UPGRADE_POLICY_FILE_PATH)
for option in config.options('autoUpgradePolicy'):
self.autoupgradepolicy.update({option:config['autoUpgradePolicy'][option]})
for key in self.autoupgradepolicy.keys():
logging.debug("%s:%s"%(key,self.autoupgradepolicy[key]))
class UnattendedUpgradesShutdown():
# 加载配置文件 unattended-upgrades-policy.conf
'''