Skip to content

Celery Channel

lingo.celery.channel

Internal message envelope types for broker communication.

ChannelEnvelope

Bases: BaseModel

Broker-visible envelope split between payload data and runtime metadata.

Source code in lingo/celery/channel.py
60
61
62
63
64
class ChannelEnvelope(BaseModel):
    """Broker-visible envelope split between payload data and runtime metadata."""

    payload: dict[str, Any]
    meta: ChannelMeta

ChannelMeta

Bases: BaseModel

System metadata used by the orchestration runtime.

Source code in lingo/celery/channel.py
45
46
47
48
49
50
51
52
53
54
55
56
57
class ChannelMeta(BaseModel):
    """System metadata used by the orchestration runtime."""

    job_id: str
    root_task: str
    channel: str
    submitted_at: str = Field(default_factory=lambda: datetime.now(timezone.utc).isoformat())
    heartbeat_at: Optional[str] = None
    attempt: int = 0
    source_node: Optional[str] = None
    preferred_worker: Optional[str] = None
    local_required: bool = False
    policy: RestartPolicy = Field(default_factory=RestartPolicy)

RestartPolicy

Bases: BaseModel

High-level retry policy with intuitive defaults.

Source code in lingo/celery/channel.py
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
class RestartPolicy(BaseModel):
    """High-level retry policy with intuitive defaults."""

    max_retries: int = 2
    backoff_seconds: float = 2.0
    exponential_backoff: bool = True
    jitter_seconds: float = 0.0
    retry_on_failure: bool = True
    retry_exceptions: Optional[list[str]] = None

    def should_retry(self, exc: Exception, retries_so_far: int) -> bool:
        """Return whether a failure should be retried under this policy."""
        if not self.retry_on_failure:
            return False
        if retries_so_far >= self.max_retries:
            return False
        if not self.retry_exceptions:
            return True

        exc_name = type(exc).__name__
        return exc_name in set(self.retry_exceptions)

    def countdown_for_retry(self, retries_so_far: int) -> float:
        """Compute countdown (seconds) before the next retry."""
        base = self.backoff_seconds
        if self.exponential_backoff:
            base = self.backoff_seconds * (2 ** max(0, retries_so_far))

        if self.jitter_seconds > 0:
            base += random.uniform(0, self.jitter_seconds)

        return max(0.0, base)

countdown_for_retry(retries_so_far: int) -> float

Compute countdown (seconds) before the next retry.

Source code in lingo/celery/channel.py
33
34
35
36
37
38
39
40
41
42
def countdown_for_retry(self, retries_so_far: int) -> float:
    """Compute countdown (seconds) before the next retry."""
    base = self.backoff_seconds
    if self.exponential_backoff:
        base = self.backoff_seconds * (2 ** max(0, retries_so_far))

    if self.jitter_seconds > 0:
        base += random.uniform(0, self.jitter_seconds)

    return max(0.0, base)

should_retry(exc: Exception, retries_so_far: int) -> bool

Return whether a failure should be retried under this policy.

Source code in lingo/celery/channel.py
21
22
23
24
25
26
27
28
29
30
31
def should_retry(self, exc: Exception, retries_so_far: int) -> bool:
    """Return whether a failure should be retried under this policy."""
    if not self.retry_on_failure:
        return False
    if retries_so_far >= self.max_retries:
        return False
    if not self.retry_exceptions:
        return True

    exc_name = type(exc).__name__
    return exc_name in set(self.retry_exceptions)