__init__.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. """定时任务模块"""
  2. import logging
  3. from apscheduler.schedulers.background import BackgroundScheduler
  4. logger = logging.getLogger(__name__)
  5. scheduler = BackgroundScheduler()
  6. def init_scheduled_tasks(get_db_session):
  7. """初始化定时任务"""
  8. if scheduler.running:
  9. return
  10. scheduler.start()
  11. logger.info("定时任务调度器已启动")
  12. from app.tasks.customer_pool import (
  13. check_customer_protect_time,
  14. pool_recovery_job,
  15. remind_customer_recycle,
  16. warn_customer_recycle,
  17. )
  18. from app.tasks.db_auto_plan import auto_generate_monthly_plans_job
  19. scheduler.add_job(
  20. check_customer_protect_time,
  21. "cron", hour=1, minute=0,
  22. id="check_customer_protect_time",
  23. replace_existing=True,
  24. )
  25. scheduler.add_job(
  26. auto_generate_monthly_plans_job,
  27. "cron", day=1, hour=1, minute=30,
  28. id="db_auto_generate_monthly_plans",
  29. replace_existing=True,
  30. )
  31. scheduler.add_job(
  32. remind_customer_recycle,
  33. "cron", hour=9, minute=0,
  34. id="remind_customer_recycle",
  35. replace_existing=True,
  36. )
  37. scheduler.add_job(
  38. pool_recovery_job,
  39. "cron", hour=2, minute=0,
  40. id="pool_recovery_job",
  41. replace_existing=True,
  42. )
  43. scheduler.add_job(
  44. warn_customer_recycle,
  45. "cron", hour=10, minute=0,
  46. id="warn_customer_recycle",
  47. replace_existing=True,
  48. )
  49. logger.info(
  50. "已注册定时任务: check_customer_protect_time(01:00), "
  51. "db_auto_generate_monthly_plans(每月1日01:30), "
  52. "pool_recovery_job(02:00), remind_customer_recycle(09:00), "
  53. "warn_customer_recycle(10:00)"
  54. )
  55. def shutdown_scheduler():
  56. if scheduler.running:
  57. scheduler.shutdown(wait=False)
  58. logger.info("定时任务调度器已关闭")
  59. __all__ = [
  60. "scheduler",
  61. "init_scheduled_tasks",
  62. "shutdown_scheduler",
  63. ]