Send import failed email when job errors

This commit is contained in:
Uku Taht 2022-03-22 11:59:30 +02:00
parent 1fb39957a6
commit f42b694f15
2 changed files with 20 additions and 12 deletions

View File

@ -14,11 +14,14 @@ defmodule ErrorReporter do
Sentry.capture_exception(meta.error, stacktrace: meta.stacktrace, extra: meta) Sentry.capture_exception(meta.error, stacktrace: meta.stacktrace, extra: meta)
end end
defp maybe_log_import_error(job) do defp maybe_log_import_error(%Oban.Job{
site = Plausible.Repo.get(Plausible.Site, job.args["site_id"]) queue: "google_analytics_imports",
args: %{"site_id" => site_id}
}) do
site = Plausible.Repo.get(Plausible.Site, site_id)
if site do if site do
Plausible.Site.import_failure(site) |> Plausible.Repo.update!() Plausible.Workers.ImportGoogleAnalytics.import_failed(site)
end end
end end
end end

View File

@ -30,8 +30,16 @@ defmodule Plausible.Workers.ImportGoogleAnalytics do
:ok :ok
{:error, error} -> {:error, error} ->
Plausible.Site.import_failure(site) import_failed(site)
|> Repo.update!()
{: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 -> Enum.each(site.memberships, fn membership ->
if membership.role in [:owner, :admin] do if membership.role in [:owner, :admin] do
@ -39,8 +47,5 @@ defmodule Plausible.Workers.ImportGoogleAnalytics do
|> Plausible.Mailer.send_email_safe() |> Plausible.Mailer.send_email_safe()
end end
end) end)
{:error, error}
end
end end
end end