home · contact · privacy
Release DownloadsManager._db_sync DB connections from waiting for file hash generation. master
authorChristian Heller <c.heller@plomlompom.de>
Fri, 9 May 2025 15:11:01 +0000 (17:11 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Fri, 9 May 2025 15:11:01 +0000 (17:11 +0200)
src/ytplom/misc.py

index a3f337bdc336cd84eff62cb45e31451d36778415..2eed335412180aac2615e1f8e3316b06a33303bb 100644 (file)
@@ -826,28 +826,30 @@ class DownloadsManager:
     def _sync_db(self):
         with DbConn() as conn:
             VideoFile.purge_deleteds(conn)
     def _sync_db(self):
         with DbConn() as conn:
             VideoFile.purge_deleteds(conn)
+            conn.commit()
             known_paths = [file.rel_path for file in VideoFile.get_all(conn)]
             known_paths = [file.rel_path for file in VideoFile.get_all(conn)]
-            old_cwd = Path.cwd()
-            chdir(PATH_DOWNLOADS)
-            for path in [p for p in Path('.').iterdir() if p.is_file()]:
-                yt_id = self._id_from_filename(path)
-                if path not in known_paths:
-                    print(f'SYNC: new file {path}, saving to YT ID "{yt_id}".')
-                    file = VideoFile(digest=None,
-                                     rel_path=path,
-                                     yt_id=yt_id,
-                                     tags_str=VideoFile.tags_default.joined)
+        old_cwd = Path.cwd()
+        chdir(PATH_DOWNLOADS)
+        for path in [p for p in Path('.').iterdir() if p.is_file()]:
+            yt_id = self._id_from_filename(path)
+            if path not in known_paths:
+                print(f'SYNC: new file {path}, saving to YT ID "{yt_id}".')
+                file = VideoFile(digest=None,
+                                 rel_path=path,
+                                 yt_id=yt_id,
+                                 tags_str=VideoFile.tags_default.joined)
+                with DbConn() as conn:
                     file.save(conn)
                     conn.commit()
                     file.save(conn)
                     conn.commit()
+        with DbConn() as conn:
+            for path in [p for p in Path('.').iterdir() if p.is_file()]:
+                yt_id = self._id_from_filename(path)
                 if (yt_id not in self._last_updates
                         or 'present' != self._last_updates[yt_id]['status']):
                 if (yt_id not in self._last_updates
                         or 'present' != self._last_updates[yt_id]['status']):
-                    self._update_status(
-                            yt_id,
-                            'present',
-                            str(path),
-                            VideoFile.get_by_yt_id(conn, yt_id).digest.b64)
+                    digest64 = VideoFile.get_by_yt_id(conn, yt_id).digest.b64
+                    self._update_status(yt_id, 'present', str(path), digest64)
             self._files = VideoFile.get_all(conn)
             self._files = VideoFile.get_all(conn)
-            chdir(old_cwd)
+        chdir(old_cwd)
 
     def last_update_for(self,
                         conn: DbConn,
 
     def last_update_for(self,
                         conn: DbConn,