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