@@ -81,7 +81,9 @@ async def watch_updates():
8181 try :
8282 updates = json .loads (res )
8383 except Exception :
84- raise Exception (f"Latest updates returned invalid JSON: { res } " )
84+ raise Exception (
85+ f"Latest updates returned invalid JSON: { res } "
86+ )
8587 if index_error := f95zone .check_error (updates , logger ):
8688 raise Exception (index_error )
8789
@@ -94,7 +96,9 @@ async def watch_updates():
9496 for update in updates ["msg" ]["data" ]:
9597 name = cache .NAME_FORMAT .format (id = update ["thread_id" ])
9698 names .append (name )
97- cached_data .hmget (name , "version" , cache .HASHED_META )
99+ cached_data .hmget (
100+ name , "version" , cache .HASHED_META , cache .LAST_CACHED
101+ )
98102 version = update ["version" ]
99103 if version == "Unknown" :
100104 version = None
@@ -114,18 +118,19 @@ async def watch_updates():
114118 cached_data = await cached_data .execute ()
115119
116120 assert len (names ) == len (current_data ) == len (cached_data )
117- for name , (version , meta ), (cached_version , cached_meta ) in zip (
118- names , current_data , cached_data
119- ):
120- if cached_version is None :
121+ for (
122+ name ,
123+ (version , meta ),
124+ (cached_version , cached_meta , last_cached ),
125+ ) in zip (names , current_data , cached_data ):
126+ if cached_version is None or not last_cached :
121127 continue
122128
123129 version_outdated = version and version != cached_version
124130 meta_outdated = meta != cached_meta
125131
126132 if version_outdated or meta_outdated :
127- # Delete version too to avoid watch_versions() picking it up as mismatch
128- invalidate_cache .hdel (name , cache .LAST_CACHED , "version" )
133+ invalidate_cache .hdel (name , cache .LAST_CACHED )
129134 invalidate_cache .hset (name , cache .HASHED_META , meta )
130135 logger .info (
131136 f"Updates: Invalidating cache for { name } "
@@ -175,16 +180,18 @@ async def watch_versions():
175180 async with asyncio .timeout (f95zone .TIMEOUT .total ):
176181 logger .info ("Poll versions start" )
177182
178- names = [n async for n in cache .redis .scan_iter ("thread:*" , 10000 , "hash" )]
183+ names = [
184+ n async for n in cache .redis .scan_iter ("thread:*" , 10000 , "hash" )
185+ ]
179186 invalidate_cache = cache .redis .pipeline ()
180187
181188 for names_chunk in chunks (names , WATCH_VERSIONS_CHUNK_SIZE ):
182189
183- cached_versions = cache .redis .pipeline ()
190+ cached_data = cache .redis .pipeline ()
184191 csv = ""
185192 ids = []
186193 for name in names_chunk :
187- cached_versions . hget (name , "version" )
194+ cached_data . hmget (name , "version" , cache . LAST_CACHED )
188195 id = name .split (":" )[1 ]
189196 csv += f"{ id } ,"
190197 ids .append (id )
@@ -195,8 +202,8 @@ async def watch_versions():
195202 f95zone .VERCHK_URL .format (threads = csv ),
196203 ) as req :
197204 # Await together for efficiency
198- res , cached_versions = await asyncio .gather (
199- req .read (), cached_versions .execute ()
205+ res , cached_data = await asyncio .gather (
206+ req .read (), cached_data .execute ()
200207 )
201208 except Exception as exc :
202209 if index_error := f95zone .check_error (exc , logger ):
@@ -210,24 +217,28 @@ async def watch_versions():
210217 versions = json .loads (res )
211218 except Exception :
212219 raise Exception (f"Versions API returned invalid JSON: { res } " )
213- if versions .get ("msg" ) in ("Missing threads data" , "Thread not found" ):
220+ if versions .get ("msg" ) in (
221+ "Missing threads data" ,
222+ "Thread not found" ,
223+ ):
214224 versions ["status" ] = "ok"
215225 versions ["msg" ] = {}
216226 if index_error := f95zone .check_error (versions , logger ):
217227 raise Exception (index_error )
218228 versions = versions ["msg" ]
219229
220- assert len (names_chunk ) == len (ids ) == len (cached_versions )
221- for name , id , cached_version in zip (names_chunk , ids , cached_versions ):
222- if cached_version is None :
230+ assert len (names_chunk ) == len (ids ) == len (cached_data )
231+ for name , id , (cached_version , last_cached ) in zip (
232+ names_chunk , ids , cached_data
233+ ):
234+ if cached_version is None or not last_cached :
223235 continue
224236 version = versions .get (id )
225237 if not version or version == "Unknown" :
226238 continue
227239
228240 if version != cached_version :
229- # Delete version too to avoid ending up here again
230- invalidate_cache .hdel (name , cache .LAST_CACHED , "version" )
241+ invalidate_cache .hdel (name , cache .LAST_CACHED )
231242 logger .warning (
232243 f"Versions: Invalidating cache for { name } "
233244 f" ({ cached_version !r} -> { version !r} )"
@@ -236,7 +247,9 @@ async def watch_versions():
236247 if len (invalidate_cache ):
237248 result = await invalidate_cache .execute ()
238249 invalidated = sum (ret != "0" for ret in result )
239- logger .warning (f"Versions: Invalidated cache for { invalidated } threads" )
250+ logger .warning (
251+ f"Versions: Invalidated cache for { invalidated } threads"
252+ )
240253
241254 logger .info ("Poll versions done" )
242255
0 commit comments