akg-factor-bridge/plan_recovery.py

150 lines
7.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""自然修订缺当日基础版时,待真实输入齐备后有限补发。
只由 plan-refresh 的缺版分支进入。日期守卫、日更、采集及发布在同一文件锁内;
不调用股票池、模型或评析同步,不续期旧输入,不重跑已有成功基础版。
"""
from __future__ import annotations
import datetime as dt
import json
from pathlib import Path
import build_readiness
import config
import plan_refresh
import plan_versions as pv
def _now():
return dt.datetime.now(dt.timezone.utc)
def _baseline(root, day, now):
"""只认可真实当前决策日的完整首版;既有不合格文件绝不覆盖。"""
latest = pv.latest_date(root)
if latest and latest > day:
return 'blocked_existing_snapshot'
try:
current = pv.load(root, day)
except FileNotFoundError:
return 'missing'
except (ValueError, TypeError):
return 'blocked_existing_snapshot'
try:
if current['date'] != day or current.get('traceability') != 'captured_inputs':
raise ValueError('计划来源不可核验')
pv.envelope(root, current['decision']['id'])
first = pv.load(root, day, morning=True)
cutoff = dt.datetime.fromisoformat(first['decision']['cutoff_at'])
if (cutoff.tzinfo is None or cutoff > now
or cutoff.astimezone(plan_refresh.BJ).date() != now.astimezone(plan_refresh.BJ).date()):
raise ValueError('首版不属于今日')
except (KeyError, TypeError, ValueError, FileNotFoundError):
return 'blocked_existing_snapshot'
return 'already_ready'
def _state(path, local_day, data_day):
if not path.exists():
return {'schema_version': 1, 'decision_date': local_day,
'data_date': data_day, 'attempts': 0}
state = json.loads(path.read_text())
if (not isinstance(state, dict) or state.get('schema_version') != 1
or state.get('decision_date') != local_day or state.get('data_date') != data_day
or type(state.get('attempts')) is not int or state['attempts'] < 0):
raise ValueError('恢复状态不可核验,不能重置重试次数')
if state['attempts']:
last = dt.datetime.fromisoformat(state['last_attempt_at'])
if last.tzinfo is None:
raise ValueError('恢复时刻缺少时区')
return state
def ensure_today(day=None, *, now=None):
"""只接受自然最新日入口;返回可核验状态,失败不伪装成功。"""
clock = (lambda: now) if now is not None else _now
current = clock()
if day is not None:
return {'status': 'explicit_date', 'reason': '指定日期的维护入口不自动重建。'}
if not plan_refresh.in_window(current):
return {'status': 'outside_window'}
expected = build_readiness.expected_data_day(current).isoformat()
root = config.PLAN_SNAPSHOT_DIR
# 有效首版已在时,不因当前来源暂时不可达而重建;后续仍由正常 refresh 修订。
baseline = _baseline(root, expected, current)
if baseline != 'missing':
return {'status': baseline, 'data_date': expected}
try:
ready = build_readiness.check(current)
if ready != expected:
raise RuntimeError('输入日期与当前交易日不一致。')
except RuntimeError as exc:
return {'status': 'waiting_data', 'data_date': expected, 'reason': str(exc)}
with pv.writer(root):
current = clock()
if (not plan_refresh.in_window(current)
or build_readiness.expected_data_day(current).isoformat() != expected):
return {'status': 'outside_window'}
baseline = _baseline(root, expected, current)
if baseline != 'missing':
return {'status': baseline, 'data_date': expected}
local_day = current.astimezone(plan_refresh.BJ).date().isoformat()
path = Path(root) / 'recovery' / (local_day + '.json')
try:
state = _state(path, local_day, expected)
maximum = max(0, min(3, int(config.PLAN_RECOVERY_MAX_ATTEMPTS)))
cooldown = max(900, int(config.PLAN_RECOVERY_COOLDOWN_SECONDS))
if state['attempts'] >= maximum:
return {'status': 'attempts_exhausted', 'attempts': state['attempts'],
'reason': '当天自动构建次数已达上限,保留上次版本。'}
if state['attempts']:
last = dt.datetime.fromisoformat(state['last_attempt_at'])
elapsed = (current - last).total_seconds()
if elapsed < 0:
raise ValueError('恢复时刻晚于当前时刻')
if elapsed < cooldown:
return {'status': 'cooldown', 'attempts': state['attempts'],
'retry_after': (last + dt.timedelta(seconds=cooldown)).isoformat()}
except (ValueError, TypeError, KeyError):
return {'status': 'blocked_state', 'reason': '恢复记录不可核验,未重置次数或重建。'}
# 等锁期间输入可能已变化;未齐只等待,不消耗实际构建次数。
try:
if build_readiness.check(current) != expected:
raise RuntimeError('输入日期与当前交易日不一致。')
except RuntimeError as exc:
return {'status': 'waiting_data', 'data_date': expected, 'reason': str(exc)}
state.update(attempts=state['attempts'] + 1, last_attempt_at=current.isoformat(),
status='building', error=None)
pv.atomic(path, state) # 在实际构建前持久化;进程退出不能退还本次预算。
try:
import run
import plan
built = run._cmd_build('all', 'daily', None, None, None)
current = clock()
if (not plan_refresh.in_window(current)
or build_readiness.expected_data_day(current).isoformat() != expected):
raise RuntimeError('构建完成时已离开允许恢复时段。')
if built != expected or build_readiness.check(current) != expected:
raise RuntimeError('构建完成后输入日期变化,未发布计划。')
def before_publish(data_day):
publishing = clock()
if (data_day != expected or not plan_refresh.in_window(publishing)
or build_readiness.expected_data_day(publishing).isoformat() != expected):
raise RuntimeError('计划采集结束时已离开允许恢复时段或日期不符。')
if build_readiness.check(publishing) != expected:
raise RuntimeError('计划发布前输入日期变化,未发布计划。')
# 持有与 generate 同一文件锁,直接调用内部实现避免递归 flock。
# Bundle 自取实际采集截止时刻;09:30 后首版由 publish 标为 recovery。
plan._generate(expected, 20, 10, 5, before_publish=before_publish)
if _baseline(root, expected, clock()) != 'already_ready':
raise RuntimeError('恢复发布未形成可核验的当前基础版。')
except (Exception, SystemExit) as exc:
state.update(status='failed', completed_at=clock().isoformat(),
error=f'基础版恢复失败({type(exc).__name__}),保留上次已发布版本。')
pv.atomic(path, state)
return {'status': 'failed', 'attempts': state['attempts'], 'reason': state['error']}
state.update(status='recovered', completed_at=clock().isoformat(), error=None)
pv.atomic(path, state)
return {'status': 'recovered', 'data_date': expected, 'attempts': state['attempts']}