fix for logger configuration through admin-fe
[akkoma] / lib / pleroma / config / transfer_task.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Config.TransferTask do
6 use Task
7
8 alias Pleroma.Config
9 alias Pleroma.ConfigDB
10 alias Pleroma.Repo
11
12 require Logger
13
14 @type env() :: :test | :benchmark | :dev | :prod
15
16 @reboot_time_keys [
17 {:pleroma, :hackney_pools},
18 {:pleroma, :chat},
19 {:pleroma, Oban},
20 {:pleroma, :rate_limit},
21 {:pleroma, :markup},
22 {:pleroma, :streamer},
23 {:pleroma, :pools},
24 {:pleroma, :connections_pool}
25 ]
26
27 @reboot_time_subkeys [
28 {:pleroma, Pleroma.Captcha, [:seconds_valid]},
29 {:pleroma, Pleroma.Upload, [:proxy_remote]},
30 {:pleroma, :instance, [:upload_limit]},
31 {:pleroma, :email_notifications, [:digest]},
32 {:pleroma, :oauth2, [:clean_expired_tokens]},
33 {:pleroma, Pleroma.ActivityExpiration, [:enabled]},
34 {:pleroma, Pleroma.ScheduledActivity, [:enabled]},
35 {:pleroma, :gopher, [:enabled]}
36 ]
37
38 def start_link(_) do
39 load_and_update_env()
40 if Config.get(:env) == :test, do: Ecto.Adapters.SQL.Sandbox.checkin(Repo)
41 :ignore
42 end
43
44 @spec load_and_update_env([ConfigDB.t()], boolean()) :: :ok
45 def load_and_update_env(deleted_settings \\ [], restart_pleroma? \\ true) do
46 with {_, true} <- {:configurable, Config.get(:configurable_from_database)} do
47 # We need to restart applications for loaded settings take effect
48
49 # TODO: some problem with prometheus after restart!
50 reject_restart =
51 if restart_pleroma? do
52 [nil, :prometheus]
53 else
54 [:pleroma, nil, :prometheus]
55 end
56
57 {logger, other} =
58 (Repo.all(ConfigDB) ++ deleted_settings)
59 |> Enum.map(&transform_and_merge/1)
60 |> Enum.split_with(fn {group, _, _, _} -> group in [:logger, :quack] end)
61
62 logger
63 |> Enum.sort()
64 |> Enum.each(&configure/1)
65
66 started_applications = Application.started_applications()
67
68 other
69 |> Enum.map(&update/1)
70 |> Enum.uniq()
71 |> Enum.reject(&(&1 in reject_restart))
72 |> maybe_set_pleroma_last()
73 |> Enum.each(&restart(started_applications, &1, Config.get(:env)))
74
75 :ok
76 else
77 {:configurable, false} -> Restarter.Pleroma.rebooted()
78 end
79 end
80
81 defp maybe_set_pleroma_last(apps) do
82 # to be ensured that pleroma will be restarted last
83 if :pleroma in apps do
84 apps
85 |> List.delete(:pleroma)
86 |> List.insert_at(-1, :pleroma)
87 else
88 Restarter.Pleroma.rebooted()
89 apps
90 end
91 end
92
93 defp transform_and_merge(%{group: group, key: key, value: value} = setting) do
94 group = ConfigDB.from_string(group)
95 key = ConfigDB.from_string(key)
96 value = ConfigDB.from_binary(value)
97
98 default = Config.Holder.default_config(group, key)
99
100 merged =
101 cond do
102 Ecto.get_meta(setting, :state) == :deleted -> default
103 can_be_merged?(default, value) -> ConfigDB.merge_group(group, key, default, value)
104 true -> value
105 end
106
107 {group, key, value, merged}
108 end
109
110 # change logger configuration in runtime, without restart
111 defp configure({:quack, key, _, merged}) do
112 Logger.configure_backend(Quack.Logger, [{key, merged}])
113 :ok = update_env(:quack, key, merged)
114 end
115
116 defp configure({_, :backends, _, merged}) do
117 # removing current backends
118 Enum.each(Application.get_env(:logger, :backends), &Logger.remove_backend/1)
119
120 Enum.each(merged, &Logger.add_backend/1)
121
122 :ok = update_env(:logger, :backends, merged)
123 end
124
125 defp configure({group, key, _, merged}) do
126 merged =
127 if key == :console do
128 put_in(merged[:format], merged[:format] <> "\n")
129 else
130 merged
131 end
132
133 backend =
134 if key == :ex_syslogger,
135 do: {ExSyslogger, :ex_syslogger},
136 else: key
137
138 Logger.configure_backend(backend, merged)
139 :ok = update_env(:logger, group, merged)
140 end
141
142 defp update({group, key, value, merged}) do
143 try do
144 :ok = update_env(group, key, merged)
145
146 if group != :pleroma or pleroma_need_restart?(group, key, value), do: group
147 rescue
148 error ->
149 error_msg =
150 "updating env causes error, group: #{inspect(group)}, key: #{inspect(key)}, value: #{
151 inspect(value)
152 } error: #{inspect(error)}"
153
154 Logger.warn(error_msg)
155
156 nil
157 end
158 end
159
160 defp update_env(group, key, nil), do: Application.delete_env(group, key)
161 defp update_env(group, key, value), do: Application.put_env(group, key, value)
162
163 @spec pleroma_need_restart?(atom(), atom(), any()) :: boolean()
164 def pleroma_need_restart?(group, key, value) do
165 group_and_key_need_reboot?(group, key) or group_and_subkey_need_reboot?(group, key, value)
166 end
167
168 defp group_and_key_need_reboot?(group, key) do
169 Enum.any?(@reboot_time_keys, fn {g, k} -> g == group and k == key end)
170 end
171
172 defp group_and_subkey_need_reboot?(group, key, value) do
173 Keyword.keyword?(value) and
174 Enum.any?(@reboot_time_subkeys, fn {g, k, subkeys} ->
175 g == group and k == key and
176 Enum.any?(Keyword.keys(value), &(&1 in subkeys))
177 end)
178 end
179
180 defp restart(_, :pleroma, env), do: Restarter.Pleroma.restart_after_boot(env)
181
182 defp restart(started_applications, app, _) do
183 with {^app, _, _} <- List.keyfind(started_applications, app, 0),
184 :ok <- Application.stop(app) do
185 :ok = Application.start(app)
186 else
187 nil ->
188 Logger.warn("#{app} is not started.")
189
190 error ->
191 error
192 |> inspect()
193 |> Logger.warn()
194 end
195 end
196
197 defp can_be_merged?(val1, val2) when is_list(val1) and is_list(val2) do
198 Keyword.keyword?(val1) and Keyword.keyword?(val2)
199 end
200
201 defp can_be_merged?(_val1, _val2), do: false
202 end