use async in email handler
This commit is contained in:
parent
eb0e327402
commit
4d7cd09847
|
@ -31,6 +31,7 @@ It should contain the following info:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
import argparse
|
import argparse
|
||||||
|
import asyncio
|
||||||
import email
|
import email
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
@ -1711,7 +1712,7 @@ def should_ignore(mail_from: str, rcpt_tos: List[str]) -> bool:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def handle(envelope: Envelope) -> str:
|
async def handle(envelope: Envelope) -> str:
|
||||||
"""Return SMTP status"""
|
"""Return SMTP status"""
|
||||||
|
|
||||||
# sanitize mail_from, rcpt_tos
|
# sanitize mail_from, rcpt_tos
|
||||||
|
@ -1934,7 +1935,7 @@ def handle(envelope: Envelope) -> str:
|
||||||
class MailHandler:
|
class MailHandler:
|
||||||
async def handle_DATA(self, server, session, envelope: Envelope):
|
async def handle_DATA(self, server, session, envelope: Envelope):
|
||||||
try:
|
try:
|
||||||
ret = self._handle(envelope)
|
ret = await self._handle(envelope)
|
||||||
return ret
|
return ret
|
||||||
except Exception:
|
except Exception:
|
||||||
LOG.e(
|
LOG.e(
|
||||||
|
@ -1945,7 +1946,7 @@ class MailHandler:
|
||||||
return status.E404
|
return status.E404
|
||||||
|
|
||||||
@newrelic.agent.background_task(application=newrelic_app)
|
@newrelic.agent.background_task(application=newrelic_app)
|
||||||
def _handle(self, envelope: Envelope):
|
async def _handle(self, envelope: Envelope):
|
||||||
start = time.time()
|
start = time.time()
|
||||||
|
|
||||||
# generate a different message_id to keep track of an email lifecycle
|
# generate a different message_id to keep track of an email lifecycle
|
||||||
|
@ -1958,23 +1959,21 @@ class MailHandler:
|
||||||
envelope.rcpt_tos,
|
envelope.rcpt_tos,
|
||||||
)
|
)
|
||||||
|
|
||||||
app = new_app()
|
ret = await handle(envelope)
|
||||||
with app.app_context():
|
elapsed = time.time() - start
|
||||||
ret = handle(envelope)
|
LOG.i(
|
||||||
elapsed = time.time() - start
|
"Finish mail from %s, rctp tos %s, takes %s seconds <<===",
|
||||||
LOG.i(
|
envelope.mail_from,
|
||||||
"Finish mail from %s, rctp tos %s, takes %s seconds <<===",
|
envelope.rcpt_tos,
|
||||||
envelope.mail_from,
|
elapsed,
|
||||||
envelope.rcpt_tos,
|
)
|
||||||
elapsed,
|
newrelic.agent.record_custom_metric(
|
||||||
)
|
"Custom/email_handler_time", elapsed, newrelic_app
|
||||||
newrelic.agent.record_custom_metric(
|
)
|
||||||
"Custom/email_handler_time", elapsed, newrelic_app
|
newrelic.agent.record_custom_metric(
|
||||||
)
|
"Custom/number_incoming_email", 1, newrelic_app
|
||||||
newrelic.agent.record_custom_metric(
|
)
|
||||||
"Custom/number_incoming_email", 1, newrelic_app
|
return ret
|
||||||
)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
|
|
||||||
def main(port: int):
|
def main(port: int):
|
||||||
|
|
Loading…
Reference in a new issue