選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

mail_change_mail_task.py 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import logging
  2. import time
  3. import click
  4. from celery import shared_task
  5. from extensions.ext_mail import mail
  6. from libs.email_i18n import EmailType, get_email_i18n_service
  7. logger = logging.getLogger(__name__)
  8. @shared_task(queue="mail")
  9. def send_change_mail_task(language: str, to: str, code: str, phase: str) -> None:
  10. """
  11. Send change email notification with internationalization support.
  12. Args:
  13. language: Language code for email localization
  14. to: Recipient email address
  15. code: Email verification code
  16. phase: Change email phase ('old_email' or 'new_email')
  17. """
  18. if not mail.is_inited():
  19. return
  20. logger.info(click.style(f"Start change email mail to {to}", fg="green"))
  21. start_at = time.perf_counter()
  22. try:
  23. email_service = get_email_i18n_service()
  24. email_service.send_change_email(
  25. language_code=language,
  26. to=to,
  27. code=code,
  28. phase=phase,
  29. )
  30. end_at = time.perf_counter()
  31. logger.info(click.style(f"Send change email mail to {to} succeeded: latency: {end_at - start_at}", fg="green"))
  32. except Exception:
  33. logger.exception("Send change email mail to %s failed", to)
  34. @shared_task(queue="mail")
  35. def send_change_mail_completed_notification_task(language: str, to: str) -> None:
  36. """
  37. Send change email completed notification with internationalization support.
  38. Args:
  39. language: Language code for email localization
  40. to: Recipient email address
  41. """
  42. if not mail.is_inited():
  43. return
  44. logger.info(click.style(f"Start change email completed notify mail to {to}", fg="green"))
  45. start_at = time.perf_counter()
  46. try:
  47. email_service = get_email_i18n_service()
  48. email_service.send_email(
  49. email_type=EmailType.CHANGE_EMAIL_COMPLETED,
  50. language_code=language,
  51. to=to,
  52. template_context={
  53. "to": to,
  54. "email": to,
  55. },
  56. )
  57. end_at = time.perf_counter()
  58. logger.info(
  59. click.style(
  60. f"Send change email completed mail to {to} succeeded: latency: {end_at - start_at}",
  61. fg="green",
  62. )
  63. )
  64. except Exception:
  65. logger.exception("Send change email completed mail to %s failed", to)