From af93f1916b73a8aafb95887e5c7a4031ecfe3d91 Mon Sep 17 00:00:00 2001 From: Rob Weber Date: Tue, 7 Nov 2017 14:35:36 -0600 Subject: [PATCH 1/3] copy/paste from the original pull request --- scheduler.py | 62 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 19 deletions(-) diff --git a/scheduler.py b/scheduler.py index 0b520e6..249ec84 100644 --- a/scheduler.py +++ b/scheduler.py @@ -12,13 +12,29 @@ class BackupScheduler: monitor = None enabled = "false" next_run = 0 + next_run_path = None restore_point = None def __init__(self): self.monitor = UpdateMonitor(update_method = self.settingsChanged) self.enabled = utils.getSetting("enable_scheduler") + self.next_run_path = os.path.join(xbmc.translatePath(utils.data_dir()),'next_run') if(self.enabled == "true"): + try: + fh = open(self.next_run_path, 'r') + fc = fh.read() + fh.close() + nr = float(fc) + except: + nr = 0 + if(0 < nr <= time.time()): + utils.log("scheduled backup was missed, doing it now...") + progress_mode = int(utils.getSetting('progress_mode')) + if(progress_mode == 0): + progress_mode = 1 # Kodi just started, don't block it with a foreground progress bar + self.doScheduledBackup(progress_mode) + utils.log("...completed!") self.setup() def setup(self): @@ -46,26 +62,8 @@ class BackupScheduler: if(self.next_run <= now): progress_mode = int(utils.getSetting('progress_mode')) - if(progress_mode != 2): - utils.showNotification(utils.getString(30053)) - - backup = XbmcBackup() + self.doScheduledBackup(progress_mode) - if(backup.remoteConfigured()): - - if(int(utils.getSetting('progress_mode')) in [0,1]): - backup.run(XbmcBackup.Backup,True) - else: - backup.run(XbmcBackup.Backup,False) - - #check if this is a "one-off" - if(int(utils.getSetting("schedule_interval")) == 0): - #disable the scheduler after this run - self.enabled = "false" - utils.setSetting('enable_scheduler','false') - else: - utils.showNotification(utils.getString(30045)) - #check if we should shut the computer down if(utils.getSetting("cron_shutdown") == 'true'): #wait 10 seconds to make sure all backup processes and files are completed @@ -80,6 +78,27 @@ class BackupScheduler: #delete monitor to free up memory del self.monitor + def doScheduledBackup(self,progress_mode): + if(progress_mode != 2): + utils.showNotification(utils.getString(30053)) + + backup = XbmcBackup() + + if(backup.remoteConfigured()): + + if(int(utils.getSetting('progress_mode')) in [0,1]): + backup.run(XbmcBackup.Backup,True) + else: + backup.run(XbmcBackup.Backup,False) + + #check if this is a "one-off" + if(int(utils.getSetting("schedule_interval")) == 0): + #disable the scheduler after this run + self.enabled = "false" + utils.setSetting('enable_scheduler','false') + else: + utils.showNotification(utils.getString(30045)) + def findNextRun(self,now): progress_mode = int(utils.getSetting('progress_mode')) @@ -92,6 +111,11 @@ class BackupScheduler: if(new_run_time != self.next_run): 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')) + try: + fh = open(self.next_run_path, 'w') + fh.write(str(self.next_run)) + fh.close() + except: pass #only show when not in silent mode if(progress_mode != 2): From 7e01f51e86caaef832ada9bf375fe32f906361e1 Mon Sep 17 00:00:00 2001 From: Rob Weber Date: Tue, 7 Nov 2017 15:03:50 -0600 Subject: [PATCH 2/3] use xbmcvfs for file reads/writes --- scheduler.py | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/scheduler.py b/scheduler.py index 249ec84..276df34 100644 --- a/scheduler.py +++ b/scheduler.py @@ -18,23 +18,32 @@ class BackupScheduler: def __init__(self): self.monitor = UpdateMonitor(update_method = self.settingsChanged) self.enabled = utils.getSetting("enable_scheduler") - self.next_run_path = os.path.join(xbmc.translatePath(utils.data_dir()),'next_run') + self.next_run_path = xbmc.translatePath(utils.data_dir()) + 'next_run.txt' if(self.enabled == "true"): - try: - fh = open(self.next_run_path, 'r') - fc = fh.read() - fh.close() - nr = float(fc) - except: + + if(xbmcvfs.exists(self.next_run_path)): + nr = 0 + fh = xbmcvfs.File(self.next_run_path) + try: + #check if we saved a run time from the last run + nr = float(fh.read()) + except ValueError: + nr = 0 + + fh.close() + + #ADD A CHECK HERE FOR IF THE USER EVEN WANTS THIS if(0 < nr <= time.time()): utils.log("scheduled backup was missed, doing it now...") progress_mode = int(utils.getSetting('progress_mode')) + if(progress_mode == 0): progress_mode = 1 # Kodi just started, don't block it with a foreground progress bar + self.doScheduledBackup(progress_mode) - utils.log("...completed!") + self.setup() def setup(self): @@ -111,11 +120,11 @@ class BackupScheduler: if(new_run_time != self.next_run): 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')) - try: - fh = open(self.next_run_path, 'w') - fh.write(str(self.next_run)) - fh.close() - except: pass + + #write the next time to a file + fh = xbmcvfs.File(self.next_run_path, 'w') + fh.write(str(self.next_run)) + fh.close() #only show when not in silent mode if(progress_mode != 2): From 2af3070528f7272fd2f99b758bd4d588337bdfd3 Mon Sep 17 00:00:00 2001 From: Rob Weber Date: Tue, 7 Nov 2017 15:10:21 -0600 Subject: [PATCH 3/3] allow user to turn on/off --- resources/language/English/strings.xml | 1 + resources/settings.xml | 3 ++- scheduler.py | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/resources/language/English/strings.xml b/resources/language/English/strings.xml index 1ecd886..595d3be 100644 --- a/resources/language/English/strings.xml +++ b/resources/language/English/strings.xml @@ -100,4 +100,5 @@ is authorized error authorizing Visit https://console.developers.google.com/ + Run on startup if missed diff --git a/resources/settings.xml b/resources/settings.xml index 69ffe7f..58c1ca7 100644 --- a/resources/settings.xml +++ b/resources/settings.xml @@ -36,6 +36,7 @@ - + + diff --git a/scheduler.py b/scheduler.py index 276df34..b01b507 100644 --- a/scheduler.py +++ b/scheduler.py @@ -34,8 +34,8 @@ class BackupScheduler: fh.close() - #ADD A CHECK HERE FOR IF THE USER EVEN WANTS THIS - if(0 < nr <= time.time()): + #if we missed and the user wants to play catch-up + if(0 < nr <= time.time() and utils.getSetting('schedule_miss') == 'true'): utils.log("scheduled backup was missed, doing it now...") progress_mode = int(utils.getSetting('progress_mode'))