ソースを参照

Prevent snapshot task from running if the snapshot is already stored

jherve 1 年間 前
コミット
56df202831

+ 14 - 0
src/de_quoi_parle_le_monde/storage.py

@@ -291,6 +291,20 @@ class Storage:
             )
             await conn.commit()
 
+    async def exists_snapshot(self, name: str, dt: datetime):
+        async with self.conn as conn:
+            exists = await conn.execute_fetchall(
+                f"""
+                    SELECT 1
+                    FROM snapshots snap
+                    JOIN sites s ON s.id = snap.site_id
+                    WHERE s.name = ? AND timestamp_virtual = ?
+                """,
+                [name, dt],
+            )
+
+            return exists != []
+
     async def list_all_featured_article_snapshots(self):
         async with self.conn as conn:
             rows = await conn.execute_fetchall(

+ 4 - 0
src/de_quoi_parle_le_monde/workers/snapshot.py

@@ -97,6 +97,10 @@ class SnapshotWorker:
         collection = job.collection
         dt = job.dt
 
+        if await self.storage.exists_snapshot(collection.name, dt):
+            # The snapshot is already stored, skipping
+            return
+
         try:
             logger.info(f"Start handling snap for collection {collection.name} @ {dt}")
             id_closest = await self.find(collection, dt)