Some checks failed
CD Pipeline / deploy (push) Failing after 59s
- 建立 Gitea Actions CD pipeline (.gitea/workflows/cd.yaml) - 部署模式: rsync Python 檔案至 188 → docker restart (volume mount) - Dockerfile/requirements 變動時自動重建 Docker image - 部署通知: Telegram (開始/成功/失敗) - 健康檢查: https://mo.wooo.work/health (最多 5 次重試) - 同步最新 CLAUDE.md / ADR-008 / memory (2026-04-19) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
57 lines
1.8 KiB
Python
57 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
任務執行器服務
|
|
封裝背景任務執行邏輯,避免 app.py 與路由模組的循環依賴
|
|
"""
|
|
|
|
import threading
|
|
from datetime import datetime, timezone, timedelta
|
|
|
|
from services.logger_manager import SystemLogger
|
|
|
|
# 時區設定
|
|
TAIPEI_TZ = timezone(timedelta(hours=8))
|
|
|
|
# Logger
|
|
sys_log = SystemLogger("TaskRunner").get_logger()
|
|
|
|
|
|
def run_momo_task_with_notification():
|
|
"""
|
|
執行 MOMO 爬蟲任務並發送通知
|
|
此函數會在背景執行緒中運行
|
|
"""
|
|
import importlib
|
|
|
|
timestamp = datetime.now(TAIPEI_TZ).strftime('%H:%M:%S')
|
|
sys_log.info(f"[TaskRunner] [{timestamp}] 啟動背景抓取執行緒...")
|
|
|
|
def job():
|
|
try:
|
|
# 1. 執行爬蟲任務
|
|
import scheduler
|
|
importlib.reload(scheduler)
|
|
scheduler.run_momo_task()
|
|
|
|
# 2. 發送通知 (僅發送今日異動)
|
|
try:
|
|
import services.notification_manager
|
|
importlib.reload(services.notification_manager)
|
|
from services.notification_manager import NotificationManager
|
|
from routes.dashboard_routes import get_dashboard_stats
|
|
|
|
stats = get_dashboard_stats()
|
|
|
|
# 只要有任何異動數據就發送通知
|
|
if any(stats.values()):
|
|
screenshot_path = scheduler.capture_page_screenshot("http://127.0.0.1/", "momo_dashboard")
|
|
NotificationManager().send_momo_report(stats, screenshot_path)
|
|
except Exception as e:
|
|
sys_log.error(f"[TaskRunner] 發送通知失敗: {e}")
|
|
|
|
except Exception as e:
|
|
sys_log.error(f"[TaskRunner] 爬蟲任務執行失敗: {e}")
|
|
|
|
threading.Thread(target=job, daemon=True).start()
|