Skip to main content

Task queue abstraction for Spakky Framework (@TaskHandler, @task)

Project description

spakky-task

Spakky Framework를 위한 태스크 큐 추상화 레이어입니다. @task@schedule metadata를 backend plugin이 실행 가능한 task route로 변환하게 합니다.

설치

pip install spakky-task

주요 기능

  • @TaskHandler stereotype: 클래스를 task handler pod로 표시합니다.
  • @task decorator: 메서드를 on-demand dispatch 가능한 task로 표시합니다.
  • @schedule decorator: 메서드를 주기 실행 대상(interval, daily, crontab)으로 표시합니다.
  • Crontab value object: Weekday/Month enum을 사용하는 Python-native cron 명세
  • Post-processor: @TaskHandler pod에서 task route를 자동 스캔하고 등록합니다.
  • 직접 실행: 같은 프로세스에서 task를 실행할 때 기존 request-scope context를 유지합니다.
  • 구현체 중립: 플러그인을 통해 Celery 등 임의의 태스크 큐 백엔드와 동작

사용법

On-demand 태스크

@task는 메서드를 on-demand dispatch 대상으로 표시합니다. 백엔드 플러그인(예: spakky-celery)은 AOP로 호출을 가로채 task queue로 라우팅합니다.

from spakky.task import TaskHandler, task


@TaskHandler()
class EmailTaskHandler:
    @task
    def send_email(self, to: str, subject: str, body: str) -> None:
        """Dispatched to the task queue when called."""
        ...

예약 태스크

@schedule는 메서드를 주기 실행 대상으로 표시합니다. interval, at, crontab 중 정확히 하나를 지정해야 합니다.

from datetime import time, timedelta

from spakky.task import TaskHandler, Crontab, Weekday, schedule


@TaskHandler()
class MaintenanceHandler:
    @schedule(interval=timedelta(minutes=30))
    def health_check(self) -> None:
        """Runs every 30 minutes."""
        ...

    @schedule(at=time(3, 0))
    def daily_cleanup(self) -> None:
        """Runs daily at 03:00."""
        ...

    @schedule(crontab=Crontab(weekday=Weekday.MONDAY, hour=9))
    def weekly_report(self) -> None:
        """Runs every Monday at 09:00."""
        ...

Crontab 명세

Crontab은 cron 문자열 대신 Python-native 타입을 사용합니다. None은 "every"(wildcard)를 의미합니다.

from spakky.task import Crontab, Weekday, Month

# Every Monday at 03:00
Crontab(weekday=Weekday.MONDAY, hour=3)

# Mon/Wed/Fri at 09:00
Crontab(weekday=(Weekday.MONDAY, Weekday.WEDNESDAY, Weekday.FRIDAY), hour=9)

# 1st and 15th of every month at midnight
Crontab(day=(1, 15))

# Every January 1st at midnight
Crontab(month=Month.JANUARY, day=1)

필드 순서(시간 단위가 큰 것부터):

필드 타입 기본값
month Month | tuple[Month, ...] | None None(every)
day int | tuple[int, ...] | None None(every)
weekday Weekday | tuple[Weekday, ...] | None None(every)
hour int 0
minute int 0

Task route 접근

from spakky.task import TaskRegistrationPostProcessor

post_processor = container.get(TaskRegistrationPostProcessor)
routes = post_processor.get_task_routes()
# {<bound method send_email>: TaskRoute(), ...}

인증 metadata와 직접 실행

@taskspakky-auth의 보호 decorator와 함께 사용할 수 있습니다. Task route는 보호 요구사항 metadata를 보존하며, 직접 실행은 현재 ApplicationContext의 request-scope 값을 그대로 사용합니다. 같은 프로세스에서 실행되는 task는 AuthContextSnapshot이 필요하지 않고, 보호된 task도 AuthContext를 메서드 인자로 받을 필요가 없습니다.

from spakky.auth import protected, require_auth_context
from spakky.core.pod.interfaces.application_context import IApplicationContext
from spakky.task import DirectTaskExecutor, DirectTaskInvocation, TaskHandler, task


@TaskHandler()
class ReportTaskHandler:
    def __init__(self, application_context: IApplicationContext) -> None:
        self._application_context = application_context

    @task
    @protected
    def generate(self) -> str:
        return require_auth_context(self._application_context).subject.id


application_context: IApplicationContext = ...
executor = DirectTaskExecutor()
executor.set_application_context(application_context)
result = executor.execute(
    DirectTaskInvocation(
        handler_type=ReportTaskHandler,
        method_name="generate",
    )
)

구성 요소

구성 요소 설명
TaskHandler 태스크 핸들러 클래스용 stereotype decorator
@task on-demand 태스크 dispatch용 메서드 decorator
@schedule 주기 실행용 메서드 decorator(interval, at, crontab)
TaskRoute @task 메서드용 annotation
ScheduleRoute @schedule 메서드용 annotation
Crontab cron 유사 schedule 명세용 frozen dataclass
Weekday 요일용 IntEnum(Monday=0 ... Sunday=6)
Month 월용 IntEnum(January=1 ... December=12)
DirectTaskExecutor request-scope context를 유지하는 같은 프로세스 task 실행기
DirectTaskInvocation 직접 실행 대상 handler/method/argument metadata
TaskRegistrationPostProcessor @TaskHandler pod를 스캔하고 @task 메서드를 수집

에러

에러 설명
TaskNotFoundError registry에서 task reference를 찾을 수 없음
DuplicateTaskError 이미 등록된 task를 다시 등록하려는 경우
InvalidScheduleSpecificationError @schedule called with zero or multiple schedule options

관련 패키지

  • spakky-celery: AOP 기반 task dispatch와 schedule 등록을 위한 Celery backend

라이선스

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spakky_task-6.9.1.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spakky_task-6.9.1-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file spakky_task-6.9.1.tar.gz.

File metadata

  • Download URL: spakky_task-6.9.1.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for spakky_task-6.9.1.tar.gz
Algorithm Hash digest
SHA256 abb987def6036ed41e5c38192535e3750eab076cfbd4aa89a86497066f79bbca
MD5 743aff32bb311ea43f6181bf64f3b79c
BLAKE2b-256 183fd601aa45136bc8906f68f44a5f19d7811f6aad01e0ad5ea0f0b9ca331e9f

See more details on using hashes here.

Provenance

The following attestation bundles were made for spakky_task-6.9.1.tar.gz:

Publisher: release.yml on E5presso/spakky-framework

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spakky_task-6.9.1-py3-none-any.whl.

File metadata

  • Download URL: spakky_task-6.9.1-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for spakky_task-6.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5ad25dd029eb87c815ff388f4bac0c8773d9717a26952b6d5f6933583f4ae158
MD5 67d6d92596797ca5c2b6b12eff12ca5b
BLAKE2b-256 cee55dfb1ec9d0e1888676785f903393bdb2af13db9136cf97722a0c51c52f0b

See more details on using hashes here.

Provenance

The following attestation bundles were made for spakky_task-6.9.1-py3-none-any.whl:

Publisher: release.yml on E5presso/spakky-framework

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page