Skip to content

Commit 011b1ba

Browse files
committed
fix: 修复写入数据库出错不写入日志中
1 parent 887a302 commit 011b1ba

File tree

3 files changed

+60
-56
lines changed

3 files changed

+60
-56
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0.12
1+
3.0.13

core/database.py

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -246,62 +246,65 @@ def _commit_response(hour: int, ip_tables: defaultdict[str, int], user_agents: d
246246
return True
247247

248248
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)
271272

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()
286287

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")
305308

306309
async def init():
307310
Base.metadata.create_all(engine)

i18n/zh_cn.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@
4343
"cluster.info.request_certing": "节点 [${cluster}] 正在请求证书……",
4444
"cluster.info.init": "当前 OpenBMCLAPI 版本为 [${openbmclapi_version}] Python OpenBMCLAPI 版本为 [${version}]",
4545
"cluster.success.no_missing_files": "当前暂无更新的文件",
46-
"cluster.debug.retry_download": "文件 [${file_path} ${file_hash}(${file_size})] 在 [${start_date}] 第 [${count}] 次下载失败,将在 [${time}] 重试"
46+
"cluster.debug.retry_download": "文件 [${file_path} ${file_hash}(${file_size})] 在 [${start_date}] 第 [${count}] 次下载失败,将在 [${time}] 重试",
47+
"database.error.write": "数据库写入出错,原因:"
4748
}

0 commit comments

Comments
 (0)