1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Healthcheck do
7 Module collects metrics about app and assign healthy status.
9 alias Pleroma.Healthcheck
13 defstruct pool_size: 0,
20 @type t :: %__MODULE__{
21 pool_size: non_neg_integer(),
22 active: non_neg_integer(),
23 idle: non_neg_integer(),
24 memory_used: number(),
25 job_queue_stats: map(),
29 @spec system_info() :: t()
32 memory_used: Float.round(:erlang.memory(:total) / 1024 / 1024, 2)
35 |> assign_job_queue_stats()
39 defp assign_db_info(healthcheck) do
40 database = Pleroma.Config.get([Repo, :database])
43 "select state, count(pid) from pg_stat_activity where datname = '#{database}' group by state;"
45 result = Repo.query!(query)
46 pool_size = Pleroma.Config.get([Repo, :pool_size])
49 Enum.reduce(result.rows, %{active: 0, idle: 0}, fn [state, cnt], states ->
50 if state == "active" do
51 Map.put(states, :active, states.active + cnt)
53 Map.put(states, :idle, states.idle + cnt)
56 |> Map.put(:pool_size, pool_size)
58 Map.merge(healthcheck, db_info)
61 defp assign_job_queue_stats(healthcheck) do
62 stats = Pleroma.JobQueueMonitor.stats()
63 Map.put(healthcheck, :job_queue_stats, stats)
66 @spec check_health(Healthcheck.t()) :: Healthcheck.t()
67 def check_health(%{pool_size: pool_size, active: active} = check)
68 when active >= pool_size do
69 %{check | healthy: false}
72 def check_health(check), do: check