60 lines
2.2 KiB
Python
60 lines
2.2 KiB
Python
"""
|
|
Scheduled Job ORM model
|
|
"""
|
|
from sqlalchemy import Column, String, Integer, TIMESTAMP, Boolean, Text, Enum, CheckConstraint
|
|
from sqlalchemy.dialects.postgresql import JSONB
|
|
from sqlalchemy.sql import func
|
|
from app.database import Base
|
|
import enum
|
|
|
|
|
|
class JobType(str, enum.Enum):
|
|
"""Job type enumeration"""
|
|
PREDEFINED = "predefined"
|
|
CUSTOM_CODE = "custom_code"
|
|
|
|
|
|
class ScheduledJob(Base):
|
|
"""Scheduled jobs configuration"""
|
|
|
|
__tablename__ = "scheduled_jobs"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
name = Column(String(100), nullable=False, comment="Task name")
|
|
job_type = Column(
|
|
Enum(JobType, values_callable=lambda obj: [e.value for e in obj]),
|
|
nullable=False,
|
|
default=JobType.PREDEFINED,
|
|
comment="Job type: predefined or custom_code"
|
|
)
|
|
predefined_function = Column(
|
|
String(100),
|
|
nullable=True,
|
|
comment="Predefined function name (required if job_type=predefined)"
|
|
)
|
|
function_params = Column(
|
|
JSONB,
|
|
nullable=True,
|
|
default={},
|
|
comment="JSON parameters for predefined function"
|
|
)
|
|
cron_expression = Column(String(50), nullable=False, comment="CRON expression")
|
|
python_code = Column(Text, nullable=True, comment="Dynamic Python code (only for custom_code type)")
|
|
is_active = Column(Boolean, default=True, comment="Active status")
|
|
last_run_at = Column(TIMESTAMP, nullable=True, comment="Last execution time")
|
|
last_run_status = Column(String(20), nullable=True, comment="Last execution status")
|
|
next_run_at = Column(TIMESTAMP, nullable=True, comment="Next scheduled execution time")
|
|
description = Column(Text, nullable=True, comment="Description")
|
|
created_at = Column(TIMESTAMP, server_default=func.now())
|
|
updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now())
|
|
|
|
__table_args__ = (
|
|
CheckConstraint(
|
|
"(job_type = 'predefined' AND predefined_function IS NOT NULL) OR (job_type = 'custom_code' AND python_code IS NOT NULL)",
|
|
name="chk_job_type_fields"
|
|
),
|
|
)
|
|
|
|
def __repr__(self):
|
|
return f"<ScheduledJob(id={self.id}, name='{self.name}', cron='{self.cron_expression}')>"
|