Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions sdk/servicebus/azure-servicebus/azure/servicebus/_common/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
MESSAGE_DEAD_LETTER,
MESSAGE_ABANDON,
MESSAGE_DEFER,
MESSAGE_RENEW_LOCK
MESSAGE_RENEW_LOCK,
DEADLETTERNAME
)
from ..exceptions import (
MessageAlreadySettled,
Expand Down Expand Up @@ -436,9 +437,10 @@ class ReceivedMessage(PeekMessage):
:dedent: 4
:caption: Checking the properties on a received message.
"""
def __init__(self, message, mode=ReceiveSettleMode.PeekLock):
def __init__(self, message, mode=ReceiveSettleMode.PeekLock, is_deferred_message=False):
Comment thread
yunhaoling marked this conversation as resolved.
Outdated
super(ReceivedMessage, self).__init__(message=message)
self._settled = (mode == ReceiveSettleMode.ReceiveAndDelete)
self._is_deferred_message = is_deferred_message
self.auto_renew_error = None

def _is_live(self, action):
Expand Down Expand Up @@ -529,9 +531,13 @@ def complete(self):
:raises: ~azure.servicebus.common.errors.SessionLockExpired if session lock has already expired.
:raises: ~azure.servicebus.common.errors.MessageSettleFailed if message settle operation fails.
"""
# pylint: disable=protected-access
self._is_live(MESSAGE_COMPLETE)
try:
self._receiver._settle_message(SETTLEMENT_COMPLETE, [self.lock_token]) # pylint: disable=protected-access
if self._is_deferred_message:
self._receiver._settle_message(SETTLEMENT_COMPLETE, [self.lock_token])
else:
self.message.accept()
except Exception as e:
raise MessageSettleFailed(MESSAGE_COMPLETE, e)
self._settled = True
Expand All @@ -558,11 +564,16 @@ def dead_letter(self, reason=None, description=None):
MGMT_REQUEST_DEAD_LETTER_REASON: str(reason) if reason else "",
MGMT_REQUEST_DEAD_LETTER_DESCRIPTION: str(description) if description else ""}
try:
self._receiver._settle_message(
SETTLEMENT_DEADLETTER,
[self.lock_token],
dead_letter_details=details
)
if self._is_deferred_message:
self._receiver._settle_message(
SETTLEMENT_DEADLETTER,
[self.lock_token],
dead_letter_details=details
)
else:
# note: message.reject() can not set reason and description properly due to the issue
# https://github.com/Azure/azure-uamqp-python/issues/155
self.message.reject(condition=DEADLETTERNAME, description=description)
Comment thread
yunhaoling marked this conversation as resolved.
Outdated
except Exception as e:
raise MessageSettleFailed(MESSAGE_DEAD_LETTER, e)
self._settled = True
Expand All @@ -579,9 +590,13 @@ def abandon(self):
:raises: ~azure.servicebus.common.errors.SessionLockExpired if session lock has already expired.
:raises: ~azure.servicebus.common.errors.MessageSettleFailed if message settle operation fails.
"""
# pylint: disable=protected-access
self._is_live(MESSAGE_ABANDON)
try:
self._receiver._settle_message(SETTLEMENT_ABANDON, [self.lock_token]) # pylint: disable=protected-access
if self._is_deferred_message:
self._receiver._settle_message(SETTLEMENT_ABANDON, [self.lock_token])
else:
self.message.modify(True, False)
except Exception as e:
raise MessageSettleFailed(MESSAGE_ABANDON, e)
self._settled = True
Expand All @@ -601,7 +616,10 @@ def defer(self):
"""
self._is_live(MESSAGE_DEFER)
try:
self._receiver._settle_message(SETTLEMENT_DEFER, [self.lock_token]) # pylint: disable=protected-access
if self._is_deferred_message:
self._receiver._settle_message(SETTLEMENT_DEFER, [self.lock_token]) # pylint: disable=protected-access
else:
self.message.modify(True, True)
except Exception as e:
raise MessageSettleFailed(MESSAGE_DEFER, e)
self._settled = True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def deferred_message_op(
parsed = []
for m in message.get_data()[b'messages']:
wrapped = uamqp.Message.decode_from_bytes(bytearray(m[b'message']))
parsed.append(message_type(wrapped, mode))
parsed.append(message_type(wrapped, mode, is_deferred_message=True))
return parsed
if status_code in [202, 204]:
return []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# -------------------------------------------------------------------------
import functools
from typing import Optional

from .._common import message as sync_message
Expand All @@ -19,7 +20,8 @@
MESSAGE_DEAD_LETTER,
MESSAGE_ABANDON,
MESSAGE_DEFER,
MESSAGE_RENEW_LOCK
MESSAGE_RENEW_LOCK,
DEADLETTERNAME
)
from .._common.utils import get_running_loop, utc_from_timestamp
from ..exceptions import MessageSettleFailed
Expand All @@ -30,9 +32,9 @@ class ReceivedMessage(sync_message.ReceivedMessage):

