aboutsummaryrefslogtreecommitdiff
path: root/.venv/lib/python3.12/site-packages/hatchet_sdk/v2/concurrency.py
blob: 73d9e3b4f38845faf9638be8014298585e1f4270 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from typing import Any, Callable

from hatchet_sdk.context.context import Context
from hatchet_sdk.contracts.workflows_pb2 import (  # type: ignore[attr-defined]
    ConcurrencyLimitStrategy,
)


class ConcurrencyFunction:
    def __init__(
        self,
        func: Callable[[Context], str],
        name: str = "concurrency",
        max_runs: int = 1,
        limit_strategy: ConcurrencyLimitStrategy = ConcurrencyLimitStrategy.GROUP_ROUND_ROBIN,
    ):
        self.func = func
        self.name = name
        self.max_runs = max_runs
        self.limit_strategy = limit_strategy
        self.namespace = "default"

    def set_namespace(self, namespace: str) -> None:
        self.namespace = namespace

    def get_action_name(self) -> str:
        return self.namespace + ":" + self.name

    def __call__(self, *args: Any, **kwargs: Any) -> str:
        return self.func(*args, **kwargs)

    def __str__(self) -> str:
        return f"{self.name}({self.max_runs})"

    def __repr__(self) -> str:
        return f"{self.name}({self.max_runs})"


def concurrency(
    name: str = "",
    max_runs: int = 1,
    limit_strategy: ConcurrencyLimitStrategy = ConcurrencyLimitStrategy.GROUP_ROUND_ROBIN,
) -> Callable[[Callable[[Context], str]], ConcurrencyFunction]:
    def inner(func: Callable[[Context], str]) -> ConcurrencyFunction:
        return ConcurrencyFunction(func, name, max_runs, limit_strategy)

    return inner