Skip to content

Commit

Permalink
add management command for sync suppressed emails (#21784)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinMind committed Feb 1, 2024
1 parent fb8a38e commit 6db88f3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/olympia/users/management/commands/sync_suppressed_emails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.core.management.base import BaseCommand

from olympia.users.tasks import sync_blocked_emails


class Command(BaseCommand):
"""Sync Socket labs suppression list to database."""

def handle(self, *args, **options):
sync_blocked_emails.apply()
10 changes: 10 additions & 0 deletions src/olympia/users/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ def assert_socket_labs_settings_defined():
def sync_blocked_emails(batch_size=BATCH_SIZE, **kw):
assert_socket_labs_settings_defined()

task_log.info('fetching suppression list from socket labs...')

path = f'servers/{settings.SOCKET_LABS_SERVER_ID}/suppressions/download'
params = {'sortField': 'suppressionLastUpdate', 'sortDirection': 'dsc'}
url = (
Expand All @@ -120,6 +122,9 @@ def sync_blocked_emails(batch_size=BATCH_SIZE, **kw):
# Raise exception if not 200 like response
response.raise_for_status()

size_in_mb = len(response.content) / (1024 * 1024)
task_log.info(f'Downloaded suppression list of {size_in_mb:.2f} MB.')

with tempfile.NamedTemporaryFile(
dir=settings.TMP_PATH, delete=not settings.DEBUG, mode='w+b'
) as csv_file:
Expand All @@ -131,6 +136,8 @@ def sync_blocked_emails(batch_size=BATCH_SIZE, **kw):

next(csv_suppression_list)

count = 0

while True:
batch = list(itertools.islice(csv_suppression_list, batch_size))

Expand All @@ -139,6 +146,9 @@ def sync_blocked_emails(batch_size=BATCH_SIZE, **kw):

email_blocks = [SuppressedEmail(email=record[3]) for record in batch]
SuppressedEmail.objects.bulk_create(email_blocks, ignore_conflicts=True)
count += len(batch)

task_log.info(f'synced {count:,} suppressed emails.')


@task(autoretry_for=(HTTPError, Timeout), max_retries=5, retry_backoff=True)
Expand Down

0 comments on commit 6db88f3

Please sign in to comment.