Fix mix tasks that make HTTP calls by starting the Gun connection pool
[akkoma] / lib / mix / pleroma.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 Mix.Pleroma do
6 @apps [
7 :restarter,
8 :ecto,
9 :ecto_sql,
10 :postgrex,
11 :db_connection,
12 :cachex,
13 :flake_id,
14 :swoosh,
15 :timex
16 ]
17 @cachex_children ["object", "user"]
18 @doc "Common functions to be reused in mix tasks"
19 def start_pleroma do
20 Pleroma.Config.Holder.save_default()
21 Application.put_env(:phoenix, :serve_endpoints, false, persistent: true)
22
23 if Pleroma.Config.get(:env) != :test do
24 Application.put_env(:logger, :console, level: :debug)
25 end
26
27 adapter = Application.get_env(:tesla, :adapter)
28
29 apps =
30 if adapter == Tesla.Adapter.Gun do
31 [:gun | @apps]
32 else
33 [:hackney | @apps]
34 end
35
36 Enum.each(apps, &Application.ensure_all_started/1)
37
38 children =
39 [
40 Pleroma.Repo,
41 {Pleroma.Config.TransferTask, false},
42 Pleroma.Web.Endpoint
43 ] ++
44 http_children(adapter)
45
46 cachex_children = Enum.map(@cachex_children, &Pleroma.Application.build_cachex(&1, []))
47
48 Supervisor.start_link(children ++ cachex_children,
49 strategy: :one_for_one,
50 name: Pleroma.Supervisor
51 )
52
53 if Pleroma.Config.get(:env) not in [:test, :benchmark] do
54 pleroma_rebooted?()
55 end
56 end
57
58 defp pleroma_rebooted? do
59 if Restarter.Pleroma.rebooted?() do
60 :ok
61 else
62 Process.sleep(10)
63 pleroma_rebooted?()
64 end
65 end
66
67 def load_pleroma do
68 Application.load(:pleroma)
69 end
70
71 def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
72 Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
73 end
74
75 def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
76 prompt_message = "#{prompt} [#{defname || defval}] "
77
78 input =
79 if mix_shell?(),
80 do: Mix.shell().prompt(prompt_message),
81 else: :io.get_line(prompt_message)
82
83 case input do
84 "\n" ->
85 case defval do
86 nil ->
87 shell_prompt(prompt, defval, defname)
88
89 defval ->
90 defval
91 end
92
93 input ->
94 String.trim(input)
95 end
96 end
97
98 def shell_yes?(message) do
99 if mix_shell?(),
100 do: Mix.shell().yes?("Continue?"),
101 else: shell_prompt(message, "Continue?") in ~w(Yn Y y)
102 end
103
104 def shell_info(message) do
105 if mix_shell?(),
106 do: Mix.shell().info(message),
107 else: IO.puts(message)
108 end
109
110 def shell_error(message) do
111 if mix_shell?(),
112 do: Mix.shell().error(message),
113 else: IO.puts(:stderr, message)
114 end
115
116 @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
117 def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
118
119 def escape_sh_path(path) do
120 ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
121 end
122
123 defp http_children(Tesla.Adapter.Gun) do
124 Pleroma.Gun.ConnectionPool.children() ++
125 [{Task, &Pleroma.HTTP.AdapterHelper.Gun.limiter_setup/0}]
126 end
127
128 defp http_children(_), do: []
129 end