acemq_amqp.ack

What a handler says about a message, and what the consumer then did.

Two different statements, and the difference is the point of Settlement. A handler saying retry() is a request; whether there is an attempt left to grant it is the retry policy's answer, and a message that asked to be retried once too often is dead-lettered. Anything watching a delivery — the tracing adapter is the one that matters — needs the answer rather than the request, which is why the consumer reports it separately.

  1# Copyright 2026 AceMQ.
  2#
  3# Licensed under the Apache License, Version 2.0 (the "License");
  4# you may not use this file except in compliance with the License.
  5# You may obtain a copy of the License at
  6#
  7#     https://www.apache.org/licenses/LICENSE-2.0
  8#
  9# Unless required by applicable law or agreed to in writing, software
 10# distributed under the License is distributed on an "AS IS" BASIS,
 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12# See the License for the specific language governing permissions and
 13# limitations under the License.
 14
 15"""What a handler says about a message, and what the consumer then did.
 16
 17Two different statements, and the difference is the point of
 18:class:`Settlement`. A handler saying :func:`retry` is a request; whether there
 19is an attempt left to grant it is the retry policy's answer, and a message that
 20asked to be retried once too often is dead-lettered. Anything watching a
 21delivery — the tracing adapter is the one that matters — needs the answer rather
 22than the request, which is why the consumer reports it separately.
 23"""
 24
 25from __future__ import annotations
 26
 27from dataclasses import dataclass
 28from datetime import timedelta
 29from enum import Enum
 30
 31#: The consumer acknowledged the message; the handler was happy with it.
 32OUTCOME_ACKED = "acked"
 33
 34#: The message goes round again, after :attr:`Settlement.delay`.
 35OUTCOME_RETRIED = "retried"
 36
 37#: The handler refused it outright. It is dead-lettered, and the word says the
 38#: decision was deliberate rather than the end of a losing streak.
 39OUTCOME_REJECTED = "rejected"
 40
 41#: It ran out of attempts, aged out, or was unprocessable.
 42OUTCOME_DEAD_LETTERED = "dead_lettered"
 43
 44#: It went to ``{queue}.parked`` rather than to the dead letters, because
 45#: nothing could read it. The same word the ``parked`` outcome on
 46#: ``acemq.messages.dead.lettered.total`` uses, and the word the engine already
 47#: used for a body that would not decode.
 48OUTCOME_PARKED = "parked"
 49
 50#: Somebody else holds a live claim on the message and has not finished, so it
 51#: was put back to be looked at again later — without spending an attempt, and
 52#: never dead-lettered for it. See :func:`in_progress`.
 53OUTCOME_IN_PROGRESS = "in_progress"
 54
 55#: A stream reader stopped at this message because its handler failed: the
 56#: subscription is closed, nothing after it is handled, and no copy of it is
 57#: written anywhere. See :func:`acemq_amqp.patterns.read_stream`.
 58OUTCOME_STOPPED = "stopped"
 59
 60#: How long a message found in progress waits before it is looked at again.
 61DEFAULT_IN_PROGRESS_DELAY = timedelta(seconds=5)
 62
 63
 64class Action(Enum):
 65    """The things that can be done with a delivered message."""
 66
 67    ACCEPT = "accept"
 68    RETRY = "retry"
 69    REJECT = "reject"
 70    PARK = "park"
 71    IN_PROGRESS = "in_progress"
 72
 73
 74@dataclass(frozen=True, slots=True)
 75class Ack:
 76    """A handler's decision: it worked, try it again, never again, or nobody
 77    can read it.
 78
 79    Returned rather than performed, so a handler that forgets to decide is a
 80    handler that returns ``None`` and is caught immediately, rather than one
 81    whose message sits unacknowledged until the connection drops and then comes
 82    back — usually to the same handler, with the same outcome.
 83    """
 84
 85    action: Action
 86    error: BaseException | None = None
 87    #: How long to wait before the message is looked at again. Read for
 88    #: :attr:`Action.IN_PROGRESS` only; a retry's wait is the policy's.
 89    delay: timedelta | None = None
 90
 91    def __str__(self) -> str:
 92        return self.action.value
 93
 94
 95@dataclass(frozen=True, slots=True)
 96class Settlement:
 97    """What the consumer did with a delivery, once it had decided.
 98
 99    Reported before it is carried out rather than after, because one of them
100    takes time: a consumer-side retry sleeps out the backoff before the message
101    is republished, and a listener told afterwards would be describing the
102    delivery minutes later, on a span that had been open the whole while.
103
104    :param outcome: one of :data:`OUTCOME_ACKED`, :data:`OUTCOME_RETRIED`,
105        :data:`OUTCOME_REJECTED`, :data:`OUTCOME_DEAD_LETTERED` and
106        :data:`OUTCOME_PARKED` — the same words the other libraries put on a
107        span
108    :param reason: why it was set aside, when it was
109    :param delay: how long until the next attempt, when there is one
110    """
111
112    outcome: str
113    reason: str | None = None
114    delay: timedelta | None = None
115
116    @property
117    def dead_lettered(self) -> bool:
118        """Whether the message went to the dead-letter queue.
119
120        True for an outright rejection as well as for an exhausted one: both
121        end up in the same queue, and the difference between the two words is
122        *who decided*, which :attr:`outcome` records and this does not.
123
124        False for :data:`OUTCOME_PARKED`, and that is the whole point of the
125        parked queue: a message nobody could read is a different problem with a
126        different answer from one that failed five times, and whoever drains
127        the dead letters should not have to sort the two apart by hand.
128        """
129        return self.outcome in (OUTCOME_REJECTED, OUTCOME_DEAD_LETTERED)
130
131    @property
132    def parked(self) -> bool:
133        """Whether the message went to the parked queue."""
134        return self.outcome == OUTCOME_PARKED
135
136    def __str__(self) -> str:
137        return self.outcome
138
139
140def accept() -> Ack:
141    """Confirms the message. It will not be delivered again."""
142    return Ack(Action.ACCEPT)
143
144
145def retry(error: BaseException | None = None) -> Ack:
146    """Returns the message to be tried again.
147
148    The retry policy decides whether there is another attempt left; when there
149    is not, the message is dead-lettered with ``error`` as the reason. A
150    :class:`Fatal` error skips the remaining attempts, because they would all
151    fail the same way.
152    """
153    return Ack(Action.RETRY, error)
154
155
156def reject(error: BaseException | None = None) -> Ack:
157    """Dead-letters the message without trying again.
158
159    For when the message itself is the problem — a field that cannot be missing
160    is missing, a reference points at nothing — rather than when the world is
161    temporarily unhelpful.
162    """
163    return Ack(Action.REJECT, error)
164
165
166def park(error: BaseException | None = None) -> Ack:
167    """Sets the message aside on ``{queue}.parked`` without trying again.
168
169    For when the message is not readable at all: a body in a format nothing
170    here understands, a version this service was never taught. The engine
171    already parks a body its codec cannot decode; this is how a handler that
172    worked out the same thing one layer further in says so.
173
174    :func:`reject` is the neighbouring word and the difference is worth
175    keeping. A rejected message is a *bad request* — understood, and refused —
176    and it belongs with the other dead letters. A parked one was never
177    understood, and mixing the two means whoever drains the dead-letter queue
178    has to sort them by hand to find the producer that is emitting rubbish.
179    """
180    return Ack(Action.PARK, error)
181
182
183def in_progress(delay: timedelta = DEFAULT_IN_PROGRESS_DELAY) -> Ack:
184    """Puts the message back because somebody else is still working on it.
185
186    What :func:`~acemq_amqp.patterns.idempotent` answers when a redelivery finds
187    a claim that is live but not yet confirmed. Accepting it would lose the
188    message if that other handler then died; running it would do the work
189    twice. So it goes back on its own queue after ``delay``, with its attempt
190    **unchanged**: a claim held elsewhere says nothing about whether this
191    message can be handled, so it neither spends a retry nor can exhaust the
192    policy and be dead-lettered. When the claim is confirmed the next look is a
193    duplicate and is accepted; when its lease runs out, it is taken over.
194
195    The wait is spent in the consumer, holding the delivery, as a short retry's
196    is; keep it short beside the store's claim timeout.
197
198    :param delay: how long until the message is looked at again
199    """
200    return Ack(Action.IN_PROGRESS, delay=delay)
201
202
203class FatalError(Exception):
204    """An error no number of retries will fix.
205
206    Wrapping a cause in this says "stop now" without the handler having to know
207    how many attempts remain, which is knowledge handlers should not need.
208
209    The other languages call this ``Fatal``; the name is Python's because a
210    class name is ergonomics rather than wire contract, and Python expects an
211    exception to say that it is one.
212    """
OUTCOME_ACKED = 'acked'
OUTCOME_RETRIED = 'retried'
OUTCOME_REJECTED = 'rejected'
OUTCOME_DEAD_LETTERED = 'dead_lettered'
OUTCOME_PARKED = 'parked'
OUTCOME_IN_PROGRESS = 'in_progress'
OUTCOME_STOPPED = 'stopped'
DEFAULT_IN_PROGRESS_DELAY = datetime.timedelta(seconds=5)
class Action(enum.Enum):
65class Action(Enum):
66    """The things that can be done with a delivered message."""
67
68    ACCEPT = "accept"
69    RETRY = "retry"
70    REJECT = "reject"
71    PARK = "park"
72    IN_PROGRESS = "in_progress"

