fix(import): fail daily sales job on monthly sync failure
All checks were successful
CD Pipeline / deploy (push) Successful in 1m3s
All checks were successful
CD Pipeline / deploy (push) Successful in 1m3s
This commit is contained in:
@@ -60,6 +60,20 @@ def _date_filter_expr(column_name: str) -> str:
|
||||
return f"date({column_name})"
|
||||
|
||||
|
||||
def _table_columns(conn, table_name: str) -> set[str]:
|
||||
"""Return non-id table columns for PostgreSQL and SQLite-backed tests."""
|
||||
if _db_dialect_name() == "postgresql":
|
||||
rows = conn.execute(text("""
|
||||
SELECT column_name FROM information_schema.columns
|
||||
WHERE table_name = :table_name AND column_name != 'id'
|
||||
ORDER BY ordinal_position
|
||||
"""), {"table_name": table_name}).fetchall()
|
||||
return {row[0] for row in rows}
|
||||
|
||||
rows = conn.execute(text(f'PRAGMA table_info("{table_name}")')).fetchall()
|
||||
return {row[1] for row in rows if row[1] != 'id'}
|
||||
|
||||
|
||||
def _normalise_date_values_for_sql(values):
|
||||
"""Keep PostgreSQL params as date objects; use ISO strings for SQLite/text comparisons."""
|
||||
normalised = []
|
||||
@@ -765,13 +779,7 @@ class ImportService:
|
||||
|
||||
# 2026-01-30 新增:驗證 DataFrame 欄位和目標表欄位是否一致
|
||||
with engine.connect() as conn:
|
||||
col_query = text(f"""
|
||||
SELECT column_name FROM information_schema.columns
|
||||
WHERE table_name = '{monthly_table}' AND column_name != 'id'
|
||||
ORDER BY ordinal_position
|
||||
""")
|
||||
result = conn.execute(col_query)
|
||||
target_columns = set([row[0] for row in result])
|
||||
target_columns = _table_columns(conn, monthly_table)
|
||||
|
||||
df_columns = set(df_monthly.columns)
|
||||
missing_in_table = df_columns - target_columns
|
||||
@@ -863,26 +871,6 @@ class ImportService:
|
||||
logger.error(f"任務 {job_id} 發送告警失敗: {notify_error}")
|
||||
|
||||
|
||||
# 更新成功資訊
|
||||
self.update_job_progress(
|
||||
job_id,
|
||||
processed_rows=total_rows,
|
||||
success_rows=total_rows
|
||||
)
|
||||
|
||||
# 2026-01-30 修正:根據同步狀態設置完成訊息
|
||||
if sync_success:
|
||||
completion_msg = '匯入完成(已同步至業績分析儀表板)'
|
||||
else:
|
||||
completion_msg = '匯入完成(警告:業績分析儀表板同步失敗,需手動處理)'
|
||||
|
||||
self.update_job_status(
|
||||
job_id,
|
||||
'completed',
|
||||
100,
|
||||
completion_msg
|
||||
)
|
||||
|
||||
# 計算日期範圍
|
||||
date_min = None
|
||||
date_max = None
|
||||
@@ -923,6 +911,43 @@ class ImportService:
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
if not sync_success:
|
||||
error_msg = sync_error_msg or '同步至業績分析儀表板失敗'
|
||||
self.update_job_progress(
|
||||
job_id,
|
||||
processed_rows=total_rows,
|
||||
success_rows=0,
|
||||
error_rows=total_rows,
|
||||
)
|
||||
self.update_job_status(
|
||||
job_id,
|
||||
'failed',
|
||||
95,
|
||||
'業績分析儀表板同步失敗',
|
||||
error_msg,
|
||||
)
|
||||
logger.error(
|
||||
"任務 %s 匯入未完成:daily_sales_snapshot 已寫入,但 %s 同步失敗;"
|
||||
"保留來源檔案等待重試,不移動 Google Drive 檔案",
|
||||
job_id,
|
||||
monthly_table,
|
||||
)
|
||||
return False
|
||||
|
||||
# 更新成功資訊
|
||||
self.update_job_progress(
|
||||
job_id,
|
||||
processed_rows=total_rows,
|
||||
success_rows=total_rows
|
||||
)
|
||||
|
||||
self.update_job_status(
|
||||
job_id,
|
||||
'completed',
|
||||
100,
|
||||
'匯入完成(已同步至業績分析儀表板)'
|
||||
)
|
||||
|
||||
logger.info(f"任務 {job_id} 匯入成功: {total_rows} 筆")
|
||||
try:
|
||||
from services.cache_service import clear_growth_cache
|
||||
|
||||
Reference in New Issue
Block a user