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