98 lines
2.7 KiB
Python
98 lines
2.7 KiB
Python
"""
|
|
Weibo-HotSign Task Scheduler Service
|
|
Celery Beat configuration for scheduled sign-in tasks
|
|
"""
|
|
|
|
import os
|
|
from celery import Celery
|
|
from celery.schedules import crontab
|
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from sqlalchemy import select
|
|
import asyncio
|
|
from datetime import datetime
|
|
|
|
from ..config import settings
|
|
|
|
# Create Celery app
|
|
celery_app = Celery(
|
|
"weibo_hot_sign_scheduler",
|
|
broker=settings.CELERY_BROKER_URL,
|
|
backend=settings.CELERY_RESULT_BACKEND,
|
|
include=["app.tasks.signin_tasks"]
|
|
)
|
|
|
|
# Celery configuration
|
|
celery_app.conf.update(
|
|
task_serializer="json",
|
|
accept_content=["json"],
|
|
result_serializer="json",
|
|
timezone="Asia/Shanghai",
|
|
enable_utc=True,
|
|
beat_schedule_filename="celerybeat-schedule",
|
|
beat_max_loop_interval=5,
|
|
)
|
|
|
|
# Database configuration for task scheduler
|
|
engine = create_async_engine(
|
|
settings.DATABASE_URL,
|
|
echo=settings.DEBUG,
|
|
pool_size=10,
|
|
max_overflow=20
|
|
)
|
|
|
|
AsyncSessionLocal = sessionmaker(
|
|
engine,
|
|
class_=AsyncSession,
|
|
expire_on_commit=False
|
|
)
|
|
|
|
async def get_db():
|
|
"""Get database session for task scheduler"""
|
|
async with AsyncSessionLocal() as session:
|
|
try:
|
|
yield session
|
|
finally:
|
|
await session.close()
|
|
|
|
class TaskSchedulerService:
|
|
"""Service to manage scheduled tasks from database"""
|
|
|
|
def __init__(self):
|
|
self.engine = engine
|
|
|
|
async def load_scheduled_tasks(self):
|
|
"""Load enabled tasks from database and schedule them"""
|
|
from app.models.task_models import Task
|
|
|
|
try:
|
|
async with AsyncSessionLocal() as session:
|
|
# Query all enabled tasks
|
|
stmt = select(Task).where(Task.is_enabled == True)
|
|
result = await session.execute(stmt)
|
|
tasks = result.scalars().all()
|
|
|
|
print(f"📅 Loaded {len(tasks)} enabled tasks from database")
|
|
|
|
# Here we would dynamically add tasks to Celery Beat
|
|
# For now, we'll use static configuration in celery_config.py
|
|
return tasks
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error loading tasks from database: {e}")
|
|
return []
|
|
|
|
# Synchronous wrapper for async function
|
|
def sync_load_tasks():
|
|
"""Synchronous wrapper to load tasks"""
|
|
service = TaskSchedulerService()
|
|
loop = asyncio.new_event_loop()
|
|
asyncio.set_event_loop(loop)
|
|
try:
|
|
return loop.run_until_complete(service.load_scheduled_tasks())
|
|
finally:
|
|
loop.close()
|
|
|
|
# Import task modules to register them
|
|
from app.tasks import signin_tasks
|