diff --git a/config/config.exs b/config/config.exs index 6317b8e1..a83a43a8 100644 --- a/config/config.exs +++ b/config/config.exs @@ -35,6 +35,7 @@ config :banchan, mature_content_enabled?: true, git_rev: hash, git_tag: tag, + ops_email: "ops@banchan.art", oban_key_fingerprint: System.get_env("OBAN_KEY_FINGERPRINT"), oban_license_key: System.get_env("OBAN_LICENSE_KEY") diff --git a/lib/banchan/workers/fly_backup.ex b/lib/banchan/workers/fly_backup.ex index 60143072..117254e6 100644 --- a/lib/banchan/workers/fly_backup.ex +++ b/lib/banchan/workers/fly_backup.ex @@ -17,6 +17,8 @@ defmodule Banchan.Workers.FlyBackup do require Logger + alias Banchan.Workers.Mailer + @impl Oban.Worker def perform(%_{args: _}) do run_backup() @@ -119,9 +121,11 @@ defmodule Banchan.Workers.FlyBackup do end |> case do {:error, error} -> + notify_completed("Backup operation errored", "N/A", error) {:error, error} {output, 0} -> + notify_completed("Backup operation succeeded", 0, output) Logger.info("Backup succeeded: #{output}") :ok @@ -131,11 +135,25 @@ defmodule Banchan.Workers.FlyBackup do # For some reason, the backup command over fly ssh complains about # the handle being invalid, *after* already succeeding. # It only seems to happen when I test this on Windows, though. + notify_completed("Backup operation succeeded suspiciously?", code, output) Logger.warning("Backup succeeded with bad error code #{code}: #{output}") :ok else + notify_completed("Backup operation failed", code, output) {:error, "Backup failed with code #{code}: #{output}"} end end end + + defp notify_completed(title, code, output) do + Mailer.new_email( + Application.get_env(:banchan, :ops_email, "ops@banchan.art"), + title, + BanchanWeb.Email.Ops, + :backup_completed, + code: code, + output: output + ) + |> Mailer.deliver() + end end diff --git a/lib/banchan_web/controllers/email/ops.ex b/lib/banchan_web/controllers/email/ops.ex new file mode 100644 index 00000000..378bc9e6 --- /dev/null +++ b/lib/banchan_web/controllers/email/ops.ex @@ -0,0 +1,23 @@ +defmodule BanchanWeb.Email.Ops do + @moduledoc """ + Emails for ops-related events. Typically sent to ops@banchan.art. + """ + use BanchanWeb, :html + + def render("backup_completed.html", assigns) do + ~F""" +

Backup completed with code {@code}.

+
+    {@output}
+    
+ """ + end + + def render("backup_completed.text", assigns) do + """ + Backup completed with code #{assigns.code}. + + #{assigns.output} + """ + end +end