2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 11:36:44 +00:00

Offload initial email task (#8413)

- Prevent blocking on email task
- Allow worker to try multiple times
- Don't pass 500 error back to form validation
This commit is contained in:
Oliver 2024-11-03 09:56:28 +11:00 committed by GitHub
parent 4308f0c2f9
commit d1c2213836
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 1 deletions

View File

@ -510,6 +510,7 @@ class UserCreateSerializer(ExtendedUserSerializer):
def create(self, validated_data): def create(self, validated_data):
"""Send an e email to the user after creation.""" """Send an e email to the user after creation."""
from InvenTree.helpers_model import get_base_url from InvenTree.helpers_model import get_base_url
from InvenTree.tasks import email_user, offload_task
base_url = get_base_url() base_url = get_base_url()
@ -527,8 +528,12 @@ class UserCreateSerializer(ExtendedUserSerializer):
if base_url: if base_url:
message += f'\n\nURL: {base_url}' message += f'\n\nURL: {base_url}'
subject = _('Welcome to InvenTree')
# Send the user an onboarding email (from current site) # Send the user an onboarding email (from current site)
instance.email_user(subject=_('Welcome to InvenTree'), message=message) offload_task(
email_user, instance.pk, str(subject), str(message), force_async=True
)
return instance return instance

View File

@ -12,6 +12,7 @@ from datetime import datetime, timedelta
from typing import Callable, Optional from typing import Callable, Optional
from django.conf import settings from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.exceptions import AppRegistryNotReady from django.core.exceptions import AppRegistryNotReady
from django.core.management import call_command from django.core.management import call_command
from django.db import DEFAULT_DB_ALIAS, connections from django.db import DEFAULT_DB_ALIAS, connections
@ -693,3 +694,14 @@ def check_for_migrations(force: bool = False, reload_registry: bool = True):
# We should be current now - triggering full reload to make sure all models # We should be current now - triggering full reload to make sure all models
# are loaded fully in their new state. # are loaded fully in their new state.
registry.reload_plugins(full_reload=True, force_reload=True, collect=True) registry.reload_plugins(full_reload=True, force_reload=True, collect=True)
def email_user(user_id: int, subject: str, message: str) -> None:
"""Send a message to a user."""
try:
user = get_user_model().objects.get(pk=user_id)
except Exception:
logger.warning('User <%s> not found - cannot send welcome message', user_id)
return
user.email_user(subject=subject, message=message)