Merge branch 'admin-fix-duplicated-endpoints' into 'develop'
[akkoma] / lib / pleroma / application.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 Pleroma.Application do
6 use Application
7
8 import Cachex.Spec
9
10 alias Pleroma.Config
11
12 require Logger
13
14 @name Mix.Project.config()[:name]
15 @version Mix.Project.config()[:version]
16 @repository Mix.Project.config()[:source_url]
17 @mix_env Mix.env()
18
19 def name, do: @name
20 def version, do: @version
21 def named_version, do: @name <> " " <> @version
22 def repository, do: @repository
23
24 def user_agent do
25 if Process.whereis(Pleroma.Web.Endpoint) do
26 case Config.get([:http, :user_agent], :default) do
27 :default ->
28 info = "#{Pleroma.Web.Endpoint.url()} <#{Config.get([:instance, :email], "")}>"
29 named_version() <> "; " <> info
30
31 custom ->
32 custom
33 end
34 else
35 # fallback, if endpoint is not started yet
36 "Pleroma Data Loader"
37 end
38 end
39
40 # See http://elixir-lang.org/docs/stable/elixir/Application.html
41 # for more information on OTP Applications
42 def start(_type, _args) do
43 # Scrubbers are compiled at runtime and therefore will cause a conflict
44 # every time the application is restarted, so we disable module
45 # conflicts at runtime
46 Code.compiler_options(ignore_module_conflict: true)
47 # Disable warnings_as_errors at runtime, it breaks Phoenix live reload
48 # due to protocol consolidation warnings
49 Code.compiler_options(warnings_as_errors: false)
50 Pleroma.Telemetry.Logger.attach()
51 Config.Holder.save_default()
52 Pleroma.HTML.compile_scrubbers()
53 Pleroma.Config.Oban.warn()
54 Config.DeprecationWarnings.warn()
55 Pleroma.Web.Plugs.HTTPSecurityPlug.warn_if_disabled()
56 Pleroma.ApplicationRequirements.verify!()
57 setup_instrumenters()
58 load_custom_modules()
59 Pleroma.Docs.JSON.compile()
60 limiters_setup()
61
62 adapter = Application.get_env(:tesla, :adapter)
63
64 if match?({Tesla.Adapter.Finch, _}, adapter) do
65 Logger.info("Starting Finch")
66 Finch.start_link(name: MyFinch)
67 end
68
69 if adapter == Tesla.Adapter.Gun do
70 if version = Pleroma.OTPVersion.version() do
71 [major, minor] =
72 version
73 |> String.split(".")
74 |> Enum.map(&String.to_integer/1)
75 |> Enum.take(2)
76
77 if (major == 22 and minor < 2) or major < 22 do
78 raise "
79 !!!OTP VERSION WARNING!!!
80 You are using gun adapter with OTP version #{version}, which doesn't support correct handling of unordered certificates chains. Please update your Erlang/OTP to at least 22.2.
81 "
82 end
83 else
84 raise "
85 !!!OTP VERSION WARNING!!!
86 To support correct handling of unordered certificates chains - OTP version must be > 22.2.
87 "
88 end
89 end
90
91 # Define workers and child supervisors to be supervised
92 children =
93 [
94 Pleroma.Repo,
95 Config.TransferTask,
96 Pleroma.Emoji,
97 Pleroma.Web.Plugs.RateLimiter.Supervisor
98 ] ++
99 cachex_children() ++
100 http_children(adapter, @mix_env) ++
101 [
102 Pleroma.Stats,
103 Pleroma.JobQueueMonitor,
104 {Majic.Pool, [name: Pleroma.MajicPool, pool_size: Config.get([:majic_pool, :size], 2)]},
105 {Oban, Config.get(Oban)},
106 Pleroma.Web.Endpoint
107 ] ++
108 task_children(@mix_env) ++
109 dont_run_in_test(@mix_env) ++
110 shout_child(shout_enabled?()) ++
111 [Pleroma.Gopher.Server]
112
113 # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
114 # for other strategies and supported options
115 opts = [strategy: :one_for_one, name: Pleroma.Supervisor]
116 result = Supervisor.start_link(children, opts)
117
118 set_postgres_server_version()
119
120 result
121 end
122
123 defp set_postgres_server_version do
124 version =
125 with %{rows: [[version]]} <- Ecto.Adapters.SQL.query!(Pleroma.Repo, "show server_version"),
126 {num, _} <- Float.parse(version) do
127 num
128 else
129 e ->
130 Logger.warn(
131 "Could not get the postgres version: #{inspect(e)}.\nSetting the default value of 9.6"
132 )
133
134 9.6
135 end
136
137 :persistent_term.put({Pleroma.Repo, :postgres_version}, version)
138 end
139
140 def load_custom_modules do
141 dir = Config.get([:modules, :runtime_dir])
142
143 if dir && File.exists?(dir) do
144 dir
145 |> Pleroma.Utils.compile_dir()
146 |> case do
147 {:error, _errors, _warnings} ->
148 raise "Invalid custom modules"
149
150 {:ok, modules, _warnings} ->
151 if @mix_env != :test do
152 Enum.each(modules, fn mod ->
153 Logger.info("Custom module loaded: #{inspect(mod)}")
154 end)
155 end
156
157 :ok
158 end
159 end
160 end
161
162 defp setup_instrumenters do
163 require Prometheus.Registry
164
165 if Application.get_env(:prometheus, Pleroma.Repo.Instrumenter) do
166 :ok =
167 :telemetry.attach(
168 "prometheus-ecto",
169 [:pleroma, :repo, :query],
170 &Pleroma.Repo.Instrumenter.handle_event/4,
171 %{}
172 )
173
174 Pleroma.Repo.Instrumenter.setup()
175 end
176
177 Pleroma.Web.Endpoint.MetricsExporter.setup()
178 Pleroma.Web.Endpoint.PipelineInstrumenter.setup()
179
180 # Note: disabled until prometheus-phx is integrated into prometheus-phoenix:
181 # Pleroma.Web.Endpoint.Instrumenter.setup()
182 PrometheusPhx.setup()
183 end
184
185 defp cachex_children do
186 [
187 build_cachex("used_captcha", ttl_interval: seconds_valid_interval()),
188 build_cachex("user", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
189 build_cachex("object", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
190 build_cachex("rich_media", default_ttl: :timer.minutes(120), limit: 5000),
191 build_cachex("scrubber", limit: 2500),
192 build_cachex("idempotency", expiration: idempotency_expiration(), limit: 2500),
193 build_cachex("web_resp", limit: 2500),
194 build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
195 build_cachex("failed_proxy_url", limit: 2500),
196 build_cachex("banned_urls", default_ttl: :timer.hours(24 * 30), limit: 5_000),
197 build_cachex("chat_message_id_idempotency_key",
198 expiration: chat_message_id_idempotency_key_expiration(),
199 limit: 500_000
200 )
201 ]
202 end
203
204 defp emoji_packs_expiration,
205 do: expiration(default: :timer.seconds(5 * 60), interval: :timer.seconds(60))
206
207 defp idempotency_expiration,
208 do: expiration(default: :timer.seconds(6 * 60 * 60), interval: :timer.seconds(60))
209
210 defp chat_message_id_idempotency_key_expiration,
211 do: expiration(default: :timer.minutes(2), interval: :timer.seconds(60))
212
213 defp seconds_valid_interval,
214 do: :timer.seconds(Config.get!([Pleroma.Captcha, :seconds_valid]))
215
216 @spec build_cachex(String.t(), keyword()) :: map()
217 def build_cachex(type, opts),
218 do: %{
219 id: String.to_atom("cachex_" <> type),
220 start: {Cachex, :start_link, [String.to_atom(type <> "_cache"), opts]},
221 type: :worker
222 }
223
224 defp shout_enabled?, do: Config.get([:shout, :enabled])
225
226 defp dont_run_in_test(env) when env in [:test, :benchmark], do: []
227
228 defp dont_run_in_test(_) do
229 [
230 {Registry,
231 [
232 name: Pleroma.Web.Streamer.registry(),
233 keys: :duplicate,
234 partitions: System.schedulers_online()
235 ]}
236 ] ++ background_migrators()
237 end
238
239 defp background_migrators do
240 [
241 Pleroma.Migrators.HashtagsTableMigrator
242 ]
243 end
244
245 defp shout_child(true) do
246 [
247 Pleroma.Web.ShoutChannel.ShoutChannelState,
248 {Phoenix.PubSub, [name: Pleroma.PubSub, adapter: Phoenix.PubSub.PG2]}
249 ]
250 end
251
252 defp shout_child(_), do: []
253
254 defp task_children(:test) do
255 [
256 %{
257 id: :web_push_init,
258 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
259 restart: :temporary
260 }
261 ]
262 end
263
264 defp task_children(_) do
265 [
266 %{
267 id: :web_push_init,
268 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
269 restart: :temporary
270 },
271 %{
272 id: :internal_fetch_init,
273 start: {Task, :start_link, [&Pleroma.Web.ActivityPub.InternalFetchActor.init/0]},
274 restart: :temporary
275 }
276 ]
277 end
278
279 # start hackney and gun pools in tests
280 defp http_children(_, :test) do
281 http_children(Tesla.Adapter.Hackney, nil) ++ http_children(Tesla.Adapter.Gun, nil)
282 end
283
284 defp http_children(Tesla.Adapter.Hackney, _) do
285 pools = [:federation, :media]
286
287 pools =
288 if Config.get([Pleroma.Upload, :proxy_remote]) do
289 [:upload | pools]
290 else
291 pools
292 end
293
294 for pool <- pools do
295 options = Config.get([:hackney_pools, pool])
296 :hackney_pool.child_spec(pool, options)
297 end
298 end
299
300 defp http_children(Tesla.Adapter.Gun, _) do
301 Pleroma.Gun.ConnectionPool.children() ++
302 [{Task, &Pleroma.HTTP.AdapterHelper.Gun.limiter_setup/0}]
303 end
304
305 defp http_children(_, _), do: []
306
307 @spec limiters_setup() :: :ok
308 def limiters_setup do
309 config = Config.get(ConcurrentLimiter, [])
310
311 [Pleroma.Web.RichMedia.Helpers, Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy]
312 |> Enum.each(fn module ->
313 mod_config = Keyword.get(config, module, [])
314
315 max_running = Keyword.get(mod_config, :max_running, 5)
316 max_waiting = Keyword.get(mod_config, :max_waiting, 5)
317
318 ConcurrentLimiter.new(module, max_running, max_waiting)
319 end)
320 end
321 end