Skip to content

Commit

Permalink
feat: Add finalback keyword argument to decorate()
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Jan 18, 2022
1 parent a11b1b5 commit 039a2e9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
=========

0.0.7 (2022-01-18)
------------------

Added
~~~~~

- :meth:`yapw.decorators.decorate` accepts a ``finalback`` keyword argument.

0.0.6 (2022-01-17)
------------------

Expand Down
40 changes: 39 additions & 1 deletion tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

import pytest

from yapw.decorators import default_decode, discard, requeue
from yapw.decorators import decorate, default_decode, discard, requeue

# https://pika.readthedocs.io/en/stable/modules/spec.html#pika.spec.Basic.Deliver
Deliver = namedtuple("Deliver", "delivery_tag redelivered routing_key")
# https://pika.readthedocs.io/en/stable/modules/spec.html#pika.spec.BasicProperties
BasicProperties = namedtuple("BasicProperties", "content_type")

logger = logging.getLogger(__name__)


def raises(*args):
raise Exception("message")
Expand All @@ -30,6 +32,16 @@ def closes(*args):
opened = False


def finalback(decode, callback, state, channel, method, properties, body):
def errback():
logger.warning("errback")

def finalback():
logger.warning("finalback")

decorate(decode, callback, state, channel, method, properties, body, errback, finalback)


@patch("yapw.decorators.nack")
def test_decode_json(nack, caplog):
caplog.set_level(logging.DEBUG)
Expand Down Expand Up @@ -139,3 +151,29 @@ def test_finally(nack):

global opened
assert opened is False


@patch("yapw.decorators.nack")
def test_finalback_raises(nack, caplog):
method = Deliver(1, False, "key")
properties = BasicProperties("application/json")

finalback(default_decode, raises, "state", "channel", method, properties, b'"body"')

assert len(caplog.records) == 2
assert caplog.records[0].levelname == "WARNING"
assert caplog.records[0].message == "errback"
assert caplog.records[-1].levelname == "WARNING"
assert caplog.records[-1].message == "finalback"


@patch("yapw.decorators.nack")
def test_finalback_passes(nack, caplog):
method = Deliver(1, False, "key")
properties = BasicProperties("application/json")

finalback(default_decode, passes, "state", "channel", method, properties, b'"body"')

assert len(caplog.records) == 1
assert caplog.records[-1].levelname == "WARNING"
assert caplog.records[-1].message == "finalback"
4 changes: 4 additions & 0 deletions yapw/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def decorate(
properties: pika.BasicProperties,
body: bytes,
errback: Callable[[], None],
finalback: Optional[Callable[[], None]] = None,
) -> None:
"""
Decode the message ``body`` using the ``decode`` function, and call the consumer ``callback``.
Expand All @@ -87,6 +88,9 @@ def decorate(
callback(state, channel, method, properties, message)
except Exception:
errback()
finally:
if finalback:
finalback()
except Exception:
logger.exception("%r can't be decoded, sending SIGUSR2", body)
os.kill(os.getpid(), signal.SIGUSR2)
Expand Down

0 comments on commit 039a2e9

Please sign in to comment.