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.Config.TransferTask do
15 if Pleroma.Config.get(:env) == :test, do: Ecto.Adapters.SQL.Sandbox.checkin(Repo)
19 def load_and_update_env do
20 with true <- Pleroma.Config.get(:configurable_from_database),
21 true <- Ecto.Adapters.SQL.table_exists?(Repo, "config"),
22 started_applications <- Application.started_applications() do
23 # We need to restart applications for loaded settings take effect
26 |> Enum.map(&update_env(&1))
28 # TODO: some problem with prometheus after restart!
29 |> Enum.reject(&(&1 in [:pleroma, nil, :prometheus]))
30 |> Enum.each(&restart(started_applications, &1))
34 defp update_env(setting) do
36 key = ConfigDB.from_string(setting.key)
37 group = ConfigDB.from_string(setting.group)
38 value = ConfigDB.from_binary(setting.value)
40 if group != :phoenix and key != :serve_endpoints do
41 default = Pleroma.Config.Holder.config(group, key)
44 if can_be_merged?(default, value) do
45 DeepMerge.deep_merge(default, value)
50 :ok = Application.put_env(group, key, merged_value)
52 if group != :logger do
55 # change logger configuration in runtime, without restart
56 if Keyword.keyword?(merged_value) and
57 key not in [:compile_time_application, :backends, :compile_time_purge_matching] do
58 Logger.configure_backend(key, merged_value)
60 Logger.configure([{key, merged_value}])
69 "updating env causes error, group: #{inspect(setting.group)}, key: #{
71 }, value: #{inspect(ConfigDB.from_binary(setting.value))}, error: #{inspect(e)}"
78 defp restart(started_applications, app) do
79 with {^app, _, _} <- List.keyfind(started_applications, app, 0),
80 :ok <- Application.stop(app) do
81 :ok = Application.start(app)
83 nil -> Logger.warn("#{app} is not started.")
84 error -> Logger.warn(inspect(error))
88 defp can_be_merged?(val1, val2) when is_map(val1) and is_map(val2), do: true
90 defp can_be_merged?(val1, val2) when is_list(val1) and is_list(val2) do
91 Keyword.keyword?(val1) and Keyword.keyword?(val2)
94 defp can_be_merged?(_val1, _val2), do: false