From 56828abf6de81a52079b26cf899b91fdd822f2ce Mon Sep 17 00:00:00 2001 From: rinpatch Date: Wed, 11 Sep 2019 23:04:01 +0300 Subject: Use Jason for rendering responses Although Jason readme says Phoenix 1.4+ already does it by default, [it actually does it only for new projects](https://github.com/phoenixframework/phoenix/blob/3bfb9f6e900c9a2e31cb95736e2cb5bdad329b61/lib/phoenix.ex#L58-L59) --- config/config.exs | 2 ++ lib/healthcheck.ex | 64 --------------------------------------------- lib/pleroma/healthcheck.ex | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 64 deletions(-) delete mode 100644 lib/healthcheck.ex create mode 100644 lib/pleroma/healthcheck.ex diff --git a/config/config.exs b/config/config.exs index 5206fe375..0e91df886 100644 --- a/config/config.exs +++ b/config/config.exs @@ -373,6 +373,8 @@ config :pleroma, :chat, enabled: true config :phoenix, :format_encoders, json: Jason +config :phoenix, :json_library, Jason + config :pleroma, :gopher, enabled: false, ip: {0, 0, 0, 0}, diff --git a/lib/healthcheck.ex b/lib/healthcheck.ex deleted file mode 100644 index f97d14432..000000000 --- a/lib/healthcheck.ex +++ /dev/null @@ -1,64 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2019 Pleroma Authors -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Healthcheck do - @moduledoc """ - Module collects metrics about app and assign healthy status. - """ - alias Pleroma.Healthcheck - alias Pleroma.Repo - - defstruct pool_size: 0, - active: 0, - idle: 0, - memory_used: 0, - healthy: true - - @type t :: %__MODULE__{ - pool_size: non_neg_integer(), - active: non_neg_integer(), - idle: non_neg_integer(), - memory_used: number(), - healthy: boolean() - } - - @spec system_info() :: t() - def system_info do - %Healthcheck{ - memory_used: Float.round(:erlang.memory(:total) / 1024 / 1024, 2) - } - |> assign_db_info() - |> check_health() - end - - defp assign_db_info(healthcheck) do - database = Pleroma.Config.get([Repo, :database]) - - query = - "select state, count(pid) from pg_stat_activity where datname = '#{database}' group by state;" - - result = Repo.query!(query) - pool_size = Pleroma.Config.get([Repo, :pool_size]) - - db_info = - Enum.reduce(result.rows, %{active: 0, idle: 0}, fn [state, cnt], states -> - if state == "active" do - Map.put(states, :active, states.active + cnt) - else - Map.put(states, :idle, states.idle + cnt) - end - end) - |> Map.put(:pool_size, pool_size) - - Map.merge(healthcheck, db_info) - end - - @spec check_health(Healthcheck.t()) :: Healthcheck.t() - def check_health(%{pool_size: pool_size, active: active} = check) - when active >= pool_size do - %{check | healthy: false} - end - - def check_health(check), do: check -end diff --git a/lib/pleroma/healthcheck.ex b/lib/pleroma/healthcheck.ex new file mode 100644 index 000000000..977b78c26 --- /dev/null +++ b/lib/pleroma/healthcheck.ex @@ -0,0 +1,65 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Healthcheck do + @moduledoc """ + Module collects metrics about app and assign healthy status. + """ + alias Pleroma.Healthcheck + alias Pleroma.Repo + + @derive Jason.Encoder + defstruct pool_size: 0, + active: 0, + idle: 0, + memory_used: 0, + healthy: true + + @type t :: %__MODULE__{ + pool_size: non_neg_integer(), + active: non_neg_integer(), + idle: non_neg_integer(), + memory_used: number(), + healthy: boolean() + } + + @spec system_info() :: t() + def system_info do + %Healthcheck{ + memory_used: Float.round(:erlang.memory(:total) / 1024 / 1024, 2) + } + |> assign_db_info() + |> check_health() + end + + defp assign_db_info(healthcheck) do + database = Pleroma.Config.get([Repo, :database]) + + query = + "select state, count(pid) from pg_stat_activity where datname = '#{database}' group by state;" + + result = Repo.query!(query) + pool_size = Pleroma.Config.get([Repo, :pool_size]) + + db_info = + Enum.reduce(result.rows, %{active: 0, idle: 0}, fn [state, cnt], states -> + if state == "active" do + Map.put(states, :active, states.active + cnt) + else + Map.put(states, :idle, states.idle + cnt) + end + end) + |> Map.put(:pool_size, pool_size) + + Map.merge(healthcheck, db_info) + end + + @spec check_health(Healthcheck.t()) :: Healthcheck.t() + def check_health(%{pool_size: pool_size, active: active} = check) + when active >= pool_size do + %{check | healthy: false} + end + + def check_health(check), do: check +end -- cgit v1.2.3