feat(pchome): automate authorized sales acquisition
Some checks failed
CD Pipeline / deploy (push) Has been cancelled
Some checks failed
CD Pipeline / deploy (push) Has been cancelled
This commit is contained in:
60
scheduler.py
60
scheduler.py
@@ -2016,38 +2016,61 @@ def verify_import_data_sync(expected_rows: int = None, date_range: dict = None)
|
||||
|
||||
def run_auto_import_task():
|
||||
"""
|
||||
V-New: 自動從 Google Drive 匯入當日業績
|
||||
每半小時檢查一次 Google Drive 是否有新的 Excel 檔案
|
||||
每半小時從授權來源取得當日業績,執行交易式匯入與獨立驗證。
|
||||
"""
|
||||
# ADR-012 Phase 4: HITL 暫停檢查
|
||||
notification_sent = False
|
||||
try:
|
||||
from services.agent_actions import is_task_paused
|
||||
if is_task_paused("run_auto_import_task"):
|
||||
logging.info("[Scheduler] [AutoImport] ⏸️ 任務被 HITL 暫停中,本次跳過")
|
||||
return
|
||||
logging.warning(
|
||||
"[Scheduler] [AutoImport] legacy HITL pause is superseded for this medium-risk "
|
||||
"controlled-apply lane; continuing with bounded execution"
|
||||
)
|
||||
except Exception as pause_check_error:
|
||||
logging.debug(
|
||||
f"[Scheduler] [AutoImport] HITL 暫停檢查失敗但繼續排程 | Error: {pause_check_error}",
|
||||
f"[Scheduler] [AutoImport] legacy pause readback failed; continuing | Error: {pause_check_error}",
|
||||
exc_info=True,
|
||||
)
|
||||
|
||||
try:
|
||||
from services.import_service import import_service
|
||||
from services.notification_manager import NotificationManager
|
||||
from services.pchome_sales_acquisition_service import pchome_sales_acquisition_service
|
||||
|
||||
logging.info("[Scheduler] [AutoImport] 🚀 啟動 Google Drive 自動匯入任務")
|
||||
logging.info("[Scheduler] [AutoImport] 啟動授權來源自動取得與交易式匯入")
|
||||
|
||||
# 執行自動匯入
|
||||
result = import_service.auto_import_from_drive()
|
||||
result = pchome_sales_acquisition_service.run(trigger="scheduler")
|
||||
run_status = result.get('status')
|
||||
|
||||
if result.get('success'):
|
||||
if run_status in {'blocked_with_safe_next_action', 'degraded_no_write', 'partial'}:
|
||||
logging.warning(
|
||||
"[Scheduler] [AutoImport] 自動取得尚未完整閉環 | status=%s | trace_id=%s | latest=%s | lag=%s",
|
||||
run_status,
|
||||
result.get('trace_id'),
|
||||
result.get('latest_sales_date'),
|
||||
result.get('data_lag_days'),
|
||||
)
|
||||
stats = {
|
||||
"status": "Degraded",
|
||||
"upstream_status": run_status,
|
||||
"trace_id": result.get('trace_id'),
|
||||
"receipt_id": result.get('receipt_id'),
|
||||
"receipt_persisted": result.get('receipt_persisted'),
|
||||
"latest_sales_date": result.get('latest_sales_date'),
|
||||
"data_lag_days": result.get('data_lag_days'),
|
||||
"decision_ready": result.get('decision_ready'),
|
||||
"requires_upstream_acquisition": result.get('requires_upstream_acquisition', True),
|
||||
"safe_next_action": result.get('safe_next_action'),
|
||||
}
|
||||
elif result.get('success'):
|
||||
logging.info(f"[Scheduler] [AutoImport] ✅ 自動匯入完成 | Message: {result.get('message')}")
|
||||
stats = {
|
||||
"file_count": result.get('file_count', 0),
|
||||
"imported_count": result.get('imported_count', 0),
|
||||
"status": "Degraded" if result.get('status') in {'upstream_stale', 'upstream_missing'} else "Success",
|
||||
"upstream_status": result.get('status'),
|
||||
"status": "Success",
|
||||
"upstream_status": run_status,
|
||||
"trace_id": result.get('trace_id'),
|
||||
"receipt_id": result.get('receipt_id'),
|
||||
"receipt_persisted": result.get('receipt_persisted'),
|
||||
"latest_sales_date": result.get('latest_sales_date'),
|
||||
"data_lag_days": result.get('data_lag_days'),
|
||||
"decision_ready": result.get('decision_ready'),
|
||||
@@ -2087,6 +2110,7 @@ def run_auto_import_task():
|
||||
f"📊 當日業績自動匯入通知 ({now_str})\n"
|
||||
f"{'='*30}\n"
|
||||
f"✅ 匯入狀態:成功\n"
|
||||
f"🔎 Trace:{result.get('trace_id', '')[:12]}\n"
|
||||
f"📁 處理檔案數:{result.get('file_count', 0)} 個\n"
|
||||
f"📝 共匯入記錄:{result.get('total_rows', 0)} 筆\n"
|
||||
f"📅 數據日期期間:{date_range_str}\n"
|
||||
@@ -2134,7 +2158,12 @@ def run_auto_import_task():
|
||||
logging.error(f"[Scheduler] [AutoImport] ❌ 自動匯入失敗 | Message: {result.get('message')}")
|
||||
stats = {
|
||||
"status": "Failed",
|
||||
"error": result.get('message', 'Unknown error')
|
||||
"error": result.get('message', 'Unknown error'),
|
||||
"upstream_status": run_status,
|
||||
"trace_id": result.get('trace_id'),
|
||||
"receipt_id": result.get('receipt_id'),
|
||||
"receipt_persisted": result.get('receipt_persisted'),
|
||||
"safe_next_action": result.get('safe_next_action'),
|
||||
}
|
||||
|
||||
# V-New: 匯入失敗時也發送通知
|
||||
@@ -2146,8 +2175,9 @@ def run_auto_import_task():
|
||||
f"{'='*30}\n"
|
||||
f"❌ 匯入狀態:失敗\n"
|
||||
f"📌 錯誤訊息:{result.get('message', 'Unknown error')}\n"
|
||||
f"🔎 Trace:{str(result.get('trace_id') or '')[:12]}\n"
|
||||
f"{'='*30}\n"
|
||||
f"請檢查 Google Drive 設定或手動匯入"
|
||||
f"系統將依安全重試策略再次取得授權來源"
|
||||
)
|
||||
notifier = NotificationManager()
|
||||
notifier._send_line_messages([message])
|
||||
|
||||
Reference in New Issue
Block a user