Skip to content

Commit 83f9061

Browse files
committed
Separate liveness and readiness checks
1 parent 2694302 commit 83f9061

File tree

3 files changed

+63
-50
lines changed

3 files changed

+63
-50
lines changed

lib/plausible_web/controllers/api/external_controller.ex

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -47,54 +47,6 @@ defmodule PlausibleWeb.Api.ExternalController do
4747
send_resp(conn, 200, "")
4848
end
4949

50-
def health(conn, _params) do
51-
postgres_health =
52-
case Ecto.Adapters.SQL.query(Plausible.Repo, "SELECT 1", []) do
53-
{:ok, _} -> "ok"
54-
e -> "error: #{inspect(e)}"
55-
end
56-
57-
clickhouse_health =
58-
case Ecto.Adapters.SQL.query(Plausible.ClickhouseRepo, "SELECT 1", []) do
59-
{:ok, _} -> "ok"
60-
e -> "error: #{inspect(e)}"
61-
end
62-
63-
cache_health =
64-
if postgres_health == "ok" and Plausible.Site.Cache.ready?() and
65-
Plausible.Shield.IPRuleCache.ready?() do
66-
"ok"
67-
end
68-
69-
status =
70-
case {postgres_health, clickhouse_health, cache_health} do
71-
{"ok", "ok", "ok"} -> 200
72-
_ -> 500
73-
end
74-
75-
put_status(conn, status)
76-
|> json(%{
77-
postgres: postgres_health,
78-
clickhouse: clickhouse_health,
79-
sites_cache: cache_health
80-
})
81-
end
82-
83-
def info(conn, _params) do
84-
build =
85-
:plausible
86-
|> Application.get_env(:runtime_metadata)
87-
|> Keyword.take([:version, :commit, :created, :tags])
88-
|> Map.new()
89-
90-
geo_database = Plausible.Geo.database_type() || "(not configured)"
91-
92-
json(conn, %{
93-
geo_database: geo_database,
94-
build: build
95-
})
96-
end
97-
9850
defp find_first_invalid_changeset(dropped) do
9951
Enum.find_value(dropped, nil, fn dropped_event ->
10052
case dropped_event.drop_reason do
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
defmodule PlausibleWeb.Api.SystemController do
2+
use PlausibleWeb, :controller
3+
4+
def info(conn, _params) do
5+
build =
6+
:plausible
7+
|> Application.get_env(:runtime_metadata)
8+
|> Keyword.take([:version, :commit, :created, :tags])
9+
|> Map.new()
10+
11+
geo_database = Plausible.Geo.database_type() || "(not configured)"
12+
13+
json(conn, %{
14+
geo_database: geo_database,
15+
build: build
16+
})
17+
end
18+
19+
def liveness(conn, _params) do
20+
json(conn, %{ok: true})
21+
end
22+
23+
def readiness(conn, _params) do
24+
postgres_health =
25+
case Ecto.Adapters.SQL.query(Plausible.Repo, "SELECT 1", []) do
26+
{:ok, _} -> "ok"
27+
e -> "error: #{inspect(e)}"
28+
end
29+
30+
clickhouse_health =
31+
case Ecto.Adapters.SQL.query(Plausible.ClickhouseRepo, "SELECT 1", []) do
32+
{:ok, _} -> "ok"
33+
e -> "error: #{inspect(e)}"
34+
end
35+
36+
cache_health =
37+
if postgres_health == "ok" and Plausible.Site.Cache.ready?() and
38+
Plausible.Shield.IPRuleCache.ready?() do
39+
"ok"
40+
end
41+
42+
status =
43+
case {postgres_health, clickhouse_health, cache_health} do
44+
{"ok", "ok", "ok"} -> 200
45+
_ -> 500
46+
end
47+
48+
put_status(conn, status)
49+
|> json(%{
50+
postgres: postgres_health,
51+
clickhouse: clickhouse_health,
52+
sites_cache: cache_health
53+
})
54+
end
55+
end

lib/plausible_web/router.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,14 @@ defmodule PlausibleWeb.Router do
294294

295295
post "/event", Api.ExternalController, :event
296296
get "/error", Api.ExternalController, :error
297-
get "/health", Api.ExternalController, :health
298-
get "/system", Api.ExternalController, :info
297+
# Remove this once all external checks are migration to new /system/health/* checks
298+
get "/health", Api.SystemController, :liveness
299+
end
300+
301+
scope "/system" do
302+
get "/", Api.SystemController, :info
303+
get "/health/live", Api.SystemController, :liveness
304+
get "/health/ready", Api.SystemController, :readiness
299305
end
300306

301307
scope [] do

0 commit comments

Comments
 (0)