snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
This commit is contained in:
92
zerver/worker/email_senders_base.py
Normal file
92
zerver/worker/email_senders_base.py
Normal file
@@ -0,0 +1,92 @@
|
||||
# Documented in https://zulip.readthedocs.io/en/latest/subsystems/queuing.html
|
||||
import copy
|
||||
import logging
|
||||
from collections.abc import Callable
|
||||
from functools import wraps
|
||||
from typing import Any
|
||||
|
||||
from django.core.mail import get_connection
|
||||
from typing_extensions import override
|
||||
|
||||
from zerver.lib.queue import retry_event
|
||||
from zerver.lib.send_email import (
|
||||
EmailNotDeliveredError,
|
||||
handle_send_email_format_changes,
|
||||
send_immediate_email,
|
||||
)
|
||||
from zerver.models import Realm
|
||||
from zerver.worker.base import ConcreteQueueWorker, LoopQueueProcessingWorker
|
||||
from zproject.email_backends import PersistentSMTPEmailBackend
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# If you change the function on which this decorator is used be careful that the new
|
||||
# function doesn't delete the "failed_tries" attribute of "data" which is needed for
|
||||
# "retry_event" to work correctly; see EmailSendingWorker for an example with deepcopy.
|
||||
def retry_send_email_failures(
|
||||
func: Callable[[ConcreteQueueWorker, dict[str, Any]], None],
|
||||
) -> Callable[[ConcreteQueueWorker, dict[str, Any]], None]:
|
||||
@wraps(func)
|
||||
def wrapper(worker: ConcreteQueueWorker, data: dict[str, Any]) -> None:
|
||||
try:
|
||||
func(worker, data)
|
||||
except EmailNotDeliveredError as e:
|
||||
error_class_name = type(e).__name__
|
||||
|
||||
def on_failure(event: dict[str, Any]) -> None:
|
||||
logging.exception(
|
||||
"Event %r failed due to exception %s", event, error_class_name, stack_info=True
|
||||
)
|
||||
|
||||
retry_event(worker.queue_name, data, on_failure)
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
class EmailSendingWorker(LoopQueueProcessingWorker):
|
||||
def __init__(
|
||||
self,
|
||||
threaded: bool = False,
|
||||
disable_timeout: bool = False,
|
||||
worker_num: int | None = None,
|
||||
) -> None:
|
||||
super().__init__(threaded, disable_timeout, worker_num)
|
||||
connection = get_connection()
|
||||
if isinstance(connection, PersistentSMTPEmailBackend):
|
||||
self.connection: PersistentSMTPEmailBackend | None = connection
|
||||
else:
|
||||
self.connection = None
|
||||
|
||||
@retry_send_email_failures
|
||||
def send_email(self, event: dict[str, Any]) -> None:
|
||||
# Copy the event, so that we don't pass the `failed_tries'
|
||||
# data to send_email (which neither takes that
|
||||
# argument nor needs that data).
|
||||
copied_event = copy.deepcopy(event)
|
||||
if "failed_tries" in copied_event:
|
||||
del copied_event["failed_tries"]
|
||||
handle_send_email_format_changes(copied_event)
|
||||
if "realm_id" in copied_event:
|
||||
# "realm" does not serialize over the queue, so we send the realm_id
|
||||
if copied_event.get("realm_id") is not None:
|
||||
copied_event["realm"] = Realm.objects.get(id=copied_event["realm_id"])
|
||||
del copied_event["realm_id"]
|
||||
if self.connection is not None:
|
||||
self.connection.ensure_connected()
|
||||
send_immediate_email(**copied_event, connection=self.connection)
|
||||
else:
|
||||
send_immediate_email(**copied_event)
|
||||
|
||||
@override
|
||||
def consume_batch(self, events: list[dict[str, Any]]) -> None:
|
||||
for event in events:
|
||||
self.send_email(event)
|
||||
|
||||
@override
|
||||
def stop(self) -> None: # nocoverage
|
||||
try:
|
||||
if self.connection is not None:
|
||||
self.connection.close()
|
||||
finally:
|
||||
super().stop()
|
||||
Reference in New Issue
Block a user