@@ -246,62 +246,65 @@ def _commit_response(hour: int, ip_tables: defaultdict[str, int], user_agents: d
246
246
return True
247
247
248
248
def commit ():
249
- global FILE_CACHE
250
- total_hits = 0
251
- total_bytes = 0
252
- total_storages = 0
253
- cache = FILE_CACHE .copy ()
254
- response_cache = RESPONSE_CACHE .copy ()
255
- session = SESSION .get_session ()
256
- clusters : defaultdict [tuple [int , str ], FileStatistics ] = defaultdict (lambda : FileStatistics (0 , 0 ))
257
- for key , value in cache .items ():
258
- hour = key .hour
259
- cluster = key .cluster_id
260
- storage = key .storage_id
261
- hits = value .hits
262
- bytes = value .bytes
263
- if _commit_storage (hour , storage , hits , bytes ):
264
- total_hits += hits
265
- total_bytes += bytes
266
- total_storages += 1
267
- clusters [(hour , cluster )].hits += hits
268
- clusters [(hour , cluster )].bytes += bytes
269
- for cluster , value in clusters .items ():
270
- _commit_cluster (cluster [0 ], cluster [1 ], value .hits , value .bytes )
249
+ try :
250
+ global FILE_CACHE
251
+ total_hits = 0
252
+ total_bytes = 0
253
+ total_storages = 0
254
+ cache = FILE_CACHE .copy ()
255
+ response_cache = RESPONSE_CACHE .copy ()
256
+ session = SESSION .get_session ()
257
+ clusters : defaultdict [tuple [int , str ], FileStatistics ] = defaultdict (lambda : FileStatistics (0 , 0 ))
258
+ for key , value in cache .items ():
259
+ hour = key .hour
260
+ cluster = key .cluster_id
261
+ storage = key .storage_id
262
+ hits = value .hits
263
+ bytes = value .bytes
264
+ if _commit_storage (hour , storage , hits , bytes ):
265
+ total_hits += hits
266
+ total_bytes += bytes
267
+ total_storages += 1
268
+ clusters [(hour , cluster )].hits += hits
269
+ clusters [(hour , cluster )].bytes += bytes
270
+ for cluster , value in clusters .items ():
271
+ _commit_cluster (cluster [0 ], cluster [1 ], value .hits , value .bytes )
271
272
272
- for hour , value in response_cache .items ():
273
- _commit_response (hour , value .ip_tables , value .user_agents , value .success , value .forbidden , value .redirect , value .not_found , value .error , value .partial )
274
-
275
- session .commit ()
276
- old_keys = []
277
- for key , value in cache .items ():
278
- FILE_CACHE [key ].hits -= value .hits
279
- FILE_CACHE [key ].bytes -= value .bytes
280
- if FILE_CACHE [key ].hits == FILE_CACHE [key ].bytes == 0 :
281
- old_keys .append (key )
282
- for key in old_keys :
283
- del FILE_CACHE [key ]
284
-
285
- old_keys .clear ()
273
+ for hour , value in response_cache .items ():
274
+ _commit_response (hour , value .ip_tables , value .user_agents , value .success , value .forbidden , value .redirect , value .not_found , value .error , value .partial )
275
+
276
+ session .commit ()
277
+ old_keys = []
278
+ for key , value in cache .items ():
279
+ FILE_CACHE [key ].hits -= value .hits
280
+ FILE_CACHE [key ].bytes -= value .bytes
281
+ if FILE_CACHE [key ].hits == FILE_CACHE [key ].bytes == 0 :
282
+ old_keys .append (key )
283
+ for key in old_keys :
284
+ del FILE_CACHE [key ]
285
+
286
+ old_keys .clear ()
286
287
287
- for hour , value in response_cache .items ():
288
- RESPONSE_CACHE [hour ].success -= value .success
289
- RESPONSE_CACHE [hour ].forbidden -= value .forbidden
290
- RESPONSE_CACHE [hour ].redirect -= value .redirect
291
- RESPONSE_CACHE [hour ].not_found -= value .not_found
292
- RESPONSE_CACHE [hour ].error -= value .error
293
- ip_hits = 0
294
- user_agent_hits = 0
295
- for ip , hits in value .ip_tables .items ():
296
- RESPONSE_CACHE [hour ].ip_tables [ip ] -= hits
297
- ip_hits += RESPONSE_CACHE [hour ].ip_tables [ip ]
298
- for user_agent , hits in value .user_agents .items ():
299
- RESPONSE_CACHE [hour ].user_agents [user_agent ] -= hits
300
- user_agent_hits += RESPONSE_CACHE [hour ].user_agents [user_agent ]
301
- if RESPONSE_CACHE [hour ].success == RESPONSE_CACHE [hour ].forbidden == RESPONSE_CACHE [hour ].redirect == RESPONSE_CACHE [hour ].not_found == RESPONSE_CACHE [hour ].error == ip_hits == user_agent_hits == 0 :
302
- old_keys .append (hour )
303
- for key in old_keys :
304
- del RESPONSE_CACHE [key ]
288
+ for hour , value in response_cache .items ():
289
+ RESPONSE_CACHE [hour ].success -= value .success
290
+ RESPONSE_CACHE [hour ].forbidden -= value .forbidden
291
+ RESPONSE_CACHE [hour ].redirect -= value .redirect
292
+ RESPONSE_CACHE [hour ].not_found -= value .not_found
293
+ RESPONSE_CACHE [hour ].error -= value .error
294
+ ip_hits = 0
295
+ user_agent_hits = 0
296
+ for ip , hits in value .ip_tables .items ():
297
+ RESPONSE_CACHE [hour ].ip_tables [ip ] -= hits
298
+ ip_hits += RESPONSE_CACHE [hour ].ip_tables [ip ]
299
+ for user_agent , hits in value .user_agents .items ():
300
+ RESPONSE_CACHE [hour ].user_agents [user_agent ] -= hits
301
+ user_agent_hits += RESPONSE_CACHE [hour ].user_agents [user_agent ]
302
+ if RESPONSE_CACHE [hour ].success == RESPONSE_CACHE [hour ].forbidden == RESPONSE_CACHE [hour ].redirect == RESPONSE_CACHE [hour ].not_found == RESPONSE_CACHE [hour ].error == ip_hits == user_agent_hits == 0 :
303
+ old_keys .append (hour )
304
+ for key in old_keys :
305
+ del RESPONSE_CACHE [key ]
306
+ except :
307
+ logger .terror ("database.error.write" )
305
308
306
309
async def init ():
307
310
Base .metadata .create_all (engine )
0 commit comments