Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add management command for sync suppressed emails #21784

Merged
merged 1 commit into from
Feb 1, 2024
Merged

Conversation

KevinMind
Copy link
Contributor

@KevinMind KevinMind commented Feb 1, 2024

relates to: mozilla/addons#9426

Description

This pull request adds a new management command for syncing suppressed emails from Socket Labs to the database. The command fetches the suppression list from Socket Labs, downloads it, and then bulk creates the suppressed emails in the database. The pull request also includes a task for sending suppressed email confirmations.

Context

This PR exposes the sync_blocked_emails task via a management command.

This can be useful for a few reasons:

  • testing the email suppression feature will likely require manually adding emails to the suppression list and then syncing them to our DB so we can run through the verification steps to unsuppress the email.
  • in general if we need, for operational reasons, to resync the suppression list we can do so via an easy to execute command.

In general the sync_blocked_emails task will be run periodically. This is not in production yet as we have only just recently been given the necessary credentials to communicate with socketlabs via API and having that job executing until now would have simply erred indefinitely.

"""Sync Socket labs suppression list to database."""

def handle(self, *args, **options):
sync_blocked_emails.apply()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using apply might be the right thing here. Then the task executes immediately, and logs are printed to the console where the management command is executed.

Happy to hear feedback on that but it felt right when executing locally.

@KevinMind KevinMind requested review from a team and eviljeff and removed request for a team February 1, 2024 08:54
Copy link
Member

@eviljeff eviljeff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good so far but needs issue to compare against to see if it satisfies.

@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we want some logging every batch to show progress?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's necessary, at least not yet. Only added the logging here because the download part can take some time to run and without any logs it can be confusing and made me wonder if something was wrong with the code. I added the count at the end to just verify that we have synced "something". The syncing is really fast due to the batching so it wouldn't add much value IMO.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue might explain further, but if this is something that is expected to be run as a cron, with no args or options, it would probably be better implemented as a simple function in cron.py instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is a simple convenience for testing and if we ever want to manually resync the suppression list. We should run this task on a periodic schedule. The issue will likely not explain much unfortunately.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do that with a simple function in cron.py too 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I trigger a function in cron in both a management command and periodically? Just curious? Also would there be retry logic if implemented that way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in crontab.tpl we execute both management commands directly, and simple functions via the cron management command.

There is no retry logic in our cron setup (other that it'd be executed again the time it was scheduled). The cron.py function vs. a separate management command .py file is just about the initial execution - in both cases you'd have to either write some custom retry logic, or wrap in a task and set a retry on the task.

@KevinMind KevinMind merged commit 6db88f3 into master Feb 1, 2024
10 checks passed
@KevinMind KevinMind deleted the ADDSRV-648 branch February 1, 2024 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants