fix: 修复定时任务模型字段与数据库表不匹配的问题
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
- task_type -> execution_type - status -> is_enabled - 移除不存在的字段 webhook_method, webhook_headers, script_timeout - time_points/input_params 适配 JSON 类型
This commit is contained in:
@@ -49,7 +49,7 @@ class SchedulerService:
|
||||
"""从数据库加载所有启用的任务"""
|
||||
db = SessionLocal()
|
||||
try:
|
||||
tasks = db.query(ScheduledTask).filter(ScheduledTask.status == 1).all()
|
||||
tasks = db.query(ScheduledTask).filter(ScheduledTask.is_enabled == True).all()
|
||||
for task in tasks:
|
||||
self._add_task_to_scheduler(task)
|
||||
print(f"已加载 {len(tasks)} 个定时任务")
|
||||
@@ -81,7 +81,7 @@ class SchedulerService:
|
||||
elif task.schedule_type == 'simple' and task.time_points:
|
||||
# 简单模式 - 多个时间点
|
||||
try:
|
||||
time_points = json.loads(task.time_points)
|
||||
time_points = task.time_points if isinstance(task.time_points, list) else json.loads(task.time_points)
|
||||
for i, time_point in enumerate(time_points):
|
||||
hour, minute = map(int, time_point.split(':'))
|
||||
sub_job_id = f"{job_id}_{i}"
|
||||
@@ -100,7 +100,7 @@ class SchedulerService:
|
||||
db = SessionLocal()
|
||||
try:
|
||||
task = db.query(ScheduledTask).filter(ScheduledTask.id == task_id).first()
|
||||
if task and task.status == 1:
|
||||
if task and task.is_enabled:
|
||||
self._add_task_to_scheduler(task)
|
||||
finally:
|
||||
db.close()
|
||||
@@ -172,12 +172,9 @@ class SchedulerService:
|
||||
# 解析输入参数
|
||||
params = {}
|
||||
if task.input_params:
|
||||
try:
|
||||
params = json.loads(task.input_params)
|
||||
except:
|
||||
pass
|
||||
params = task.input_params if isinstance(task.input_params, dict) else {}
|
||||
|
||||
if task.task_type == 'webhook':
|
||||
if task.execution_type == 'webhook':
|
||||
success, output, error = await self._execute_webhook(task)
|
||||
else:
|
||||
success, output, error = await self._execute_script(db, task, trace_id, params)
|
||||
@@ -206,20 +203,12 @@ class SchedulerService:
|
||||
async def _execute_webhook(self, task: ScheduledTask):
|
||||
"""执行Webhook任务"""
|
||||
try:
|
||||
headers = {}
|
||||
if task.webhook_headers:
|
||||
headers = json.loads(task.webhook_headers)
|
||||
|
||||
body = {}
|
||||
if task.input_params:
|
||||
body = json.loads(task.input_params)
|
||||
body = task.input_params if isinstance(task.input_params, dict) else {}
|
||||
|
||||
async with httpx.AsyncClient(timeout=30) as client:
|
||||
if task.webhook_method.upper() == 'GET':
|
||||
response = await client.get(task.webhook_url, headers=headers, params=body)
|
||||
else:
|
||||
response = await client.post(task.webhook_url, headers=headers, json=body)
|
||||
|
||||
response = await client.post(task.webhook_url, json=body)
|
||||
response.raise_for_status()
|
||||
return True, response.text[:5000], ''
|
||||
|
||||
@@ -238,7 +227,7 @@ class SchedulerService:
|
||||
tenant_id=task.tenant_id,
|
||||
trace_id=trace_id,
|
||||
params=params,
|
||||
timeout=task.script_timeout or 300
|
||||
timeout=300 # 默认超时
|
||||
)
|
||||
|
||||
return success, output, error
|
||||
@@ -285,14 +274,6 @@ class SchedulerService:
|
||||
if not task:
|
||||
return {"success": False, "error": "任务不存在"}
|
||||
|
||||
# 解析参数
|
||||
params = {}
|
||||
if task.input_params:
|
||||
try:
|
||||
params = json.loads(task.input_params)
|
||||
except:
|
||||
pass
|
||||
|
||||
success, output, error = await self._execute_task_once(db, task)
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user