v1.1.48: fix 4 scenario bugs — save_run: scenario_run_id+step_number, run_id before thread (202), TIMEOUT op_data init, RUNNING before API call, GET /run/<id>
This commit is contained in:
+45
-14
@@ -127,25 +127,41 @@ def _create_scenario_run(client_id, stand, user_email, scenario_name, total_step
|
||||
return rid
|
||||
|
||||
|
||||
def run_scenario(client, scenario_name, client_id, stand, user_email, app_version):
|
||||
def _update_scenario_total(scenario_run_id, total_steps):
|
||||
"""Обновить total_steps после того как стало известно точное число шагов."""
|
||||
conn = get_conn()
|
||||
if not conn:
|
||||
return
|
||||
try:
|
||||
cur = conn.cursor()
|
||||
cur.execute("UPDATE scenario_runs SET total_steps = %s WHERE id = %s", (total_steps, scenario_run_id))
|
||||
conn.commit()
|
||||
cur.close()
|
||||
except Exception as e:
|
||||
print(f"[SCENARIO] update_total error: {e}", flush=True)
|
||||
conn.rollback()
|
||||
finally:
|
||||
put_conn(conn)
|
||||
|
||||
|
||||
def run_scenario(client, scenario_name, client_id, stand, user_email, app_version, scenario_run_id):
|
||||
"""Главный исполнитель сценария. Вызывается в фоновом потоке.
|
||||
Возвращает scenario_run_id для поллинга."""
|
||||
scenario_run_id — уже созданная запись (из api_scenario_run)."""
|
||||
config = load_config() or {}
|
||||
scenarios = config.get("scenarios", {})
|
||||
scenario = scenarios.get(scenario_name)
|
||||
if not scenario:
|
||||
raise ValueError(f"Scenario '{scenario_name}' not found")
|
||||
_save_scenario_run(scenario_run_id, "FAIL", 0, 0, f"Scenario '{scenario_name}' not found")
|
||||
return
|
||||
|
||||
steps = scenario.get("steps", [])
|
||||
if not steps:
|
||||
raise ValueError(f"Scenario '{scenario_name}' has no steps")
|
||||
|
||||
total = len(steps)
|
||||
if not steps:
|
||||
_save_scenario_run(scenario_run_id, "FAIL", 0, 0, "No steps")
|
||||
return
|
||||
|
||||
# Создать запись в scenario_runs
|
||||
scenario_run_id = _create_scenario_run(client_id, stand, user_email, scenario_name, total, app_version)
|
||||
if scenario_run_id is None:
|
||||
raise RuntimeError("Failed to create scenario_run record")
|
||||
# Обновить total_steps (стало известно только сейчас)
|
||||
_update_scenario_total(scenario_run_id, total)
|
||||
|
||||
t0 = time.time()
|
||||
instance_map = {} # svc_name → instanceUid (переиспользование внутри сценария)
|
||||
@@ -205,6 +221,18 @@ def run_scenario(client, scenario_name, client_id, stand, user_email, app_versio
|
||||
_send_params_terraform(client, op_uid, resolved_params)
|
||||
client.post(f"/instanceOperations/{op_uid}/run")
|
||||
|
||||
# 5b. Сохранить RUNNING в runs ДО поллинга (шаг не потеряется при ошибке)
|
||||
step_display = display_name if op_name == "create" else instance_uid
|
||||
try:
|
||||
save_run(client_id, stand, user_email,
|
||||
svc_id, "", op_name, svc_op_id,
|
||||
op_uid, instance_uid, step_display,
|
||||
"RUNNING", 0, "",
|
||||
resolved_params, [], app_version,
|
||||
scenario_run_id, step_num)
|
||||
except Exception as e:
|
||||
print(f"[SCENARIO] save_run(RUNNING) failed for step {step_num}: {e}", flush=True)
|
||||
|
||||
# 6. Поллинг завершения
|
||||
step_t0 = time.time()
|
||||
deadline = step_t0 + 1800
|
||||
@@ -212,6 +240,7 @@ def run_scenario(client, scenario_name, client_id, stand, user_email, app_versio
|
||||
step_error = ""
|
||||
step_duration = 0
|
||||
step_stages = []
|
||||
op_data = {} # инициализировать на случай TIMEOUT
|
||||
|
||||
while time.time() < deadline:
|
||||
try:
|
||||
@@ -231,13 +260,15 @@ def run_scenario(client, scenario_name, client_id, stand, user_email, app_versio
|
||||
break
|
||||
time.sleep(5)
|
||||
|
||||
# 7. Сохранить шаг в runs
|
||||
# 7. Сохранить финальный результат шага в runs
|
||||
svc_final = op_data.get("svc", svc_name) if op_data else svc_name
|
||||
try:
|
||||
save_run(client_id, stand, user_email,
|
||||
svc_id, op_data.get("svc", svc_name), op_name, svc_op_id,
|
||||
op_uid, instance_uid, display_name if op_name == "create" else instance_uid,
|
||||
svc_id, svc_final, op_name, svc_op_id,
|
||||
op_uid, instance_uid, step_display,
|
||||
step_status, step_duration, step_error,
|
||||
resolved_params, step_stages, app_version)
|
||||
resolved_params, step_stages, app_version,
|
||||
scenario_run_id, step_num)
|
||||
except Exception as e:
|
||||
print(f"[SCENARIO] save_run failed for step {step_num}: {e}", flush=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user