Files
syops/backend/app/models/job.py
hugoandCursor 39fe3c05f2 feat(freight-rpa): 库存/收料改名在手/入站,报表归属RPA,类名统一FreightRpa
把 shipping 侧 inventory/receipt 词根迁到 onhand/inbound,29 张表与
枚举/权限/前端/i18n 同步改名;在手与入站报表配置及文件改由 freight_rpa
持有,发货与调拨仅按 source_code 消费;ORM 类加 FreightRpa 前缀,
上传动作改为 freight_rpa_upload。含类名与上传动作改名规格文档。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-19 10:46:06 +08:00

163 lines
7.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
backend/app/models/job.py
用途:通用后台任务模型,承载跨业务域的异步任务执行状态。
依赖:models.base.Base;候选集外键指向 shipping_onhand_candidate_sets。
逻辑:由 app.jobs.queue 写入和领取,由 worker 分发到具体业务 handler;业务模型保存业务状态,本表保存执行状态、锁、重试与结果。
"""
from __future__ import annotations
from datetime import datetime
from typing import Optional
from sqlalchemy import DateTime, ForeignKey, Index, Integer, JSON, String, Text, text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.models.base import Base
class Job(Base):
"""
通用后台任务
用途:承载非知识库专属的后台任务队列与执行记录
"""
__tablename__ = 'jobs'
__table_args__ = (
Index('ix_jobs_domain_status_created', 'domain', 'status', 'created_at'),
Index('ix_jobs_type_status_created', 'job_type', 'status', 'created_at'),
Index('ix_jobs_business_ref', 'domain', 'job_type', 'business_ref'),
Index('ix_jobs_locked_at', 'locked_at'),
Index('ix_jobs_retry_of_job_id', 'retry_of_job_id'),
# 仅预解析 Job:同业务锚点同时只允许一条 queued
# (不含 running:retry 可在旧 running 残留时另建 queued;RUN 复查也不受限)
Index(
'uq_jobs_shipping_parse_queued_ref',
'domain',
'job_type',
'business_ref',
unique=True,
postgresql_where=text(
"job_type = 'shipping_notice_parse' "
"AND status = 'queued' "
"AND business_ref IS NOT NULL"
),
sqlite_where=text(
"job_type = 'shipping_notice_parse' "
"AND status = 'queued' "
"AND business_ref IS NOT NULL"
),
),
Index(
'uq_jobs_shipping_order_extraction_active_ref',
'domain',
'job_type',
'business_ref',
unique=True,
postgresql_where=text(
"job_type = 'shipping_order_extraction' "
"AND status IN ('queued', 'running') "
"AND business_ref IS NOT NULL"
),
sqlite_where=text(
"job_type = 'shipping_order_extraction' "
"AND status IN ('queued', 'running') "
"AND business_ref IS NOT NULL"
),
),
Index(
'uq_jobs_shipping_document_layout_active_ref',
'domain',
'job_type',
'business_ref',
unique=True,
postgresql_where=text(
"job_type = 'shipping_document_layout' "
"AND status IN ('queued', 'running') "
"AND business_ref IS NOT NULL"
),
sqlite_where=text(
"job_type = 'shipping_document_layout' "
"AND status IN ('queued', 'running') "
"AND business_ref IS NOT NULL"
),
),
# STP 自动建单:同幂等键同时只允许一条 queued/running(failed 可保留审计并新建重试 Job)
Index(
'uq_jobs_shipping_stp_auto_create_active_ref',
'domain',
'job_type',
'business_ref',
unique=True,
postgresql_where=text(
"job_type = 'shipping_stp_auto_create' "
"AND status IN ('queued', 'running') "
"AND business_ref IS NOT NULL"
),
sqlite_where=text(
"job_type = 'shipping_stp_auto_create' "
"AND status IN ('queued', 'running') "
"AND business_ref IS NOT NULL"
),
),
# Review Revalidate Job:同 Case 同时只允许一条 queued/running(failed 可新建重试 Job)
Index(
'uq_jobs_shipping_review_revalidate_progress_active_ref',
'domain',
'job_type',
'business_ref',
unique=True,
postgresql_where=text(
"domain = 'shipping_notice' "
"AND job_type = 'shipping_review_revalidate_progress' "
"AND status IN ('queued', 'running') "
"AND business_ref IS NOT NULL"
),
sqlite_where=text(
"domain = 'shipping_notice' "
"AND job_type = 'shipping_review_revalidate_progress' "
"AND status IN ('queued', 'running') "
"AND business_ref IS NOT NULL"
),
),
)
id: Mapped[int] = mapped_column(primary_key=True)
domain: Mapped[str] = mapped_column(String(64), index=True)
job_type: Mapped[str] = mapped_column(String(64), index=True)
status: Mapped[str] = mapped_column(String(32), default='queued', index=True)
priority: Mapped[int] = mapped_column(Integer, default=0, index=True)
business_ref: Mapped[Optional[str]] = mapped_column(String(128))
payload_json: Mapped[Optional[dict]] = mapped_column(JSON)
result_json: Mapped[Optional[dict]] = mapped_column(JSON)
progress: Mapped[int] = mapped_column(Integer, default=0)
message: Mapped[Optional[str]] = mapped_column(Text)
error_message: Mapped[Optional[str]] = mapped_column(Text)
attempts: Mapped[int] = mapped_column(Integer, default=0)
max_attempts: Mapped[int] = mapped_column(Integer, default=3)
locked_by: Mapped[Optional[str]] = mapped_column(String(128))
locked_at: Mapped[Optional[datetime]] = mapped_column(DateTime)
# 重试链:新 Job 指向被替换的 failed/dead Job,保留旧行审计。
retry_of_job_id: Mapped[Optional[int]] = mapped_column(ForeignKey('jobs.id'), nullable=True)
# 正式编排作用域。payload 仍保留相同 identity,列用于数据库唯一约束、恢复扫描和审计查询。
task_id: Mapped[Optional[int]] = mapped_column(ForeignKey('shipping_notices.id'), nullable=True, index=True)
execution_id: Mapped[Optional[int]] = mapped_column(
ForeignKey('shipping_process_executions.id'), nullable=True, index=True,
)
gate_id: Mapped[Optional[int]] = mapped_column(
ForeignKey('shipping_process_gates.id'), nullable=True, index=True,
)
candidate_set_id: Mapped[Optional[int]] = mapped_column(
ForeignKey('shipping_onhand_candidate_sets.id'), nullable=True, index=True,
)
fencing_token: Mapped[Optional[str]] = mapped_column(String(64), nullable=True, index=True)
created_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'))
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
started_at: Mapped[Optional[datetime]] = mapped_column(DateTime)
finished_at: Mapped[Optional[datetime]] = mapped_column(DateTime)
created_by: Mapped[Optional['User']] = relationship('User', foreign_keys=[created_by_id])
retry_of_job: Mapped[Optional['Job']] = relationship('Job', remote_side=[id], foreign_keys=[retry_of_job_id])