diff --git a/lib/keila/mailings/schedule_worker.ex b/lib/keila/mailings/schedule_worker.ex index bc060065..efd10821 100644 --- a/lib/keila/mailings/schedule_worker.ex +++ b/lib/keila/mailings/schedule_worker.ex @@ -29,14 +29,17 @@ defmodule Keila.Mailings.ScheduleWorker do @impl true def perform(%Oban.Job{}) do Enum.reduce_while(1..@passes, :ok, fn _, _ -> - Repo.transaction(fn -> - if jobs_below_threshold?() do - schedule_recipients() - {:cont, :ok} - else - {:halt, :ok} - end - end) + {_, acc} = + Repo.transaction(fn -> + if jobs_below_threshold?() do + schedule_recipients() + {:cont, :ok} + else + {:halt, :ok} + end + end) + + acc end) end @@ -58,6 +61,7 @@ defmodule Keila.Mailings.ScheduleWorker do limit: @limit, lock: "FOR NO KEY UPDATE" ) + |> Repo.all() |> Enum.group_by(& &1.campaign_id) |> Enum.each(fn {campaign_id, recipients} -> insert_jobs(campaign_id, recipients) diff --git a/test/keila/mailings/mailings_campaign_test.exs b/test/keila/mailings/mailings_campaign_test.exs index d358f1c1..36000710 100644 --- a/test/keila/mailings/mailings_campaign_test.exs +++ b/test/keila/mailings/mailings_campaign_test.exs @@ -83,7 +83,7 @@ defmodule Keila.MailingsCampaignTest do assert :ok = Mailings.deliver_campaign(campaign.id) - Oban.drain_queue(queue: :periodic) + assert %{success: 1} = Oban.drain_queue(queue: :periodic) assert %{success: ^n, failure: 0} = Oban.drain_queue(queue: :mailer, with_scheduled: true) for _ <- 1..n do @@ -122,7 +122,7 @@ defmodule Keila.MailingsCampaignTest do assert :ok = Mailings.deliver_campaign(campaign.id) - Oban.drain_queue(queue: :periodic) + assert %{success: 1} = Oban.drain_queue(queue: :periodic) assert %{success: ^n_expected_sent, failure: 0, snoozed: ^n_expected_snoozed} = Oban.drain_queue(queue: :mailer, with_scheduled: true) @@ -161,7 +161,7 @@ defmodule Keila.MailingsCampaignTest do assert :ok = Mailings.deliver_campaign(campaign.id) - Oban.drain_queue(queue: :periodic) + assert %{success: 1} = Oban.drain_queue(queue: :periodic) assert %{success: ^n, failure: 0} = Oban.drain_queue(queue: :mailer, with_scheduled: true) for _ <- 1..n do @@ -186,7 +186,7 @@ defmodule Keila.MailingsCampaignTest do assert :ok = Mailings.deliver_campaign(campaign.id) - Oban.drain_queue(queue: :periodic) + assert %{success: 1} = Oban.drain_queue(queue: :periodic) assert %{success: ^n, failure: 0} = Oban.drain_queue(queue: :mailer, with_scheduled: true) for _ <- 1..n do @@ -308,7 +308,7 @@ defmodule Keila.MailingsCampaignTest do ) assert :ok = Mailings.deliver_campaign(campaign.id) - Oban.drain_queue(queue: :periodic) + assert %{success: 1} = Oban.drain_queue(queue: :periodic) assert %{success: 1, failure: 0} = Oban.drain_queue(queue: :mailer, with_scheduled: true) receive do @@ -343,7 +343,7 @@ defmodule Keila.MailingsCampaignTest do assert :ok = Mailings.deliver_campaign(campaign.id) - Oban.drain_queue(queue: :periodic) + assert %{success: 1} = Oban.drain_queue(queue: :periodic) assert %{success: 1, cancelled: 1} = Oban.drain_queue(queue: :mailer, with_scheduled: true) assert %{status: :sent} = Mailings.get_campaign_stats(campaign.id) end diff --git a/test/keila_web/controllers/campaign_controller_test.exs b/test/keila_web/controllers/campaign_controller_test.exs index 38f11ea0..2678803a 100644 --- a/test/keila_web/controllers/campaign_controller_test.exs +++ b/test/keila_web/controllers/campaign_controller_test.exs @@ -301,7 +301,7 @@ defmodule KeilaWeb.CampaignControllerTest do Oban.drain_queue(queue: :periodic) Oban.drain_queue(queue: :mailer, with_scheduled: true) - :timer.sleep(500) + :timer.sleep(1500) assert render(lv) =~ "
emails sent
10
"