Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send emails to ops@ whenever a backup completes/errors, for peace of … #793

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: "[email protected]",
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, "[email protected]"),
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 [email protected].
"""
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
Loading