cosmo/backend/scripts/update_scheduled_jobs.py

88 lines
2.9 KiB
Python
Raw Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""
Update existing job to use predefined task and add new event sync job
"""
import asyncio
import sys
from pathlib import Path
# Add backend to path
sys.path.insert(0, str(Path(__file__).parent.parent))
from sqlalchemy import text, update
from app.database import engine
from app.models.db.scheduled_job import ScheduledJob, JobType
async def update_jobs():
"""Update existing job and add new event sync job"""
async with engine.begin() as conn:
print("🔄 Updating scheduled jobs...")
# 1. Update existing job to use predefined task
result = await conn.execute(text("""
UPDATE scheduled_jobs
SET
job_type = 'predefined',
predefined_function = 'sync_solar_system_positions',
function_params = '{"days": 7, "source": "nasa_horizons_cron"}'::jsonb,
description = '每日同步太阳系天体位置数据(使用内置任务)'
WHERE id = 1
RETURNING id, name
"""))
updated = result.fetchone()
if updated:
print(f"✅ Updated job ID {updated[0]}: {updated[1]} -> predefined task")
# 2. Add new celestial events sync job (disabled)
result = await conn.execute(text("""
INSERT INTO scheduled_jobs (
name,
job_type,
predefined_function,
function_params,
cron_expression,
description,
is_active
)
VALUES (
'天体事件同步',
'predefined',
'sync_celestial_events',
'{"days_ahead": 30}'::jsonb,
'0 3 * * *',
'每日凌晨3点同步未来30天的天体事件预留功能暂未实现',
false
)
ON CONFLICT DO NOTHING
RETURNING id, name
"""))
new_job = result.fetchone()
if new_job:
print(f"✅ Added new job ID {new_job[0]}: {new_job[1]} (disabled)")
else:
print(" Event sync job already exists")
# 3. Show all jobs
print("\n📋 Current scheduled jobs:")
result = await conn.execute(text("""
SELECT
id,
name,
job_type,
predefined_function,
is_active,
cron_expression
FROM scheduled_jobs
ORDER BY id
"""))
for row in result.fetchall():
status = "🟢 启用" if row[4] else "🔴 禁用"
job_type_display = "内置任务" if row[2] == 'predefined' else "自定义代码"
print(f" {status} ID {row[0]}: {row[1]}")
print(f" 类型: {job_type_display} | 函数: {row[3]} | CRON: {row[5]}")
if __name__ == "__main__":
asyncio.run(update_jobs())