34 lines
1.0 KiB
Python
34 lines
1.0 KiB
Python
import asyncio
|
|
from datetime import datetime
|
|
from app.core.database import async_session
|
|
from app.services.robot_sync_service import robot_sync_service
|
|
|
|
class Scheduler:
|
|
def __init__(self):
|
|
self.is_running = False
|
|
|
|
async def start(self):
|
|
"""启动定时任务"""
|
|
if self.is_running:
|
|
return
|
|
|
|
self.is_running = True
|
|
while self.is_running:
|
|
try:
|
|
# 同步机器人数据
|
|
async with async_session() as session:
|
|
await robot_sync_service.sync_robot_data(session)
|
|
|
|
# 等待5分钟
|
|
await asyncio.sleep(600)
|
|
|
|
except Exception as e:
|
|
print(f"定时任务执行失败: {str(e)}")
|
|
await asyncio.sleep(60) # 发生错误时等待1分钟后重试
|
|
|
|
def stop(self):
|
|
"""停止定时任务"""
|
|
self.is_running = False
|
|
|
|
# 创建单例
|
|
scheduler = Scheduler() |