Files
video-gen/video-gen-api/app/tasks/material_consumption_task.py
T
2026-07-03 10:10:03 +08:00

39 lines
1.5 KiB
Python

from datetime import datetime, timedelta, timezone
import asyncio
from app.services.material_consumption_queue import sync_all_advertisers_consumption
from app.utils.logger import get_logger
logger = get_logger("material_consumption_task", "material_consumption_task")
async def schedule_daily_sync():
"""每天9点自动同步素材消耗数据"""
logger.info("Daily material consumption sync task started")
while True:
try:
now = datetime.now(timezone.utc)
target_time = now.replace(hour=9, minute=0, second=0, microsecond=0)
if now >= target_time:
target_time += timedelta(days=1)
wait_seconds = (target_time - now).total_seconds()
logger.info(f"Waiting {wait_seconds/3600:.1f} hours until next sync at {target_time}")
await asyncio.sleep(wait_seconds)
logger.info("Starting daily material consumption sync...")
try:
result = await sync_all_advertisers_consumption()
logger.info(f"Daily sync completed: {result['message']}")
except Exception as e:
logger.error(f"Daily sync failed: {e}")
except asyncio.CancelledError:
logger.info("Daily sync task cancelled")
break
except Exception as e:
logger.error(f"Error in schedule_daily_sync: {e}")
await asyncio.sleep(60)