The things that can be done with a delivered message.

ACCEPT = <Action.ACCEPT: 'accept'>
RETRY = <Action.RETRY: 'retry'>
REJECT = <Action.REJECT: 'reject'>
PARK = <Action.PARK: 'park'>
IN_PROGRESS = <Action.IN_PROGRESS: 'in_progress'>
@dataclass(frozen=True, slots=True)
class Ack:
75@dataclass(frozen=True, slots=True)
76class Ack:
77    """A handler's decision: it worked, try it again, never again, or nobody
78    can read it.
79
80    Returned rather than performed, so a handler that forgets to decide is a
81    handler that returns ``None`` and is caught immediately, rather than one
82    whose message sits unacknowledged until the connection drops and then comes
83    back — usually to the same handler, with the same outcome.
84    """
85
86    action: Action
87    error: BaseException | None = None
88    #: How long to wait before the message is looked at again. Read for
89    #: :attr:`Action.IN_PROGRESS` only; a retry's wait is the policy's.
90    delay: timedelta | None = None
91
92    def __str__(self) -> str:
93        return self.action.value

A handler's decision: it worked, try it again, never again, or nobody can read it.

Returned rather than performed, so a handler that forgets to decide is a handler that returns None and is caught immediately, rather than one whose message sits unacknowledged until the connection drops and then comes back — usually to the same handler, with the same outcome.

