X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fconfig%2Ftransfer_task.ex;h=aad45aab89127ad6f59d3b1147adbd2d8cd5aedc;hb=5856f51717c12f4c6b0b89e480ff689c8480393d;hp=7c3449b5e0e06ac27f677dcc74dfca0477caa322;hpb=3c2c32b460c2d942d085725d14e77a96c4a01e4c;p=akkoma
diff --git a/lib/pleroma/config/transfer_task.ex b/lib/pleroma/config/transfer_task.ex
index 7c3449b5e..aad45aab8 100644
--- a/lib/pleroma/config/transfer_task.ex
+++ b/lib/pleroma/config/transfer_task.ex
@@ -1,10 +1,11 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors
+# Copyright © 2017-2021 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Config.TransferTask do
use Task
+ alias Pleroma.Config
alias Pleroma.ConfigDB
alias Pleroma.Repo
@@ -18,59 +19,56 @@ defmodule Pleroma.Config.TransferTask do
{:pleroma, Oban},
{:pleroma, :rate_limit},
{:pleroma, :markup},
- {:plerome, :streamer}
+ {:pleroma, :streamer},
+ {:pleroma, :pools},
+ {:pleroma, :connections_pool}
]
@reboot_time_subkeys [
{:pleroma, Pleroma.Captcha, [:seconds_valid]},
{:pleroma, Pleroma.Upload, [:proxy_remote]},
{:pleroma, :instance, [:upload_limit]},
- {:pleroma, :email_notifications, [:digest]},
- {:pleroma, :oauth2, [:clean_expired_tokens]},
- {:pleroma, Pleroma.ActivityExpiration, [:enabled]},
- {:pleroma, Pleroma.ScheduledActivity, [:enabled]},
{:pleroma, :gopher, [:enabled]}
]
- @reject [nil, :prometheus]
-
- def start_link(_) do
- load_and_update_env()
- if Pleroma.Config.get(:env) == :test, do: Ecto.Adapters.SQL.Sandbox.checkin(Repo)
+ def start_link(restart_pleroma? \\ true) do
+ load_and_update_env([], restart_pleroma?)
+ if Config.get(:env) == :test, do: Ecto.Adapters.SQL.Sandbox.checkin(Repo)
:ignore
end
- @spec load_and_update_env([ConfigDB.t()]) :: :ok | false
- def load_and_update_env(deleted \\ [], restart_pleroma? \\ true) do
- with {:configurable, true} <-
- {:configurable, Pleroma.Config.get(:configurable_from_database)},
- true <- Ecto.Adapters.SQL.table_exists?(Repo, "config"),
- started_applications <- Application.started_applications() do
+ @spec load_and_update_env([ConfigDB.t()], boolean()) :: :ok
+ def load_and_update_env(deleted_settings \\ [], restart_pleroma? \\ true) do
+ with {_, true} <- {:configurable, Config.get(:configurable_from_database)} do
# We need to restart applications for loaded settings take effect
- in_db = Repo.all(ConfigDB)
+ {logger, other} =
+ (Repo.all(ConfigDB) ++ deleted_settings)
+ |> Enum.map(&merge_with_default/1)
+ |> Enum.split_with(fn {group, _, _, _} -> group in [:logger, :quack] end)
- with_deleted = in_db ++ deleted
+ logger
+ |> Enum.sort()
+ |> Enum.each(&configure/1)
- reject_for_restart = if restart_pleroma?, do: @reject, else: [:pleroma | @reject]
+ started_applications = Application.started_applications()
- applications =
- with_deleted
- |> Enum.map(&merge_and_update(&1))
- |> Enum.uniq()
- # TODO: some problem with prometheus after restart!
- |> Enum.reject(&(&1 in reject_for_restart))
+ # TODO: some problem with prometheus after restart!
+ reject = [nil, :prometheus, :postgrex]
- # to be ensured that pleroma will be restarted last
- applications =
- if :pleroma in applications do
- List.delete(applications, :pleroma) ++ [:pleroma]
+ reject =
+ if restart_pleroma? do
+ reject
else
- Restarter.Pleroma.rebooted()
- applications
+ [:pleroma | reject]
end
- Enum.each(applications, &restart(started_applications, &1, Pleroma.Config.get(:env)))
+ other
+ |> Enum.map(&update/1)
+ |> Enum.uniq()
+ |> Enum.reject(&(&1 in reject))
+ |> maybe_set_pleroma_last()
+ |> Enum.each(&restart(started_applications, &1, Config.get(:env)))
:ok
else
@@ -78,51 +76,79 @@ defmodule Pleroma.Config.TransferTask do
end
end
- defp merge_and_update(setting) do
- try do
- key = ConfigDB.from_string(setting.key)
- group = ConfigDB.from_string(setting.group)
+ defp maybe_set_pleroma_last(apps) do
+ # to be ensured that pleroma will be restarted last
+ if :pleroma in apps do
+ apps
+ |> List.delete(:pleroma)
+ |> List.insert_at(-1, :pleroma)
+ else
+ Restarter.Pleroma.rebooted()
+ apps
+ end
+ end
- default = Pleroma.Config.Holder.default_config(group, key)
- value = ConfigDB.from_binary(setting.value)
+ defp merge_with_default(%{group: group, key: key, value: value} = setting) do
+ default = Config.Holder.default_config(group, key)
- merged_value =
- if Ecto.get_meta(setting, :state) == :deleted do
- default
- else
- if can_be_merged?(default, value) do
- ConfigDB.merge_group(group, key, default, value)
- else
- value
- end
- end
+ merged =
+ cond do
+ Ecto.get_meta(setting, :state) == :deleted -> default
+ can_be_merged?(default, value) -> ConfigDB.merge_group(group, key, default, value)
+ true -> value
+ end
- :ok = update_env(group, key, merged_value)
+ {group, key, value, merged}
+ end
- if group != :logger do
- if group != :pleroma or pleroma_need_restart?(group, key, value) do
- group
- end
- else
- # change logger configuration in runtime, without restart
- if Keyword.keyword?(merged_value) and
- key not in [:compile_time_application, :backends, :compile_time_purge_matching] do
- Logger.configure_backend(key, merged_value)
- else
- Logger.configure([{key, merged_value}])
- end
+ # change logger configuration in runtime, without restart
+ defp configure({:quack, key, _, merged}) do
+ Logger.configure_backend(Quack.Logger, [{key, merged}])
+ :ok = update_env(:quack, key, merged)
+ end
- nil
+ defp configure({_, :backends, _, merged}) do
+ # removing current backends
+ Enum.each(Application.get_env(:logger, :backends), &Logger.remove_backend/1)
+
+ Enum.each(merged, &Logger.add_backend/1)
+
+ :ok = update_env(:logger, :backends, merged)
+ end
+
+ defp configure({_, key, _, merged}) when key in [:console, :ex_syslogger] do
+ merged =
+ if key == :console do
+ put_in(merged[:format], merged[:format] <> "\n")
+ else
+ merged
end
+
+ backend =
+ if key == :ex_syslogger,
+ do: {ExSyslogger, :ex_syslogger},
+ else: key
+
+ Logger.configure_backend(backend, merged)
+ :ok = update_env(:logger, key, merged)
+ end
+
+ defp configure({_, key, _, merged}) do
+ Logger.configure([{key, merged}])
+ :ok = update_env(:logger, key, merged)
+ end
+
+ defp update({group, key, value, merged}) do
+ try do
+ :ok = update_env(group, key, merged)
+
+ if group != :pleroma or pleroma_need_restart?(group, key, value), do: group
rescue
error ->
error_msg =
- "updating env causes error, group: " <>
- inspect(setting.group) <>
- " key: " <>
- inspect(setting.key) <>
- " value: " <>
- inspect(ConfigDB.from_binary(setting.value)) <> " error: " <> inspect(error)
+ "updating env causes error, group: #{inspect(group)}, key: #{inspect(key)}, value: #{
+ inspect(value)
+ } error: #{inspect(error)}"
Logger.warn(error_msg)
@@ -130,6 +156,9 @@ defmodule Pleroma.Config.TransferTask do
end
end
+ defp update_env(group, key, nil), do: Application.delete_env(group, key)
+ defp update_env(group, key, value), do: Application.put_env(group, key, value)
+
@spec pleroma_need_restart?(atom(), atom(), any()) :: boolean()
def pleroma_need_restart?(group, key, value) do
group_and_key_need_reboot?(group, key) or group_and_subkey_need_reboot?(group, key, value)
@@ -147,9 +176,6 @@ defmodule Pleroma.Config.TransferTask do
end)
end
- defp update_env(group, key, nil), do: Application.delete_env(group, key)
- defp update_env(group, key, value), do: Application.put_env(group, key, value)
-
defp restart(_, :pleroma, env), do: Restarter.Pleroma.restart_after_boot(env)
defp restart(started_applications, app, _) do