"""

def __init__(self, message, mode=ReceiveSettleMode.PeekLock, loop=None):
def __init__(self, message, mode=ReceiveSettleMode.PeekLock, is_deferred_message=False, loop=None):
self._loop = loop or get_running_loop()
super(ReceivedMessage, self).__init__(message=message, mode=mode)
super(ReceivedMessage, self).__init__(message=message, mode=mode, is_deferred_message=is_deferred_message)

async def complete(self):
# type: () -> None
Expand All @@ -49,7 +51,10 @@ async def complete(self):
# pylint: disable=protected-access
self._is_live(MESSAGE_COMPLETE)
try:
await self._receiver._settle_message(SETTLEMENT_COMPLETE, [self.lock_token])
if self._is_deferred_message:
await self._receiver._settle_message(SETTLEMENT_COMPLETE, [self.lock_token])
else:
await self._loop.run_in_executor(None, self.message.accept)
except Exception as e:
raise MessageSettleFailed(MESSAGE_COMPLETE, e)
self._settled = True
Expand All @@ -75,11 +80,17 @@ async def dead_letter(self, reason=None, description=None):
MGMT_REQUEST_DEAD_LETTER_REASON: str(reason) if reason else "",
MGMT_REQUEST_DEAD_LETTER_DESCRIPTION: str(description) if description else ""}
try:
await self._receiver._settle_message(
SETTLEMENT_DEADLETTER,
[self.lock_token],
dead_letter_details=details
)
if self._is_deferred_message:
await self._receiver._settle_message(
SETTLEMENT_DEADLETTER,
[self.lock_token],
dead_letter_details=details
)
else:
# note: message.reject() can not set reason and description properly due to the issue
# https://github.com/Azure/azure-uamqp-python/issues/155
reject = functools.partial(self.message.reject, condition=DEADLETTERNAME, description=description)
await self._loop.run_in_executor(None, reject)
except Exception as e:
raise MessageSettleFailed(MESSAGE_DEAD_LETTER, e)
self._settled = True
Expand All @@ -96,7 +107,11 @@ async def abandon(self):
# pylint: disable=protected-access
self._is_live(MESSAGE_ABANDON)
try:
await self._receiver._settle_message(SETTLEMENT_ABANDON, [self.lock_token])
if self._is_deferred_message:
await self._receiver._settle_message(SETTLEMENT_ABANDON, [self.lock_token])
else:
modify = functools.partial(self.message.modify, True, False)
await self._loop.run_in_executor(None, modify)
except Exception as e:
raise MessageSettleFailed(MESSAGE_ABANDON, e)
self._settled = True
Expand All @@ -113,7 +128,11 @@ async def defer(self):
# pylint: disable=protected-access
self._is_live(MESSAGE_DEFER)
try:
await self._receiver._settle_message(SETTLEMENT_DEFER, [self.lock_token])
if self._is_deferred_message:
await self._receiver._settle_message(SETTLEMENT_DEFER, [self.lock_token])
else:
modify = functools.partial(self.message.modify, True, True)
await self._loop.run_in_executor(None, modify)
except Exception as e:
raise MessageSettleFailed(MESSAGE_DEFER, e)
self._settled = True
Expand Down