Ack( action: Action, error: BaseException | None = None, delay: datetime.timedelta | None = None)
action: Action
error: BaseException | None
delay: datetime.timedelta | None
@dataclass(frozen=True, slots=True)
class Settlement:
 96@dataclass(frozen=True, slots=True)
 97class Settlement:
 98    """What the consumer did with a delivery, once it had decided.
 99
100    Reported before it is carried out rather than after, because one of them
101    takes time: a consumer-side retry sleeps out the backoff before the message
102    is republished, and a listener told afterwards would be describing the
103    delivery minutes later, on a span that had been open the whole while.
104
105    :param outcome: one of :data:`OUTCOME_ACKED`, :data:`OUTCOME_RETRIED`,
106        :data:`OUTCOME_REJECTED`, :data:`OUTCOME_DEAD_LETTERED` and
107        :data:`OUTCOME_PARKED` — the same words the other libraries put on a
108        span
109    :param reason: why it was set aside, when it was
110    :param delay: how long until the next attempt, when there is one
111    """
112
113    outcome: str
114    reason: str | None = None
115    delay: timedelta | None = None
116
117    @property
118    def dead_lettered(self) -> bool:
119        """Whether the message went to the dead-letter queue.
120
121        True for an outright rejection as well as for an exhausted one: both
122        end up in the same queue, and the difference between the two words is
123        *who decided*, which :attr:`outcome` records and this does not.
124
125        False for :data:`OUTCOME_PARKED`, and that is the whole point of the
126        parked queue: a message nobody could read is a different problem with a
127        different answer from one that failed five times, and whoever drains
128        the dead letters should not have to sort the two apart by hand.
129        """
130        return self.outcome in (OUTCOME_REJECTED, OUTCOME_DEAD_LETTERED)
131
132    @property
133    def parked(self) -> bool:
134        """Whether the message went to the parked queue."""
135        return self.outcome == OUTCOME_PARKED
136
137    def __str__(self) -> str:
138        return self.outcome

