fix(costreview): 历史清单定额展开支持重导后按批次+哈希重定位,并后台预升级过期批次

This commit is contained in:
lianjianwei2024
2026-11-15 10:26:48 +08:00
parent 0e5785f1dc
commit 4b54ee1cfa
5 changed files with 95 additions and 13 deletions

View File

@@ -458,7 +458,19 @@ def api_adopt_list(sid=0):
finally:
c.close()
_UPGRADE_LOCK = threading.Lock() # 决策 341:采纳内升级与后台预升级互斥——同批次绝不并发重导
def _upgrade_batch_row(hc, row):
"""决策 341 薄壳:过期判断(快速路径不占锁)→ 持锁重导。采纳内升级与后台预升级共用,
绝不并发重导同一批。"""
if row["kind"] != "project" or row["x_parser_ver"] == job.PARSER_VER:
return None
with _UPGRADE_LOCK:
return _upgrade_batch_row_locked(hc, row)
def _upgrade_batch_row_locked(hc, row):
"""决策 34 的核心:把一个旧版批次行用**当前解析器**原位重导(row 需含 batches 全列)。
原位升级口径(区别于手动重导的「删旧插新」——绑定不能丢):
@@ -471,8 +483,6 @@ def _upgrade_batch_row(hc, row):
护栏:源文件缺失/内容哈希不符/加密/解析失败 → 保留旧数据只打日志,**绝不影响调用方**。
返回 None(没升级)或 {"dropped_binds": n};异常由调用方兜(本函数自己 rollback 后转 None)。
"""
if row["kind"] != "project" or row["x_parser_ver"] == job.PARSER_VER:
return None
nm = row["x_src_hash"]
path = _src_path(nm) if nm else None
if not path:
@@ -578,6 +588,53 @@ def _lme_lib_map(con, T, oid):
return {r["code"]: [r["name"], r["spec"], r["unit"], r["kind"], r["ntp"], r["taxp"]]
for r in rows if r["code"]}
def upgrade_stale_batches_once():
"""决策 341(2026-11-15 主人拍板「修下」):把解析器过期的工程批次**逐批预升级**一遍——
把 279 按需重导的一次性成本从「采纳时卡几分钟」集中到启动后台消化。只升 kind='project';
每批独立兜错(失败不挡后续);与采纳内升级共用 _upgrade_batch_row(内置互斥锁,绝不并发重导)。
返回 (已升级数, 跳过数, 总数)。测试可同步直调;服务启动走 upgrade_stale_batches_bg()。"""
hc = conn()
try:
rows = hc.execute("""SELECT * FROM batches WHERE kind='project'
AND COALESCE(x_parser_ver,'')<>? ORDER BY id""",
(job.PARSER_VER,)).fetchall()
finally:
hc.close()
up = skip = 0
for i, row in enumerate(rows):
try:
hc2 = conn()
try:
r = _upgrade_batch_row(hc2, row)
finally:
hc2.close()
if r:
up += 1
else:
skip += 1
print("279 预升级 %d/%d:批次 #%s「%s」%s"
% (i + 1, len(rows), row["id"], (row["Name"] or "")[:20],
"已升级" if r else "跳过(源件缺失/哈希不符/不可解析)"), flush=True)
except Exception as _e:
skip += 1
print("279 预升级批次 #%s 失败(不挡后续):%s" % (row["id"], _e), flush=True)
return (up, skip, len(rows))
def upgrade_stale_batches_bg():
"""决策 341:启动后台线程跑 upgrade_stale_batches_once()——先让服务就绪(3s)再动库。"""
def _bg():
time.sleep(3)
try:
up, skip, total = upgrade_stale_batches_once()
if total:
print("279 后台预升级完成:升级 %d / 跳过 %d / 共 %d 批" % (up, skip, total), flush=True)
except Exception as _e:
print("279 预升级线程异常(不挡服务):%s" % _e, flush=True)
threading.Thread(target=_bg, name="upgrade-stale-batches", daemon=True).start()
def _norm_res(con, T, norm_id):
"""一条定额下挂的工料机(**两侧唯一实现**):定额 → 消耗量 → 工料机。