Merge branch 'stable' into mergeback/2.2.1
[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 :fast_html
17 ]
18 @cachex_children ["object", "user", "scrubber", "web_resp"]
19 @doc "Common functions to be reused in mix tasks"
20 def start_pleroma do
21 Pleroma.Config.Holder.save_default()
22 Pleroma.Config.Oban.warn()
23 Pleroma.Application.limiters_setup()
24 Application.put_env(:phoenix, :serve_endpoints, false, persistent: true)
25
26 unless System.get_env("DEBUG") do
27 Logger.remove_backend(:console)
28 end
29
30 adapter = Application.get_env(:tesla, :adapter)
31
32 apps =
33 if adapter == Tesla.Adapter.Gun do
34 [:gun | @apps]
35 else
36 [:hackney | @apps]
37 end
38
39 Enum.each(apps, &Application.ensure_all_started/1)
40
41 oban_config = [
42 crontab: [],
43 repo: Pleroma.Repo,
44 log: false,
45 queues: [],
46 plugins: []
47 ]
48
49 children =
50 [
51 Pleroma.Repo,
52 Pleroma.Emoji,
53 {Pleroma.Config.TransferTask, false},
54 Pleroma.Web.Endpoint,
55 {Oban, oban_config},
56 {Majic.Pool,
57 [name: Pleroma.MajicPool, pool_size: Pleroma.Config.get([:majic_pool, :size], 2)]}
58 ] ++
59 http_children(adapter)
60
61 cachex_children = Enum.map(@cachex_children, &Pleroma.Application.build_cachex(&1, []))
62
63 Supervisor.start_link(children ++ cachex_children,
64 strategy: :one_for_one,
65 name: Pleroma.Supervisor
66 )
67
68 if Pleroma.Config.get(:env) not in [:test, :benchmark] do
69 pleroma_rebooted?()
70 end
71 end
72
73 defp pleroma_rebooted? do
74 if Restarter.Pleroma.rebooted?() do
75 :ok
76 else
77 Process.sleep(10)
78 pleroma_rebooted?()
79 end
80 end
81
82 def load_pleroma do
83 Application.load(:pleroma)
84 end
85
86 def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
87 Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
88 end
89
90 def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
91 prompt_message = "#{prompt} [#{defname || defval}] "
92
93 input =
94 if mix_shell?(),
95 do: Mix.shell().prompt(prompt_message),
96 else: :io.get_line(prompt_message)
97
98 case input do
99 "\n" ->
100 case defval do
101 nil ->
102 shell_prompt(prompt, defval, defname)
103
104 defval ->
105 defval
106 end
107
108 input ->
109 String.trim(input)
110 end
111 end
112
113 def shell_info(message) do
114 if mix_shell?(),
115 do: Mix.shell().info(message),
116 else: IO.puts(message)
117 end
118
119 def shell_error(message) do
120 if mix_shell?(),
121 do: Mix.shell().error(message),
122 else: IO.puts(:stderr, message)
123 end
124
125 @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
126 def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
127
128 def escape_sh_path(path) do
129 ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
130 end
131
132 defp http_children(Tesla.Adapter.Gun) do
133 Pleroma.Gun.ConnectionPool.children() ++
134 [{Task, &Pleroma.HTTP.AdapterHelper.Gun.limiter_setup/0}]
135 end
136
137 defp http_children(_), do: []
138 end