What the consumer did with a delivery, once it had decided.

Reported before it is carried out rather than after, because one of them takes time: a consumer-side retry sleeps out the backoff before the message is republished, and a listener told afterwards would be describing the delivery minutes later, on a span that had been open the whole while.

Parameters
Settlement( outcome: str, reason: str | None = None, delay: datetime.timedelta | None = None)
outcome: str
reason: str | None
delay: datetime.timedelta | None
dead_lettered: bool
117    @property
118    def dead_lettered(self) -> bool:
119        """Whether the message went to the dead-letter queue.
120
121        True for an outright rejection as well as for an exhausted one: both
122        end up in the same queue, and the difference between the two words is
123        *who decided*, which :attr:`outcome` records and this does not.
124
125        False for :data:`OUTCOME_PARKED`, and that is the whole point of the
126        parked queue: a message nobody could read is a different problem with a
127        different answer from one that failed five times, and whoever drains
128        the dead letters should not have to sort the two apart by hand.
129        """
130        return self.outcome in (OUTCOME_REJECTED, OUTCOME_DEAD_LETTERED)

Whether the message went to the dead-letter queue.

True for an outright rejection as well as for an exhausted one: both end up in the same queue, and the difference between the two words is who decided, which outcome records and this does not.

False for OUTCOME_PARKED, and that is the whole point of the parked queue: a message nobody could read is a different problem with a different answer from one that failed five times, and whoever drains the dead letters should not have to sort the two apart by hand.

parked: bool
132    @property
133    def parked(self) -> bool:
134        """Whether the message went to the parked queue."""
135        return self.outcome == OUTCOME_PARKED

Whether the message went to the parked queue.

def accept() -> Ack:
141def accept() -> Ack:
142    """Confirms the message. It will not be delivered again."""
143    return Ack(Action.ACCEPT)

Confirms the message. It will not be delivered again.

def retry(error: BaseException | None = None) -> Ack:
146def retry(error: BaseException | None = None) -> Ack:
147    """Returns the message to be tried again.
148
149    The retry policy decides whether there is another attempt left; when there
150    is not, the message is dead-lettered with ``error`` as the reason. A
151    :class:`Fatal` error skips the remaining attempts, because they would all
152    fail the same way.
153    """
154    return Ack(Action.RETRY, error)

Returns the message to be tried again.

The retry policy decides whether there is another attempt left; when there is not, the message is dead-lettered with error as the reason. A Fatal error skips the remaining attempts, because they would all fail the same way.

def reject(error: BaseException | None = None) -> Ack:
157def reject(error: BaseException | None = None) -> Ack:
158    """Dead-letters the message without trying again.
159
160    For when the message itself is the problem — a field that cannot be missing
161    is missing, a reference points at nothing — rather than when the world is
162    temporarily unhelpful.
163    """
164    return Ack(Action.REJECT, error)

