From 72ad3a66f48d4500be1f25dd7b02b834399d3bbe Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Fri, 3 Jul 2020 19:18:08 +0300 Subject: [PATCH 1/6] don't fully start pleroma in mix tasks --- lib/mix/pleroma.ex | 20 +++++++++++++++++++- lib/mix/tasks/pleroma/digest.ex | 2 ++ lib/mix/tasks/pleroma/email.ex | 1 + lib/mix/tasks/pleroma/relay.ex | 3 +++ lib/pleroma/application.ex | 3 ++- 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/mix/pleroma.ex b/lib/mix/pleroma.ex index 3ad6edbfb..553c74c25 100644 --- a/lib/mix/pleroma.ex +++ b/lib/mix/pleroma.ex @@ -3,6 +3,8 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Mix.Pleroma do + @apps [:restarter, :ecto, :ecto_sql, :postgrex, :db_connection, :cachex] + @cachex_childs ["object", "user"] @doc "Common functions to be reused in mix tasks" def start_pleroma do Application.put_env(:phoenix, :serve_endpoints, false, persistent: true) @@ -11,7 +13,23 @@ defmodule Mix.Pleroma do Application.put_env(:logger, :console, level: :debug) end - {:ok, _} = Application.ensure_all_started(:pleroma) + apps = + if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Gun do + [:gun | @apps] + else + [:hackney | @apps] + end + + Enum.each(apps, &Application.ensure_all_started/1) + + childs = [Pleroma.Repo, Pleroma.Config.TransferTask, Pleroma.Web.Endpoint] + + cachex_childs = Enum.map(@cachex_childs, &Pleroma.Application.build_cachex(&1, [])) + + Supervisor.start_link(childs ++ cachex_childs, + strategy: :one_for_one, + name: Pleroma.Supervisor + ) if Pleroma.Config.get(:env) not in [:test, :benchmark] do pleroma_rebooted?() diff --git a/lib/mix/tasks/pleroma/digest.ex b/lib/mix/tasks/pleroma/digest.ex index 3595f912d..8bde2d4f2 100644 --- a/lib/mix/tasks/pleroma/digest.ex +++ b/lib/mix/tasks/pleroma/digest.ex @@ -7,6 +7,8 @@ defmodule Mix.Tasks.Pleroma.Digest do def run(["test", nickname | opts]) do Mix.Pleroma.start_pleroma() + Application.ensure_all_started(:timex) + Application.ensure_all_started(:swoosh) user = Pleroma.User.get_by_nickname(nickname) diff --git a/lib/mix/tasks/pleroma/email.ex b/lib/mix/tasks/pleroma/email.ex index d3fac6ec8..16fe31431 100644 --- a/lib/mix/tasks/pleroma/email.ex +++ b/lib/mix/tasks/pleroma/email.ex @@ -7,6 +7,7 @@ defmodule Mix.Tasks.Pleroma.Email do def run(["test" | args]) do Mix.Pleroma.start_pleroma() + Application.ensure_all_started(:swoosh) {options, [], []} = OptionParser.parse( diff --git a/lib/mix/tasks/pleroma/relay.ex b/lib/mix/tasks/pleroma/relay.ex index c3312507e..b67d256c3 100644 --- a/lib/mix/tasks/pleroma/relay.ex +++ b/lib/mix/tasks/pleroma/relay.ex @@ -12,6 +12,7 @@ defmodule Mix.Tasks.Pleroma.Relay do def run(["follow", target]) do start_pleroma() + Application.ensure_all_started(:flake_id) with {:ok, _activity} <- Relay.follow(target) do # put this task to sleep to allow the genserver to push out the messages @@ -23,6 +24,7 @@ defmodule Mix.Tasks.Pleroma.Relay do def run(["unfollow", target]) do start_pleroma() + Application.ensure_all_started(:flake_id) with {:ok, _activity} <- Relay.unfollow(target) do # put this task to sleep to allow the genserver to push out the messages @@ -34,6 +36,7 @@ defmodule Mix.Tasks.Pleroma.Relay do def run(["list"]) do start_pleroma() + Application.ensure_all_started(:flake_id) with {:ok, list} <- Relay.list(true) do list |> Enum.each(&shell_info(&1)) diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex index 9615af122..7eb629abf 100644 --- a/lib/pleroma/application.ex +++ b/lib/pleroma/application.ex @@ -162,7 +162,8 @@ defmodule Pleroma.Application do defp seconds_valid_interval, do: :timer.seconds(Config.get!([Pleroma.Captcha, :seconds_valid])) - defp build_cachex(type, opts), + @spec build_cachex(String.t(), keyword()) :: map() + def build_cachex(type, opts), do: %{ id: String.to_atom("cachex_" <> type), start: {Cachex, :start_link, [String.to_atom(type <> "_cache"), opts]}, From b28cc154596ad9cbf5ef9708a5967672c61ddbdc Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Fri, 3 Jul 2020 20:12:00 +0300 Subject: [PATCH 2/6] don't restart pleroma in mix tasks --- lib/mix/pleroma.ex | 6 +++++- lib/pleroma/config/transfer_task.ex | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/mix/pleroma.ex b/lib/mix/pleroma.ex index 553c74c25..0fbb6f1cd 100644 --- a/lib/mix/pleroma.ex +++ b/lib/mix/pleroma.ex @@ -22,7 +22,11 @@ defmodule Mix.Pleroma do Enum.each(apps, &Application.ensure_all_started/1) - childs = [Pleroma.Repo, Pleroma.Config.TransferTask, Pleroma.Web.Endpoint] + childs = [ + Pleroma.Repo, + {Pleroma.Config.TransferTask, false}, + Pleroma.Web.Endpoint + ] cachex_childs = Enum.map(@cachex_childs, &Pleroma.Application.build_cachex(&1, [])) diff --git a/lib/pleroma/config/transfer_task.ex b/lib/pleroma/config/transfer_task.ex index eb86b8ff4..a0d7b7d71 100644 --- a/lib/pleroma/config/transfer_task.ex +++ b/lib/pleroma/config/transfer_task.ex @@ -31,8 +31,8 @@ defmodule Pleroma.Config.TransferTask do {:pleroma, :gopher, [:enabled]} ] - def start_link(_) do - load_and_update_env() + 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 From 4cbafcef0c3a0ce9ddf888b558c6691afb96ad94 Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Thu, 9 Jul 2020 11:17:43 +0300 Subject: [PATCH 3/6] load default config in mix tasks --- lib/mix/pleroma.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/mix/pleroma.ex b/lib/mix/pleroma.ex index 0fbb6f1cd..de16cc52c 100644 --- a/lib/mix/pleroma.ex +++ b/lib/mix/pleroma.ex @@ -7,6 +7,7 @@ defmodule Mix.Pleroma do @cachex_childs ["object", "user"] @doc "Common functions to be reused in mix tasks" def start_pleroma do + Pleroma.Config.Holder.save_default() Application.put_env(:phoenix, :serve_endpoints, false, persistent: true) if Pleroma.Config.get(:env) != :test do From c2edfd16e063cda117181da66d9b4fec87c121ac Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Thu, 9 Jul 2020 18:59:15 +0300 Subject: [PATCH 4/6] fix for user revoke invite task --- docs/administration/CLI_tasks/user.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/administration/CLI_tasks/user.md b/docs/administration/CLI_tasks/user.md index 1e6f4a8b4..3b4c421a7 100644 --- a/docs/administration/CLI_tasks/user.md +++ b/docs/administration/CLI_tasks/user.md @@ -57,11 +57,11 @@ mix pleroma.user invites ## Revoke invite ```sh tab="OTP" - ./bin/pleroma_ctl user revoke_invite + ./bin/pleroma_ctl user revoke_invite ``` ```sh tab="From Source" -mix pleroma.user revoke_invite +mix pleroma.user revoke_invite ``` From d5fcec8315a5f48d94a083b38abcc615834dc532 Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Thu, 9 Jul 2020 18:59:48 +0300 Subject: [PATCH 5/6] fix for info after tag/untag user --- lib/mix/tasks/pleroma/user.ex | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index bca7e87bf..a9370b5e7 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -15,6 +15,8 @@ defmodule Mix.Tasks.Pleroma.User do @moduledoc File.read!("docs/administration/CLI_tasks/user.md") def run(["new", nickname, email | rest]) do + Application.ensure_all_started(:flake_id) + {options, [], []} = OptionParser.parse( rest, @@ -97,6 +99,7 @@ defmodule Mix.Tasks.Pleroma.User do def run(["rm", nickname]) do start_pleroma() + Application.ensure_all_started(:flake_id) with %User{local: true} = user <- User.get_cached_by_nickname(nickname), {:ok, delete_data, _} <- Builder.delete(user, user.ap_id), @@ -232,7 +235,7 @@ defmodule Mix.Tasks.Pleroma.User do with %User{} = user <- User.get_cached_by_nickname(nickname) do user = user |> User.tag(tags) - shell_info("Tags of #{user.nickname}: #{inspect(tags)}") + shell_info("Tags of #{user.nickname}: #{inspect(user.tags)}") else _ -> shell_error("Could not change user tags for #{nickname}") @@ -245,7 +248,7 @@ defmodule Mix.Tasks.Pleroma.User do with %User{} = user <- User.get_cached_by_nickname(nickname) do user = user |> User.untag(tags) - shell_info("Tags of #{user.nickname}: #{inspect(tags)}") + shell_info("Tags of #{user.nickname}: #{inspect(user.tags)}") else _ -> shell_error("Could not change user tags for #{nickname}") @@ -328,6 +331,7 @@ defmodule Mix.Tasks.Pleroma.User do def run(["delete_activities", nickname]) do start_pleroma() + Application.ensure_all_started(:flake_id) with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do User.delete_user_activities(user) From 79707e879d3af359be9e1f6ac10717cc9cb72b2c Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Thu, 9 Jul 2020 19:13:16 +0300 Subject: [PATCH 6/6] cleap up --- lib/mix/pleroma.ex | 20 +++++++++++++++----- lib/mix/tasks/pleroma/digest.ex | 2 -- lib/mix/tasks/pleroma/email.ex | 1 - lib/mix/tasks/pleroma/relay.ex | 3 --- lib/mix/tasks/pleroma/user.ex | 4 ---- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/mix/pleroma.ex b/lib/mix/pleroma.ex index de16cc52c..9f0bf6ecb 100644 --- a/lib/mix/pleroma.ex +++ b/lib/mix/pleroma.ex @@ -3,8 +3,18 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Mix.Pleroma do - @apps [:restarter, :ecto, :ecto_sql, :postgrex, :db_connection, :cachex] - @cachex_childs ["object", "user"] + @apps [ + :restarter, + :ecto, + :ecto_sql, + :postgrex, + :db_connection, + :cachex, + :flake_id, + :swoosh, + :timex + ] + @cachex_children ["object", "user"] @doc "Common functions to be reused in mix tasks" def start_pleroma do Pleroma.Config.Holder.save_default() @@ -23,15 +33,15 @@ defmodule Mix.Pleroma do Enum.each(apps, &Application.ensure_all_started/1) - childs = [ + children = [ Pleroma.Repo, {Pleroma.Config.TransferTask, false}, Pleroma.Web.Endpoint ] - cachex_childs = Enum.map(@cachex_childs, &Pleroma.Application.build_cachex(&1, [])) + cachex_children = Enum.map(@cachex_children, &Pleroma.Application.build_cachex(&1, [])) - Supervisor.start_link(childs ++ cachex_childs, + Supervisor.start_link(children ++ cachex_children, strategy: :one_for_one, name: Pleroma.Supervisor ) diff --git a/lib/mix/tasks/pleroma/digest.ex b/lib/mix/tasks/pleroma/digest.ex index 8bde2d4f2..3595f912d 100644 --- a/lib/mix/tasks/pleroma/digest.ex +++ b/lib/mix/tasks/pleroma/digest.ex @@ -7,8 +7,6 @@ defmodule Mix.Tasks.Pleroma.Digest do def run(["test", nickname | opts]) do Mix.Pleroma.start_pleroma() - Application.ensure_all_started(:timex) - Application.ensure_all_started(:swoosh) user = Pleroma.User.get_by_nickname(nickname) diff --git a/lib/mix/tasks/pleroma/email.ex b/lib/mix/tasks/pleroma/email.ex index 16fe31431..d3fac6ec8 100644 --- a/lib/mix/tasks/pleroma/email.ex +++ b/lib/mix/tasks/pleroma/email.ex @@ -7,7 +7,6 @@ defmodule Mix.Tasks.Pleroma.Email do def run(["test" | args]) do Mix.Pleroma.start_pleroma() - Application.ensure_all_started(:swoosh) {options, [], []} = OptionParser.parse( diff --git a/lib/mix/tasks/pleroma/relay.ex b/lib/mix/tasks/pleroma/relay.ex index b67d256c3..c3312507e 100644 --- a/lib/mix/tasks/pleroma/relay.ex +++ b/lib/mix/tasks/pleroma/relay.ex @@ -12,7 +12,6 @@ defmodule Mix.Tasks.Pleroma.Relay do def run(["follow", target]) do start_pleroma() - Application.ensure_all_started(:flake_id) with {:ok, _activity} <- Relay.follow(target) do # put this task to sleep to allow the genserver to push out the messages @@ -24,7 +23,6 @@ defmodule Mix.Tasks.Pleroma.Relay do def run(["unfollow", target]) do start_pleroma() - Application.ensure_all_started(:flake_id) with {:ok, _activity} <- Relay.unfollow(target) do # put this task to sleep to allow the genserver to push out the messages @@ -36,7 +34,6 @@ defmodule Mix.Tasks.Pleroma.Relay do def run(["list"]) do start_pleroma() - Application.ensure_all_started(:flake_id) with {:ok, list} <- Relay.list(true) do list |> Enum.each(&shell_info(&1)) diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index a9370b5e7..01824aa18 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -15,8 +15,6 @@ defmodule Mix.Tasks.Pleroma.User do @moduledoc File.read!("docs/administration/CLI_tasks/user.md") def run(["new", nickname, email | rest]) do - Application.ensure_all_started(:flake_id) - {options, [], []} = OptionParser.parse( rest, @@ -99,7 +97,6 @@ defmodule Mix.Tasks.Pleroma.User do def run(["rm", nickname]) do start_pleroma() - Application.ensure_all_started(:flake_id) with %User{local: true} = user <- User.get_cached_by_nickname(nickname), {:ok, delete_data, _} <- Builder.delete(user, user.ap_id), @@ -331,7 +328,6 @@ defmodule Mix.Tasks.Pleroma.User do def run(["delete_activities", nickname]) do start_pleroma() - Application.ensure_all_started(:flake_id) with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do User.delete_user_activities(user)