mirror of
https://github.com/robweber/xbmcbackup.git
synced 2024-11-14 20:35:48 +01:00
moved settings changed to Monitor class
use xbmc.sleep instead
This commit is contained in:
parent
7652a90c1a
commit
e63ba10473
59
scheduler.py
59
scheduler.py
@ -7,11 +7,12 @@ from resources.lib.croniter import croniter
|
||||
from resources.lib.backup import XbmcBackup
|
||||
|
||||
class BackupScheduler:
|
||||
monitor = None
|
||||
enabled = "false"
|
||||
next_run = 0
|
||||
settings_update_time = 0
|
||||
|
||||
def __init__(self):
|
||||
self.monitor = UpdateMonitor(update_method = self.settingsChanged)
|
||||
self.enabled = utils.getSetting("enable_scheduler")
|
||||
|
||||
if(self.enabled == "true"):
|
||||
@ -20,21 +21,13 @@ class BackupScheduler:
|
||||
def setup(self):
|
||||
#scheduler was turned on, find next run time
|
||||
utils.log("scheduler enabled, finding next run time")
|
||||
self.findNextRun(time.time(),True)
|
||||
self.findNextRun(time.time())
|
||||
utils.log("scheduler will run again on " + datetime.datetime.fromtimestamp(self.next_run).strftime('%m-%d-%Y %H:%M'))
|
||||
|
||||
def start(self):
|
||||
while(not xbmc.abortRequested):
|
||||
current_enabled = utils.getSetting("enable_scheduler")
|
||||
|
||||
if(current_enabled == "true" and self.enabled == "false"):
|
||||
#scheduler was just turned on
|
||||
self.enabled = current_enabled
|
||||
self.setup()
|
||||
elif (current_enabled == "false" and self.enabled == "true"):
|
||||
#schedule was turn off
|
||||
self.enabled = current_enabled
|
||||
elif(self.enabled == "true"):
|
||||
if(self.enabled == "true"):
|
||||
#scheduler is still on
|
||||
now = time.time()
|
||||
|
||||
@ -52,26 +45,11 @@ class BackupScheduler:
|
||||
xbmc.executebuiltin('ShutDown()')
|
||||
else:
|
||||
#find the next run time like normal
|
||||
self.findNextRun(now,True)
|
||||
else:
|
||||
self.findNextRun(now)
|
||||
|
||||
time.sleep(10)
|
||||
|
||||
def findNextRun(self,now,forceUpdate = False):
|
||||
mod_time = self.settings_update_time
|
||||
|
||||
#check if the schedule has been modified
|
||||
try:
|
||||
#get the last modified time of the file
|
||||
mod_time = os.path.getmtime(xbmc.translatePath(utils.data_dir()) + "settings.xml")
|
||||
except:
|
||||
#don't do anything here
|
||||
mod_time = self.settings_update_time
|
||||
|
||||
if(mod_time > self.settings_update_time or forceUpdate):
|
||||
self.settings_update_time = mod_time
|
||||
xbmc.sleep(500)
|
||||
|
||||
def findNextRun(self,now):
|
||||
#find the cron expression and get the next run time
|
||||
cron_exp = self.parseSchedule()
|
||||
|
||||
@ -82,6 +60,21 @@ class BackupScheduler:
|
||||
self.next_run = new_run_time
|
||||
utils.log("scheduler will run again on " + datetime.datetime.fromtimestamp(self.next_run).strftime('%m-%d-%Y %H:%M'))
|
||||
|
||||
def settingsChanged(self):
|
||||
current_enabled = utils.getSetting("enable_scheduler")
|
||||
|
||||
if(current_enabled == "true" and self.enabled == "false"):
|
||||
#scheduler was just turned on
|
||||
self.enabled = current_enabled
|
||||
self.setup()
|
||||
elif (current_enabled == "false" and self.enabled == "true"):
|
||||
#schedule was turn off
|
||||
self.enabled = current_enabled
|
||||
|
||||
if(self.enabled == "true"):
|
||||
#always recheck the next run time after an update
|
||||
self.findNextRun(time.time())
|
||||
|
||||
def parseSchedule(self):
|
||||
schedule_type = int(utils.getSetting("schedule_interval"))
|
||||
cron_exp = utils.getSetting("cron_schedule")
|
||||
@ -101,4 +94,14 @@ class BackupScheduler:
|
||||
|
||||
return cron_exp
|
||||
|
||||
class UpdateMonitor(xbmc.Monitor):
|
||||
update_method = None
|
||||
|
||||
def __init__(self,*args, **kwargs):
|
||||
xbmc.Monitor.__init__(self)
|
||||
self.update_method = kwargs['update_method']
|
||||
|
||||
def onSettingsChanged(self):
|
||||
self.update_method()
|
||||
|
||||
BackupScheduler().start()
|
||||
|
Loading…
Reference in New Issue
Block a user