Dead-letters the message without trying again.

For when the message itself is the problem — a field that cannot be missing is missing, a reference points at nothing — rather than when the world is temporarily unhelpful.

def park(error: BaseException | None = None) -> Ack:
167def park(error: BaseException | None = None) -> Ack:
168    """Sets the message aside on ``{queue}.parked`` without trying again.
169
170    For when the message is not readable at all: a body in a format nothing
171    here understands, a version this service was never taught. The engine
172    already parks a body its codec cannot decode; this is how a handler that
173    worked out the same thing one layer further in says so.
174
175    :func:`reject` is the neighbouring word and the difference is worth
176    keeping. A rejected message is a *bad request* — understood, and refused —
177    and it belongs with the other dead letters. A parked one was never
178    understood, and mixing the two means whoever drains the dead-letter queue
179    has to sort them by hand to find the producer that is emitting rubbish.
180    """
181    return Ack(Action.PARK, error)

Sets the message aside on {queue}.parked without trying again.

For when the message is not readable at all: a body in a format nothing here understands, a version this service was never taught. The engine already parks a body its codec cannot decode; this is how a handler that worked out the same thing one layer further in says so.

reject() is the neighbouring word and the difference is worth keeping. A rejected message is a bad request — understood, and refused — and it belongs with the other dead letters. A parked one was never understood, and mixing the two means whoever drains the dead-letter queue has to sort them by hand to find the producer that is emitting rubbish.

def in_progress( delay: datetime.timedelta = datetime.timedelta(seconds=5)) -> Ack:
184def in_progress(delay: timedelta = DEFAULT_IN_PROGRESS_DELAY) -> Ack:
185    """Puts the message back because somebody else is still working on it.
186
187    What :func:`~acemq_amqp.patterns.idempotent` answers when a redelivery finds
188    a claim that is live but not yet confirmed. Accepting it would lose the
189    message if that other handler then died; running it would do the work
190    twice. So it goes back on its own queue after ``delay``, with its attempt
191    **unchanged**: a claim held elsewhere says nothing about whether this
192    message can be handled, so it neither spends a retry nor can exhaust the
193    policy and be dead-lettered. When the claim is confirmed the next look is a
194    duplicate and is accepted; when its lease runs out, it is taken over.
195
196    The wait is spent in the consumer, holding the delivery, as a short retry's
197    is; keep it short beside the store's claim timeout.
198
199    :param delay: how long until the message is looked at again
200    """
201    return Ack(Action.IN_PROGRESS, delay=delay)

Puts the message back because somebody else is still working on it.

What ~acemq_amqp.patterns.idempotent() answers when a redelivery finds a claim that is live but not yet confirmed. Accepting it would lose the message if that other handler then died; running it would do the work twice. So it goes back on its own queue after delay, with its attempt unchanged: a claim held elsewhere says nothing about whether this message can be handled, so it neither spends a retry nor can exhaust the policy and be dead-lettered. When the claim is confirmed the next look is a duplicate and is accepted; when its lease runs out, it is taken over.

The wait is spent in the consumer, holding the delivery, as a short retry's is; keep it short beside the store's claim timeout.

Parameters
  • delay: how long until the message is looked at again
class FatalError(builtins.Exception):
204class FatalError(Exception):
205    """An error no number of retries will fix.
206
207    Wrapping a cause in this says "stop now" without the handler having to know
208    how many attempts remain, which is knowledge handlers should not need.
209
210    The other languages call this ``Fatal``; the name is Python's because a
211    class name is ergonomics rather than wire contract, and Python expects an
212    exception to say that it is one.
213    """

An error no number of retries will fix.

Wrapping a cause in this says "stop now" without the handler having to know how many attempts remain, which is knowledge handlers should not need.

The other languages call this Fatal; the name is Python's because a class name is ergonomics rather than wire contract, and Python expects an exception to say that it is one.