Skip to content

Commit

Permalink
send emails to ops@ whenever a backup completes/errors, for peace of …
Browse files Browse the repository at this point in the history
…mind (#793)
  • Loading branch information
zkat authored Mar 31, 2024
1 parent 932aa06 commit b97a450
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
18 changes: 18 additions & 0 deletions lib/banchan/workers/fly_backup.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ defmodule Banchan.Workers.FlyBackup do

require Logger

alias Banchan.Workers.Mailer

@impl Oban.Worker
def perform(%_{args: _}) do
run_backup()
Expand Down Expand Up @@ -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

Expand All @@ -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
23 changes: 23 additions & 0 deletions lib/banchan_web/controllers/email/ops.ex
Original file line number Diff line number Diff line change
@@ -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"""
<h1>Backup completed with code {@code}.</h1>
<pre>
{@output}
</pre>
"""
end

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

0 comments on commit b97a450

Please sign in to comment.