54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
"""
|
|
任务管理模块 (task_manager)
|
|
模块: 服务端 / 任务管理
|
|
输入: init 请求 / 卷到达事件
|
|
输出: transfer 状态机
|
|
|
|
状态机: uploading -> assembling -> decrypting -> done / failed
|
|
transfer_id 生命周期贯穿接收->入库; 孤儿任务由清理逻辑回收 (见 server/data/tmp)。
|
|
"""
|
|
|
|
import uuid
|
|
from typing import Any
|
|
|
|
from db import ServerDB
|
|
|
|
STATUS_UPLOADING = "uploading"
|
|
STATUS_ASSEMBLING = "assembling"
|
|
STATUS_DECRYPTING = "decrypting"
|
|
STATUS_DONE = "done"
|
|
STATUS_FAILED = "failed"
|
|
|
|
|
|
class TaskManager:
|
|
"""transfer 生命周期管理 (基于 ServerDB)"""
|
|
|
|
def __init__(self, db: ServerDB) -> None:
|
|
self.db = db
|
|
|
|
def create(self, init_json: dict[str, Any]) -> str:
|
|
"""POST /init: 建任务, 返回 transfer_id"""
|
|
transfer_id = uuid.uuid4().hex[:12]
|
|
self.db.create_transfer(transfer_id, init_json)
|
|
return transfer_id
|
|
|
|
def get(self, transfer_id: str) -> dict[str, Any] | None:
|
|
return self.db.get_transfer(transfer_id)
|
|
|
|
def chunk_spec(self, transfer_id: str, idx: int) -> dict[str, Any] | None:
|
|
return self.db.get_chunk_spec(transfer_id, idx)
|
|
|
|
def is_chunk_received(self, transfer_id: str, idx: int) -> bool:
|
|
return self.db.is_chunk_received(transfer_id, idx)
|
|
|
|
def mark_chunk_received(self, transfer_id: str, idx: int) -> None:
|
|
"""事件: 卷到达 (校验通过后)"""
|
|
self.db.mark_chunk_received(transfer_id, idx)
|
|
|
|
def received(self, transfer_id: str) -> set[int]:
|
|
"""已收卷集合 (GET /chunks 依据)"""
|
|
return self.db.get_received_chunks(transfer_id)
|
|
|
|
def set_status(self, transfer_id: str, status: str) -> None:
|
|
self.db.set_status(transfer_id, status)
|