Skip to content

Commit 18d4aa9

Browse files
authored
Merge pull request #62 from chadgates/bugfix/46
Bugfix/46
2 parents e499e36 + bfaa493 commit 18d4aa9

File tree

3 files changed

+53
-37
lines changed

3 files changed

+53
-37
lines changed

Diff for: pyas2/management/commands/manageas2server.py

+38-35
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,41 @@ def add_arguments(self, parser):
4242
help="Handle sending and receiving of Asynchronous MDNs.",
4343
)
4444

45+
def retry(self, retry_msg):
46+
# Increase the retry count
47+
if not retry_msg.retries:
48+
retry_msg.retries = 1
49+
else:
50+
retry_msg.retries += 1
51+
52+
# if max retries has exceeded then mark message status as error
53+
if retry_msg.retries > settings.MAX_RETRIES:
54+
if retry_msg.status == "P":
55+
retry_msg.detailed_status = (
56+
"Failed to receive asynchronous MDN within the threshold limit."
57+
)
58+
elif retry_msg.status == "R":
59+
retry_msg.detailed_status = "Retry count exceeded the limit."
60+
61+
retry_msg.status = "E"
62+
retry_msg.save()
63+
return
64+
65+
self.stdout.write("Retry send the message with ID %s" % retry_msg.message_id)
66+
67+
# Build and resend the AS2 message
68+
as2message = AS2Message(
69+
sender=retry_msg.organization.as2org,
70+
receiver=retry_msg.partner.as2partner,
71+
)
72+
as2message.build(
73+
retry_msg.payload.read(),
74+
filename=os.path.basename(retry_msg.payload.name),
75+
subject=retry_msg.partner.subject,
76+
content_type=retry_msg.partner.content_type,
77+
)
78+
retry_msg.send_message(as2message.headers, as2message.content)
79+
4580
def handle(self, *args, **options):
4681

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

5287
for failed_msg in failed_msgs:
53-
54-
# Increase the retry count
55-
if not failed_msg.retries:
56-
failed_msg.retries = 1
57-
else:
58-
failed_msg.retries += 1
59-
60-
# if max retries has exceeded then mark message status as error
61-
if failed_msg.retries > settings.MAX_RETRIES:
62-
failed_msg.status = "E"
63-
failed_msg.save()
64-
continue
65-
66-
self.stdout.write(
67-
"Retry send the message with ID %s" % failed_msg.message_id
68-
)
69-
70-
# Build and resend the AS2 message
71-
as2message = AS2Message(
72-
sender=failed_msg.organization.as2org,
73-
receiver=failed_msg.partner.as2partner,
74-
)
75-
as2message.build(
76-
failed_msg.payload.read(),
77-
filename=os.path.basename(failed_msg.payload.name),
78-
subject=failed_msg.partner.subject,
79-
content_type=failed_msg.partner.content_type,
80-
)
81-
failed_msg.send_message(as2message.headers, as2message.content)
88+
self.retry(failed_msg)
8289

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

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

138-
# Mark these messages as erred
145+
# Retry sending the message if not MDN received.
139146
for pending_msg in out_pending_msgs:
140-
pending_msg.status = "E"
141-
pending_msg.detailed_status = (
142-
"Failed to receive asynchronous MDN within the " "threshold limit."
143-
)
144-
pending_msg.save()
147+
self.retry(pending_msg)
145148

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

Diff for: pyas2/models.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,10 @@ def create_from_as2mdn(self, as2mdn, message, status, return_url=None):
545545
mdn, _ = self.update_or_create(
546546
message=message,
547547
defaults=dict(
548-
mdn_id=message_id, status=status, signed=signed, return_url=return_url,
548+
mdn_id=message_id,
549+
status=status,
550+
signed=signed,
551+
return_url=return_url,
549552
),
550553
)
551554
filename = f"{uuid4()}.mdn"

Diff for: pyas2/tests/test_commands.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,17 @@ def test_manageserver_command(mocker, organization, partner):
122122
assert out_message.retries == 2
123123
assert out_message.status == "E"
124124

125-
# Test the async mdn command for outbound messages
125+
# Test the async mdn command for outbound messages, retry when no MDN received
126+
app_settings.ASYNC_MDN_WAIT = 0
127+
out_message.status = "P"
128+
out_message.retries = 0
129+
out_message.save()
130+
management.call_command("manageas2server", async_mdns=True)
131+
out_message.refresh_from_db()
132+
assert out_message.status == "R"
133+
assert out_message.retries == 1
134+
135+
# Test the async mdn command for outbound messages, finally fail when no MDN received
126136
app_settings.ASYNC_MDN_WAIT = 0
127137
out_message.status = "P"
128138
out_message.save()

0 commit comments

Comments
 (0)