-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
189 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
# The rules below have been manually copied from @commitlint/config-conventional | ||
# and match the v1.0.0 specification: | ||
# https://www.conventionalcommits.org/en/v1.0.0/#specification | ||
# | ||
# You can remove them and uncomment the config below when the following issue is | ||
# fixed: https://github.com/conventional-changelog/commitlint/issues/613 | ||
# | ||
# extends: | ||
# - '@commitlint/config-conventional' | ||
rules: | ||
body-leading-blank: [1, always] | ||
body-max-line-length: [2, always, 100] | ||
footer-leading-blank: [1, always] | ||
footer-max-line-length: [2, always, 100] | ||
header-max-length: [2, always, 100] | ||
subject-case: | ||
- 2 | ||
- never | ||
- [sentence-case, start-case, pascal-case, upper-case] | ||
subject-empty: [2, never] | ||
subject-full-stop: [2, never, "."] | ||
type-case: [2, always, lower-case] | ||
type-empty: [2, never] | ||
type-enum: | ||
- 2 | ||
- always | ||
- [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
defmodule PenguinMemories.Release do | ||
Check warning on line 1 in lib/penguin_memories/release.ex GitHub Actions / Setup, Build, Test / test (1.16.3, 25.1)
|
||
@app :penguin_memories | ||
|
||
import Ecto.Query | ||
alias PenguinMemories.Repo | ||
|
||
def migrate do | ||
for repo <- repos() do | ||
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true)) | ||
end | ||
end | ||
|
||
def rollback(repo, version) do | ||
for r <- repos(), r == repo do | ||
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version)) | ||
end | ||
end | ||
|
||
def seconds_since_last_migration do | ||
Repo.one( | ||
from m in "schema_migrations", | ||
select: fragment("EXTRACT(EPOCH FROM age(NOW(), ?::timestamp))::BIGINT", m.inserted_at), | ||
order_by: [desc: m.inserted_at], | ||
limit: 1 | ||
) | ||
end | ||
|
||
def health_check do | ||
repos = Application.fetch_env!(@app, :ecto_repos) | ||
|
||
migrations = | ||
repos | ||
|> Enum.map(&Ecto.Migrator.migrations/1) | ||
|> List.flatten() | ||
|> Enum.filter(fn | ||
{:up, _, _} -> false | ||
{_, _, _} -> true | ||
end) | ||
|
||
migrations = | ||
if Enum.empty?(migrations) do | ||
:ok | ||
else | ||
{:error, "Migrations pending: #{inspect(migrations)}"} | ||
end | ||
|
||
database = | ||
Enum.reduce_while(repos, :ok, fn item, _acc -> | ||
case Ecto.Adapters.SQL.query(item, "SELECT 1") do | ||
{:ok, %{num_rows: 1, rows: [[1]]}} -> | ||
{:cont, :ok} | ||
|
||
{:error, reason} -> | ||
{:halt, {:error, inspect(reason)}} | ||
end | ||
end) | ||
|
||
case {migrations, database} do | ||
{:ok, :ok} -> :ok | ||
{{:error, reason}, _} -> {:error, reason} | ||
{_, {:error, reason}} -> {:error, reason} | ||
end | ||
end | ||
|
||
defp repos do | ||
Application.ensure_all_started(:ssl) | ||
Application.load(@app) | ||
Application.fetch_env!(@app, :ecto_repos) | ||
end | ||
end |
17 changes: 17 additions & 0 deletions
17
lib/penguin_memories_web/controllers/healh_check_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule PenguinMemoriesWeb.HealthCheckController do | ||
use PenguinMemoriesWeb, :controller | ||
|
||
alias PenguinMemories.Release | ||
require Logger | ||
|
||
def index(conn, _params) do | ||
case Release.health_check() do | ||
:ok -> | ||
text(conn, "HEALTHY") | ||
|
||
{:error, reason} -> | ||
Logger.error("health check error: #{reason}") | ||
conn |> put_status(500) |> text("ERROR") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters