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.Config.TransferTask do
14 @type env() :: :test | :benchmark | :dev | :prod
17 {:pleroma, :hackney_pools},
20 {:pleroma, :rate_limit},
22 {:pleroma, :streamer},
24 {:pleroma, :connections_pool}
27 @reboot_time_subkeys [
28 {:pleroma, Pleroma.Captcha, [:seconds_valid]},
29 {:pleroma, Pleroma.Upload, [:proxy_remote]},
30 {:pleroma, :instance, [:upload_limit]},
31 {:pleroma, :gopher, [:enabled]}
34 def start_link(restart_pleroma? \\ true) do
35 load_and_update_env([], restart_pleroma?)
36 if Config.get(:env) == :test, do: Ecto.Adapters.SQL.Sandbox.checkin(Repo)
40 @spec load_and_update_env([ConfigDB.t()], boolean()) :: :ok
41 def load_and_update_env(deleted_settings \\ [], restart_pleroma? \\ true) do
42 with {_, true} <- {:configurable, Config.get(:configurable_from_database)} do
43 # We need to restart applications for loaded settings take effect
46 (Repo.all(ConfigDB) ++ deleted_settings)
47 |> Enum.map(&merge_with_default/1)
48 |> Enum.split_with(fn {group, _, _, _} -> group in [:logger, :quack] end)
52 |> Enum.each(&configure/1)
54 started_applications = Application.started_applications()
56 # TODO: some problem with prometheus after restart!
57 reject = [nil, :prometheus, :postgrex]
60 if restart_pleroma? do
67 |> Enum.map(&update/1)
69 |> Enum.reject(&(&1 in reject))
70 |> maybe_set_pleroma_last()
71 |> Enum.each(&restart(started_applications, &1, Config.get(:env)))
75 {:configurable, false} -> Restarter.Pleroma.rebooted()
79 defp maybe_set_pleroma_last(apps) do
80 # to be ensured that pleroma will be restarted last
81 if :pleroma in apps do
83 |> List.delete(:pleroma)
84 |> List.insert_at(-1, :pleroma)
86 Restarter.Pleroma.rebooted()
91 defp merge_with_default(%{group: group, key: key, value: value} = setting) do
92 default = Config.Holder.default_config(group, key)
96 Ecto.get_meta(setting, :state) == :deleted -> default
97 can_be_merged?(default, value) -> ConfigDB.merge_group(group, key, default, value)
101 {group, key, value, merged}
104 # change logger configuration in runtime, without restart
105 defp configure({:quack, key, _, merged}) do
106 Logger.configure_backend(Quack.Logger, [{key, merged}])
107 :ok = update_env(:quack, key, merged)
110 defp configure({_, :backends, _, merged}) do
111 # removing current backends
112 Enum.each(Application.get_env(:logger, :backends), &Logger.remove_backend/1)
114 Enum.each(merged, &Logger.add_backend/1)
116 :ok = update_env(:logger, :backends, merged)
119 defp configure({_, key, _, merged}) when key in [:console, :ex_syslogger] do
121 if key == :console do
122 put_in(merged[:format], merged[:format] <> "\n")
128 if key == :ex_syslogger,
129 do: {ExSyslogger, :ex_syslogger},
132 Logger.configure_backend(backend, merged)
133 :ok = update_env(:logger, key, merged)
136 defp configure({_, key, _, merged}) do
137 Logger.configure([{key, merged}])
138 :ok = update_env(:logger, key, merged)
141 defp update({group, key, value, merged}) do
143 :ok = update_env(group, key, merged)
145 if group != :pleroma or pleroma_need_restart?(group, key, value), do: group
149 "updating env causes error, group: #{inspect(group)}, key: #{inspect(key)}, value: #{
151 } 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