Use finch everywhere (#33)
[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 Finch.start_link(name: MyFinch)
27
28
29 unless System.get_env("DEBUG") do
30 Logger.remove_backend(:console)
31 end
32
33 Enum.each(@apps, &Application.ensure_all_started/1)
34
35 oban_config = [
36 crontab: [],
37 repo: Pleroma.Repo,
38 log: false,
39 queues: [],
40 plugins: []
41 ]
42
43 children =
44 [
45 Pleroma.Repo,
46 Pleroma.Emoji,
47 {Pleroma.Config.TransferTask, false},
48 Pleroma.Web.Endpoint,
49 {Oban, oban_config},
50 {Majic.Pool,
51 [name: Pleroma.MajicPool, pool_size: Pleroma.Config.get([:majic_pool, :size], 2)]}
52 ] ++
53 elasticsearch_children()
54
55 cachex_children = Enum.map(@cachex_children, &Pleroma.Application.build_cachex(&1, []))
56
57 Supervisor.start_link(children ++ cachex_children,
58 strategy: :one_for_one,
59 name: Pleroma.Supervisor
60 )
61
62 if Pleroma.Config.get(:env) not in [:test, :benchmark] do
63 pleroma_rebooted?()
64 end
65 end
66
67 defp pleroma_rebooted? do
68 if Restarter.Pleroma.rebooted?() do
69 :ok
70 else
71 Process.sleep(10)
72 pleroma_rebooted?()
73 end
74 end
75
76 def load_pleroma do
77 Application.load(:pleroma)
78 end
79
80 def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
81 Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
82 end
83
84 def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
85 prompt_message = "#{prompt} [#{defname || defval}] "
86
87 input =
88 if mix_shell?(),
89 do: Mix.shell().prompt(prompt_message),
90 else: :io.get_line(prompt_message)
91
92 case input do
93 "\n" ->
94 case defval do
95 nil ->
96 shell_prompt(prompt, defval, defname)
97
98 defval ->
99 defval
100 end
101
102 input ->
103 String.trim(input)
104 end
105 end
106
107 def shell_info(message) do
108 if mix_shell?(),
109 do: Mix.shell().info(message),
110 else: IO.puts(message)
111 end
112
113 def shell_error(message) do
114 if mix_shell?(),
115 do: Mix.shell().error(message),
116 else: IO.puts(:stderr, message)
117 end
118
119 @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
120 def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
121
122 def escape_sh_path(path) do
123 ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
124 end
125
126 def elasticsearch_children do
127 config = Pleroma.Config.get([Pleroma.Search, :module])
128
129 if config == Pleroma.Search.Elasticsearch do
130 [Pleroma.Search.Elasticsearch.Cluster]
131 else
132 []
133 end
134 end
135 end