1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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
12 defstruct pool_size: 0,
18 @type t :: %__MODULE__{
19 pool_size: non_neg_integer(),
20 active: non_neg_integer(),
21 idle: non_neg_integer(),
22 memory_used: number(),
26 @spec system_info() :: t()
29 memory_used: Float.round(:erlang.memory(:total) / 1024 / 1024, 2)
35 defp assign_db_info(healthcheck) do
36 database = Pleroma.Config.get([Repo, :database])
39 "select state, count(pid) from pg_stat_activity where datname = '#{database}' group by state;"
41 result = Repo.query!(query)
42 pool_size = Pleroma.Config.get([Repo, :pool_size])
45 Enum.reduce(result.rows, %{active: 0, idle: 0}, fn [state, cnt], states ->
46 if state == "active" do
47 Map.put(states, :active, states.active + cnt)
49 Map.put(states, :idle, states.idle + cnt)
52 |> Map.put(:pool_size, pool_size)
54 Map.merge(healthcheck, db_info)
57 @spec check_health(Healthcheck.t()) :: Healthcheck.t()
58 def check_health(%{pool_size: pool_size, active: active} = check)
59 when active >= pool_size do
60 %{check | healthy: false}
63 def check_health(check), do: check