Skip to content

Commit

Permalink
Merge pull request #62 from chadgates/bugfix/46
Browse files Browse the repository at this point in the history
Bugfix/46
  • Loading branch information
abhishek-ram authored Apr 3, 2021
2 parents e499e36 + bfaa493 commit 18d4aa9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 37 deletions.
73 changes: 38 additions & 35 deletions pyas2/management/commands/manageas2server.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,41 @@ def add_arguments(self, parser):
help="Handle sending and receiving of Asynchronous MDNs.",
)

def retry(self, retry_msg):
# Increase the retry count
if not retry_msg.retries:
retry_msg.retries = 1
else:
retry_msg.retries += 1

# if max retries has exceeded then mark message status as error
if retry_msg.retries > settings.MAX_RETRIES:
if retry_msg.status == "P":
retry_msg.detailed_status = (
"Failed to receive asynchronous MDN within the threshold limit."
)
elif retry_msg.status == "R":
retry_msg.detailed_status = "Retry count exceeded the limit."

retry_msg.status = "E"
retry_msg.save()
return

self.stdout.write("Retry send the message with ID %s" % retry_msg.message_id)

# Build and resend the AS2 message
as2message = AS2Message(
sender=retry_msg.organization.as2org,
receiver=retry_msg.partner.as2partner,
)
as2message.build(
retry_msg.payload.read(),
filename=os.path.basename(retry_msg.payload.name),
subject=retry_msg.partner.subject,
content_type=retry_msg.partner.content_type,
)
retry_msg.send_message(as2message.headers, as2message.content)

def handle(self, *args, **options):

if options["retry"]:
Expand All @@ -50,35 +85,7 @@ def handle(self, *args, **options):
failed_msgs = Message.objects.filter(status="R", direction="OUT")

for failed_msg in failed_msgs:

# Increase the retry count
if not failed_msg.retries:
failed_msg.retries = 1
else:
failed_msg.retries += 1

# if max retries has exceeded then mark message status as error
if failed_msg.retries > settings.MAX_RETRIES:
failed_msg.status = "E"
failed_msg.save()
continue

self.stdout.write(
"Retry send the message with ID %s" % failed_msg.message_id
)

# Build and resend the AS2 message
as2message = AS2Message(
sender=failed_msg.organization.as2org,
receiver=failed_msg.partner.as2partner,
)
as2message.build(
failed_msg.payload.read(),
filename=os.path.basename(failed_msg.payload.name),
subject=failed_msg.partner.subject,
content_type=failed_msg.partner.content_type,
)
failed_msg.send_message(as2message.headers, as2message.content)
self.retry(failed_msg)

self.stdout.write("Processed all failed outbound messages")

Expand Down Expand Up @@ -135,13 +142,9 @@ def handle(self, *args, **options):
status="P", direction="OUT", timestamp__lt=time_threshold
)

# Mark these messages as erred
# Retry sending the message if not MDN received.
for pending_msg in out_pending_msgs:
pending_msg.status = "E"
pending_msg.detailed_status = (
"Failed to receive asynchronous MDN within the " "threshold limit."
)
pending_msg.save()
self.retry(pending_msg)

self.stdout.write(u"Successfully processed all pending mdns.")

Expand Down
5 changes: 4 additions & 1 deletion pyas2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,10 @@ def create_from_as2mdn(self, as2mdn, message, status, return_url=None):
mdn, _ = self.update_or_create(
message=message,
defaults=dict(
mdn_id=message_id, status=status, signed=signed, return_url=return_url,
mdn_id=message_id,
status=status,
signed=signed,
return_url=return_url,
),
)
filename = f"{uuid4()}.mdn"
Expand Down
12 changes: 11 additions & 1 deletion pyas2/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,17 @@ def test_manageserver_command(mocker, organization, partner):
assert out_message.retries == 2
assert out_message.status == "E"

# Test the async mdn command for outbound messages
# Test the async mdn command for outbound messages, retry when no MDN received
app_settings.ASYNC_MDN_WAIT = 0
out_message.status = "P"
out_message.retries = 0
out_message.save()
management.call_command("manageas2server", async_mdns=True)
out_message.refresh_from_db()
assert out_message.status == "R"
assert out_message.retries == 1

# Test the async mdn command for outbound messages, finally fail when no MDN received
app_settings.ASYNC_MDN_WAIT = 0
out_message.status = "P"
out_message.save()
Expand Down

0 comments on commit 18d4aa9

Please sign in to comment.