diff --git a/lib/telemetry.ex b/lib/telemetry.ex index ac1fd1d4e..961fe2993 100644 --- a/lib/telemetry.ex +++ b/lib/telemetry.ex @@ -14,11 +14,14 @@ defmodule ErrorReporter do Sentry.capture_exception(meta.error, stacktrace: meta.stacktrace, extra: meta) end - defp maybe_log_import_error(job) do - site = Plausible.Repo.get(Plausible.Site, job.args["site_id"]) + defp maybe_log_import_error(%Oban.Job{ + queue: "google_analytics_imports", + args: %{"site_id" => site_id} + }) do + site = Plausible.Repo.get(Plausible.Site, site_id) if site do - Plausible.Site.import_failure(site) |> Plausible.Repo.update!() + Plausible.Workers.ImportGoogleAnalytics.import_failed(site) end end end diff --git a/lib/workers/import_google_analytics.ex b/lib/workers/import_google_analytics.ex index 1387018a9..b313891e1 100644 --- a/lib/workers/import_google_analytics.ex +++ b/lib/workers/import_google_analytics.ex @@ -30,17 +30,22 @@ defmodule Plausible.Workers.ImportGoogleAnalytics do :ok {:error, error} -> - Plausible.Site.import_failure(site) - |> Repo.update!() - - Enum.each(site.memberships, fn membership -> - if membership.role in [:owner, :admin] do - PlausibleWeb.Email.import_failure(membership.user, site) - |> Plausible.Mailer.send_email_safe() - end - end) + import_failed(site) {:error, error} end end + + def import_failed(site) do + site = Repo.preload(site, memberships: :user) + + Plausible.Site.import_failure(site) |> Repo.update!() + + Enum.each(site.memberships, fn membership -> + if membership.role in [:owner, :admin] do + PlausibleWeb.Email.import_failure(membership.user, site) + |> Plausible.Mailer.send_email_safe() + end + end) + end end