model.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # -*- coding: utf-8 -*-
  2. from sqlalchemy import Boolean, String, Integer, Text, ForeignKey
  3. from sqlalchemy.orm import Mapped, mapped_column, relationship
  4. from app.core.base_model import ModelMixin, UserMixin
  5. class JobModel(ModelMixin, UserMixin):
  6. """
  7. 定时任务调度表
  8. - 0: 运行中
  9. - 1: 暂停中
  10. """
  11. __tablename__: str = 'app_job'
  12. __table_args__: dict[str, str] = ({'comment': '定时任务调度表'})
  13. __loader_options__: list[str] = ["job_logs", "created_by", "updated_by"]
  14. name: Mapped[str | None] = mapped_column(String(64), nullable=True, default='', comment='任务名称')
  15. jobstore: Mapped[str | None] = mapped_column(String(64), nullable=True, default='default', comment='存储器')
  16. executor: Mapped[str | None] = mapped_column(String(64), nullable=True, default='default', comment='执行器:将运行此作业的执行程序的名称')
  17. trigger: Mapped[str] = mapped_column(String(64), nullable=False, comment='触发器:控制此作业计划的 trigger 对象')
  18. trigger_args: Mapped[str | None] = mapped_column(Text, nullable=True, comment='触发器参数')
  19. func: Mapped[str] = mapped_column(Text, nullable=False, comment='任务函数')
  20. args: Mapped[str | None] = mapped_column(Text, nullable=True, comment='位置参数')
  21. kwargs: Mapped[str | None] = mapped_column(Text, nullable=True, comment='关键字参数')
  22. coalesce: Mapped[bool] = mapped_column(Boolean, nullable=True, default=False, comment='是否合并运行:是否在多个运行时间到期时仅运行作业一次')
  23. max_instances: Mapped[int] = mapped_column(Integer, nullable=True, default=1, comment='最大实例数:允许的最大并发执行实例数')
  24. start_date: Mapped[str | None] = mapped_column(String(64), nullable=True, comment='开始时间')
  25. end_date: Mapped[str | None] = mapped_column(String(64), nullable=True, comment='结束时间')
  26. # 关联关系
  27. job_logs: Mapped[list['JobLogModel'] | None] = relationship(
  28. back_populates="job",
  29. lazy="selectin"
  30. )
  31. class JobLogModel(ModelMixin):
  32. """
  33. 定时任务调度日志表
  34. """
  35. __tablename__: str = 'app_job_log'
  36. __table_args__: dict[str, str] = ({'comment': '定时任务调度日志表'})
  37. __loader_options__: list[str] = ["job"]
  38. job_name: Mapped[str] = mapped_column(String(64), nullable=False, comment='任务名称')
  39. job_group: Mapped[str] = mapped_column(String(64), nullable=False, comment='任务组名')
  40. job_executor: Mapped[str] = mapped_column(String(64), nullable=False, comment='任务执行器')
  41. invoke_target: Mapped[str] = mapped_column(String(500), nullable=False, comment='调用目标字符串')
  42. job_args: Mapped[str | None] = mapped_column(String(255), nullable=True, default='', comment='位置参数')
  43. job_kwargs: Mapped[str | None] = mapped_column(String(255), nullable=True, default='', comment='关键字参数')
  44. job_trigger: Mapped[str | None] = mapped_column(String(255), nullable=True, default='', comment='任务触发器')
  45. job_message: Mapped[str | None] = mapped_column(String(500), nullable=True, default='', comment='日志信息')
  46. exception_info: Mapped[str | None] = mapped_column(String(2000), nullable=True, default='', comment='异常信息')
  47. # 任务关联
  48. job_id: Mapped[int | None] = mapped_column(
  49. ForeignKey('app_job.id', ondelete="CASCADE"),
  50. nullable=True,
  51. index=True,
  52. comment='任务ID'
  53. )
  54. job: Mapped["JobModel | None"] = relationship(
  55. back_populates="job_logs",
  56. lazy="selectin"
  57. )