Files
boss_dp/worker/tasks/base.py
2026-02-12 16:27:43 +08:00

52 lines
1.3 KiB
Python
Raw 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.

# -*- coding: utf-8 -*-
"""
任务处理器基类。
所有具体任务(如 BOSS 招聘)都继承此类,实现 execute 方法。
"""
from __future__ import annotations
import logging
from abc import ABC, abstractmethod
from typing import Any, Callable, Coroutine, Dict, Optional
class BaseTaskHandler(ABC):
"""
任务处理器基类。
子类需实现:
execute(task_id, params, progress_callback) -> result
progress_callback 是一个异步函数:
await progress_callback(task_id, "当前进度描述")
"""
# 子类覆盖,声明处理哪个 task_type
task_type: str = ""
def __init__(self) -> None:
self.logger = logging.getLogger(f"worker.tasks.{self.task_type or self.__class__.__name__}")
@abstractmethod
async def execute(
self,
task_id: str,
params: Dict[str, Any],
progress_cb: Callable[[str, str], Coroutine],
) -> Any:
"""
执行任务。
Args:
task_id: 任务 ID
params: 任务参数
progress_cb: 进度上报回调 async def(task_id, progress_text)
Returns:
任务结果(可序列化为 JSON
Raises:
Exception: 执行失败时抛出异常
"""
...