From 8654c505944542c7143fafb7b46363d659706095 Mon Sep 17 00:00:00 2001 From: Paul Culley Date: Fri, 21 Aug 2020 09:46:18 -0500 Subject: [PATCH] Bug10853m (#1096) * autobackup; add delay after wake from sleep/hibernate to allow time for system to settle. Fixes #10953 * Autobackup only if new commits since last autobackup in session --- gramps/gen/db/generic.py | 2 +- gramps/gui/configure.py | 4 +++- gramps/gui/displaystate.py | 4 ++++ gramps/gui/viewmanager.py | 32 +++++++++++++++++++++++++++++++- gramps/plugins/db/dbapi/dbapi.py | 2 +- 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/gramps/gen/db/generic.py b/gramps/gen/db/generic.py index 4afa34b3b..4dfb013a9 100644 --- a/gramps/gen/db/generic.py +++ b/gramps/gen/db/generic.py @@ -537,7 +537,7 @@ class DbGeneric(DbWriteBase, DbReadBase, UpdateCallback, Callback): self.transaction = None self.abort_possible = False self._bm_changes = 0 - self.has_changed = False + self.has_changed = 0 # Also gives commits since startup self.surname_list = [] self.genderStats = GenderStats() # can pass in loaded stats as dict self.owner = Researcher() diff --git a/gramps/gui/configure.py b/gramps/gui/configure.py index 70877bfb8..87655c7a8 100644 --- a/gramps/gui/configure.py +++ b/gramps/gui/configure.py @@ -1779,7 +1779,9 @@ class GrampsPreferences(ConfigureDialog): formats = [_("Never"), _("Every 15 minutes"), _("Every 30 minutes"), - _("Every hour")] + _("Every hour"), + _("Every 12 hours"), + _("Every day")] list(map(obox.append_text, formats)) active = config.get('database.autobackup') obox.set_active(active) diff --git a/gramps/gui/displaystate.py b/gramps/gui/displaystate.py index 44512aa3b..e3a7ac9b8 100644 --- a/gramps/gui/displaystate.py +++ b/gramps/gui/displaystate.py @@ -458,6 +458,10 @@ class DisplayState(Callback): minutes = 30 elif interval == 3: minutes = 60 + elif interval == 4: + minutes = 720 + elif interval == 5: + minutes = 1440 if interval > 0: self.backup_timer = GLib.timeout_add_seconds( minutes*60, self.__emit_autobackup) diff --git a/gramps/gui/viewmanager.py b/gramps/gui/viewmanager.py index 158c5869b..d298fc11f 100644 --- a/gramps/gui/viewmanager.py +++ b/gramps/gui/viewmanager.py @@ -56,6 +56,7 @@ LOG = logging.getLogger(".") #------------------------------------------------------------------------- from gi.repository import Gtk from gi.repository import Gdk +from gi.repository import GLib #------------------------------------------------------------------------- # @@ -181,6 +182,9 @@ class ViewManager(CLIManager): self.views = None self.current_views = [] # The current view in each category self.view_changing = False + self.autobackup_time = time.time() # time of start or last autobackup + self.delay_timer = None # autobackup delay timer for after wakeup + self.prev_has_changed = 0 # db commit count at autobackup time self.show_navigator = config.get('interface.view') self.show_toolbar = config.get('interface.toolbar-on') @@ -1183,7 +1187,33 @@ class ViewManager(CLIManager): """ Backup the current family tree. """ - if self.dbstate.db.is_open() and self.dbstate.db.has_changed: + if self.delay_timer is not None: + GLib.source_remove(self.delay_timer) + self.delay_timer = None + interval = config.get('database.autobackup') + if interval == 1: + seconds = 900. # 15min *60 + elif interval == 2: + seconds = 1800. # 30min *60 + elif interval == 3: + seconds = 3600. # 60min *60 + elif interval == 4: + seconds = 43200. # (12 hours) 720min *60 + elif interval == 5: + seconds = 86400. # (24 hours) 1440min *60 + now = time.time() + if interval and now > self.autobackup_time + seconds + 300.: + # we have been delayed by more than 5 minutes + # so we have probably been awakened from sleep/hibernate + # we should delay a bit more to let the system settle + self.delay_timer = GLib.timeout_add_seconds(300, self.autobackup) + self.autobackup_time = now + return + self.autobackup_time = now + # Only backup if more commits since last time + if(self.dbstate.db.is_open() and + self.dbstate.db.has_changed > self.prev_has_changed): + self.prev_has_changed = self.dbstate.db.has_changed self.uistate.set_busy_cursor(True) self.uistate.progress.show() self.uistate.push_message(self.dbstate, _("Autobackup...")) diff --git a/gramps/plugins/db/dbapi/dbapi.py b/gramps/plugins/db/dbapi/dbapi.py index f7a75ce49..6debedf04 100644 --- a/gramps/plugins/db/dbapi/dbapi.py +++ b/gramps/plugins/db/dbapi/dbapi.py @@ -276,7 +276,7 @@ class DBAPI(DbGeneric): self.undodb.commit(txn, msg) self._after_commit(txn) txn.clear() - self.has_changed = True + self.has_changed += 1 # Also gives commits since startup def transaction_abort(self, txn): """