[pitivi] previewers: Leave autosaving to the caches



commit 7719a00453478b6946501531effd9f592d138df9
Author: Alexandru Băluț <alexandru balut gmail com>
Date:   Sun Dec 10 20:21:45 2017 +0100

    previewers: Leave autosaving to the caches
    
    Leaving the autosave to the ThumbnailCache makes it simpler. It becomes
    a matter of scheduling an autosave when something has been added to the
    cache (and there is none already scheduled).
    
    Reviewed-by: Thibault Saunier <tsaunier gnome org>
    Differential Revision: https://phabricator.freedesktop.org/D1911

 pitivi/timeline/previewers.py |   37 ++++++++++++++++++++++---------------
 1 files changed, 22 insertions(+), 15 deletions(-)
---
diff --git a/pitivi/timeline/previewers.py b/pitivi/timeline/previewers.py
index ede626f..6a5fb03 100644
--- a/pitivi/timeline/previewers.py
+++ b/pitivi/timeline/previewers.py
@@ -535,12 +535,6 @@ class VideoPreviewer(Previewer, Zoomable, Loggable):
 
         self._checkCPU()
 
-        # Save periodically to avoid the common situation where the user exits
-        # the app before a long clip has been fully thumbnailed.
-        # Spread timeouts between 30-80 secs to avoid concurrent disk writes.
-        random_time = random.randrange(30, 80)
-        GLib.timeout_add_seconds(random_time, self._autosave)
-
         # Remove the GSource
         return False
 
@@ -549,7 +543,6 @@ class VideoPreviewer(Previewer, Zoomable, Loggable):
             # nothing left to do
             self.debug("Thumbnails generation complete")
             self.stopGeneration()
-            self.thumb_cache.commit()
             return
         else:
             self.debug("Missing %d thumbs", len(self.wishlist))
@@ -570,14 +563,6 @@ class VideoPreviewer(Previewer, Zoomable, Loggable):
         self._thumb_cb_id = None
         return False
 
-    def _autosave(self):
-        if self.wishlist:
-            self.log("Periodic thumbnail autosave")
-            self.thumb_cache.commit()
-            return True
-        # Stop the timer
-        return False
-
     def _get_thumb_duration(self):
         thumb_duration_tmp = Zoomable.pixelToNs(self.thumb_width + THUMB_MARGIN_PX)
         # quantize thumb length to thumb_period
@@ -772,6 +757,8 @@ class ThumbnailCache(Loggable):
         self._image_size = (None, None)
         # The cached positions available in the database.
         self.positions = self.__existing_positions()
+        # The ID of the autosave event.
+        self.__autosave_id = None
 
     def __existing_positions(self):
         self._cur.execute("SELECT Time FROM Thumbs")
@@ -873,6 +860,26 @@ class ThumbnailCache(Loggable):
         self._cur.execute("DELETE FROM Thumbs WHERE  time=?", (position,))
         self._cur.execute("INSERT INTO Thumbs VALUES (?,?)", (position, blob,))
         self.positions.add(position)
+        self._schedule_commit()
+
+    def _schedule_commit(self):
+        """Schedules an autosave at a random later time."""
+        if self.__autosave_id is not None:
+            # A commit is already scheduled.
+            return
+        # Save after some time, to avoid saving too often.
+        # Randomize to avoid concurrent disk writes.
+        random_time = random.randrange(10, 20)
+        self.__autosave_id = GLib.timeout_add_seconds(random_time, self._autosave_cb)
+
+    def _autosave_cb(self):
+        """Handles the autosave event."""
+        try:
+            self.commit()
+        finally:
+            self.__autosave_id = None
+        # Stop calling me.
+        return False
 
     def commit(self):
         """Saves the cache on disk (in the database)."""


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]