|
| 1 | +defmodule Admin.MailingWorker do |
| 2 | + @moduledoc """ |
| 3 | + Worker for sending batch emails to a target audience with internationalisation. |
| 4 | + """ |
| 5 | + |
| 6 | + use Oban.Worker, queue: :mailing |
| 7 | + |
| 8 | + alias Admin.Accounts |
| 9 | + alias Admin.Accounts.Scope |
| 10 | + alias Admin.Accounts.UserNotifier |
| 11 | + alias Admin.Notifications |
| 12 | + |
| 13 | + @impl Oban.Worker |
| 14 | + def perform(%Oban.Job{ |
| 15 | + args: |
| 16 | + %{ |
| 17 | + "user_id" => user_id, |
| 18 | + "notification_id" => notification_id |
| 19 | + } = |
| 20 | + _args |
| 21 | + }) do |
| 22 | + user = Accounts.get_user!(user_id) |
| 23 | + scope = Scope.for_user(user) |
| 24 | + |
| 25 | + with {:ok, notification} <- Notifications.get_notification(scope, notification_id), |
| 26 | + included_langs = notification.localized_emails |> Enum.map(& &1.language), |
| 27 | + {:ok, audience} <- |
| 28 | + Notifications.get_target_audience( |
| 29 | + scope, |
| 30 | + notification.audience, |
| 31 | + if(notification.use_strict_languages, do: [only_langs: included_langs], else: []) |
| 32 | + ) do |
| 33 | + # save number of recipients to the notification |
| 34 | + Notifications.update_recipients(notification, %{total_recipients: length(audience)}) |
| 35 | + # start sending emails |
| 36 | + send_emails(scope, notification, audience) |
| 37 | + # await email progress messages |
| 38 | + await_emails(scope, notification) |
| 39 | + else |
| 40 | + {:error, :notification_not_found} -> |
| 41 | + {:cancel, :notification_not_found} |
| 42 | + |
| 43 | + {:error, error} -> |
| 44 | + {:error, "Failed to send notification: #{inspect(error)}"} |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + defp send_emails(scope, notification, audience) do |
| 49 | + job_pid = self() |
| 50 | + |
| 51 | + Task.async(fn -> |
| 52 | + audience |
| 53 | + |> Enum.with_index(1) |
| 54 | + |> Enum.each(fn {user, index} -> |
| 55 | + send_local_email(scope, user, notification) |
| 56 | + |
| 57 | + current_progress = trunc(index / length(audience) * 100) |
| 58 | + |
| 59 | + send(job_pid, {:progress, current_progress}) |
| 60 | + |
| 61 | + :timer.sleep(1000) |
| 62 | + end) |
| 63 | + |
| 64 | + send(job_pid, {:completed}) |
| 65 | + end) |
| 66 | + end |
| 67 | + |
| 68 | + defp send_local_email(scope, user, notification) do |
| 69 | + # get the localized email |
| 70 | + case Notifications.get_local_email_from_notification(notification, user.lang) do |
| 71 | + nil -> |
| 72 | + :skipped |
| 73 | + |
| 74 | + localized_email -> |
| 75 | + # deliver the email |
| 76 | + UserNotifier.deliver_call_to_action( |
| 77 | + user, |
| 78 | + localized_email.subject, |
| 79 | + localized_email.message, |
| 80 | + localized_email.button_text, |
| 81 | + localized_email.button_url |
| 82 | + ) |
| 83 | + |
| 84 | + # save message log |
| 85 | + Notifications.save_log( |
| 86 | + scope, |
| 87 | + %{ |
| 88 | + email: user.email, |
| 89 | + status: "sent" |
| 90 | + }, |
| 91 | + notification |
| 92 | + ) |
| 93 | + |
| 94 | + :ok |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + defp await_emails(scope, notification) do |
| 99 | + receive do |
| 100 | + {:progress, percent} -> |
| 101 | + Notifications.report_sending_progress(scope, {:progress, notification.name, percent}) |
| 102 | + await_emails(scope, notification) |
| 103 | + |
| 104 | + {:completed} -> |
| 105 | + Notifications.report_sending_progress(scope, {:completed, notification.name}) |
| 106 | + |
| 107 | + {:failed} -> |
| 108 | + Notifications.report_sending_progress(scope, {:failed, notification.name}) |
| 109 | + after |
| 110 | + 30_000 -> |
| 111 | + Notifications.report_sending_progress(scope, {:failed, notification.name}) |
| 112 | + raise RuntimeError, "no progress after 30s" |
| 113 | + end |
| 114 | + end |
| 115 | +end |
0 commit comments