1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Config.TransferTask do
14 @type env() :: :test | :benchmark | :dev | :prod
16 defp reboot_time_keys,
18 {:pleroma, :hackney_pools},
21 {:pleroma, :rate_limit},
23 {:pleroma, :streamer},
25 {:pleroma, :connections_pool}
28 defp reboot_time_subkeys,
30 {:pleroma, Pleroma.Captcha, [:seconds_valid]},
31 {:pleroma, Pleroma.Upload, [:proxy_remote]},
32 {:pleroma, :instance, [:upload_limit]},
33 {:pleroma, :gopher, [:enabled]}
36 def start_link(restart_pleroma? \\ true) do
37 load_and_update_env([], restart_pleroma?)
38 if Config.get(:env) == :test, do: Ecto.Adapters.SQL.Sandbox.checkin(Repo)
42 @spec load_and_update_env([ConfigDB.t()], boolean()) :: :ok
43 def load_and_update_env(deleted_settings \\ [], restart_pleroma? \\ true) do
44 with {_, true} <- {:configurable, Config.get(:configurable_from_database)} do
45 # We need to restart applications for loaded settings take effect
48 (Repo.all(ConfigDB) ++ deleted_settings)
49 |> Enum.map(&merge_with_default/1)
50 |> Enum.split_with(fn {group, _, _, _} -> group in [:logger, :quack] end)
54 |> Enum.each(&configure/1)
56 started_applications = Application.started_applications()
58 # TODO: some problem with prometheus after restart!
59 reject = [nil, :prometheus, :postgrex]
62 if restart_pleroma? do
69 |> Enum.map(&update/1)
71 |> Enum.reject(&(&1 in reject))
72 |> maybe_set_pleroma_last()
73 |> Enum.each(&restart(started_applications, &1, Config.get(:env)))
77 {:configurable, false} -> Restarter.Pleroma.rebooted()
81 defp maybe_set_pleroma_last(apps) do
82 # to be ensured that pleroma will be restarted last
83 if :pleroma in apps do
85 |> List.delete(:pleroma)
86 |> List.insert_at(-1, :pleroma)
88 Restarter.Pleroma.rebooted()
93 defp merge_with_default(%{group: group, key: key, value: value} = setting) do
94 default = Config.Holder.default_config(group, key)
98 Ecto.get_meta(setting, :state) == :deleted -> default
99 can_be_merged?(default, value) -> ConfigDB.merge_group(group, key, default, value)
103 {group, key, value, merged}
106 # change logger configuration in runtime, without restart
107 defp configure({:quack, key, _, merged}) do
108 Logger.configure_backend(Quack.Logger, [{key, merged}])
109 :ok = update_env(:quack, key, merged)
112 defp configure({_, :backends, _, merged}) do
113 # removing current backends
114 Enum.each(Application.get_env(:logger, :backends), &Logger.remove_backend/1)
116 Enum.each(merged, &Logger.add_backend/1)
118 :ok = update_env(:logger, :backends, merged)
121 defp configure({_, key, _, merged}) when key in [:console, :ex_syslogger] do
123 if key == :console do
124 put_in(merged[:format], merged[:format] <> "\n")
130 if key == :ex_syslogger,
131 do: {ExSyslogger, :ex_syslogger},
134 Logger.configure_backend(backend, merged)
135 :ok = update_env(:logger, key, merged)
138 defp configure({_, key, _, merged}) do
139 Logger.configure([{key, merged}])
140 :ok = update_env(:logger, key, merged)
143 defp update({group, key, value, merged}) do
145 :ok = update_env(group, key, merged)
147 if group != :pleroma or pleroma_need_restart?(group, key, value), do: group
151 "updating env causes error, group: #{inspect(group)}, key: #{inspect(key)}, value: #{inspect(value)} error: #{inspect(error)}"
153 Logger.warn(error_msg)
159 defp update_env(group, key, nil), do: Application.delete_env(group, key)
160 defp update_env(group, key, value), do: Application.put_env(group, key, value)
162 @spec pleroma_need_restart?(atom(), atom(), any()) :: boolean()
163 def pleroma_need_restart?(group, key, value) do
164 group_and_key_need_reboot?(group, key) or group_and_subkey_need_reboot?(group, key, value)
167 defp group_and_key_need_reboot?(group, key) do
168 Enum.any?(reboot_time_keys(), fn {g, k} -> g == group and k == key end)
171 defp group_and_subkey_need_reboot?(group, key, value) do
172 Keyword.keyword?(value) and
173 Enum.any?(reboot_time_subkeys(), fn {g, k, subkeys} ->
174 g == group and k == key and
175 Enum.any?(Keyword.keys(value), &(&1 in subkeys))
179 defp restart(_, :pleroma, env), do: Restarter.Pleroma.restart_after_boot(env)
181 defp restart(started_applications, app, _) do
182 with {^app, _, _} <- List.keyfind(started_applications, app, 0),
183 :ok <- Application.stop(app) do
184 :ok = Application.start(app)
187 Logger.warn("#{app} is not started.")
196 defp can_be_merged?(val1, val2) when is_list(val1) and is_list(val2) do
197 Keyword.keyword?(val1) and Keyword.keyword?(val2)
200 defp can_be_merged?(_val1, _val2), do: false