Merge branch 'replies-count' 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 adapter == Tesla.Adapter.Gun do
65 if version = Pleroma.OTPVersion.version() do
66 [major, minor] =
67 version
68 |> String.split(".")
69 |> Enum.map(&String.to_integer/1)
70 |> Enum.take(2)
71
72 if (major == 22 and minor < 2) or major < 22 do
73 raise "
74 !!!OTP VERSION WARNING!!!
75 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.
76 "
77 end
78 else
79 raise "
80 !!!OTP VERSION WARNING!!!
81 To support correct handling of unordered certificates chains - OTP version must be > 22.2.
82 "
83 end
84 end
85
86 # Define workers and child supervisors to be supervised
87 children =
88 [
89 Pleroma.Repo,
90 Config.TransferTask,
91 Pleroma.Emoji,
92 Pleroma.Web.Plugs.RateLimiter.Supervisor
93 ] ++
94 cachex_children() ++
95 http_children(adapter, @mix_env) ++
96 [
97 Pleroma.Stats,
98 Pleroma.JobQueueMonitor,
99 {Majic.Pool, [name: Pleroma.MajicPool, pool_size: Config.get([:majic_pool, :size], 2)]},
100 {Oban, Config.get(Oban)},
101 Pleroma.Web.Endpoint
102 ] ++
103 task_children(@mix_env) ++
104 dont_run_in_test(@mix_env) ++
105 shout_child(shout_enabled?()) ++
106 [Pleroma.Gopher.Server]
107
108 # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
109 # for other strategies and supported options
110 opts = [strategy: :one_for_one, name: Pleroma.Supervisor]
111 result = Supervisor.start_link(children, opts)
112
113 set_postgres_server_version()
114
115 result
116 end
117
118 defp set_postgres_server_version do
119 version =
120 with %{rows: [[version]]} <- Ecto.Adapters.SQL.query!(Pleroma.Repo, "show server_version"),
121 {num, _} <- Float.parse(version) do
122 num
123 else
124 e ->
125 Logger.warn(
126 "Could not get the postgres version: #{inspect(e)}.\nSetting the default value of 9.6"
127 )
128
129 9.6
130 end
131
132 :persistent_term.put({Pleroma.Repo, :postgres_version}, version)
133 end
134
135 def load_custom_modules do
136 dir = Config.get([:modules, :runtime_dir])
137
138 if dir && File.exists?(dir) do
139 dir
140 |> Pleroma.Utils.compile_dir()
141 |> case do
142 {:error, _errors, _warnings} ->
143 raise "Invalid custom modules"
144
145 {:ok, modules, _warnings} ->
146 if @mix_env != :test do
147 Enum.each(modules, fn mod ->
148 Logger.info("Custom module loaded: #{inspect(mod)}")
149 end)
150 end
151
152 :ok
153 end
154 end
155 end
156
157 defp setup_instrumenters do
158 require Prometheus.Registry
159
160 if Application.get_env(:prometheus, Pleroma.Repo.Instrumenter) do
161 :ok =
162 :telemetry.attach(
163 "prometheus-ecto",
164 [:pleroma, :repo, :query],
165 &Pleroma.Repo.Instrumenter.handle_event/4,
166 %{}
167 )
168
169 Pleroma.Repo.Instrumenter.setup()
170 end
171
172 Pleroma.Web.Endpoint.MetricsExporter.setup()
173 Pleroma.Web.Endpoint.PipelineInstrumenter.setup()
174
175 # Note: disabled until prometheus-phx is integrated into prometheus-phoenix:
176 # Pleroma.Web.Endpoint.Instrumenter.setup()
177 PrometheusPhx.setup()
178 end
179
180 defp cachex_children do
181 [
182 build_cachex("used_captcha", ttl_interval: seconds_valid_interval()),
183 build_cachex("user", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
184 build_cachex("object", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
185 build_cachex("rich_media", default_ttl: :timer.minutes(120), limit: 5000),
186 build_cachex("scrubber", limit: 2500),
187 build_cachex("idempotency", expiration: idempotency_expiration(), limit: 2500),
188 build_cachex("web_resp", limit: 2500),
189 build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
190 build_cachex("failed_proxy_url", limit: 2500),
191 build_cachex("banned_urls", default_ttl: :timer.hours(24 * 30), limit: 5_000),
192 build_cachex("chat_message_id_idempotency_key",
193 expiration: chat_message_id_idempotency_key_expiration(),
194 limit: 500_000
195 )
196 ]
197 end
198
199 defp emoji_packs_expiration,
200 do: expiration(default: :timer.seconds(5 * 60), interval: :timer.seconds(60))
201
202 defp idempotency_expiration,
203 do: expiration(default: :timer.seconds(6 * 60 * 60), interval: :timer.seconds(60))
204
205 defp chat_message_id_idempotency_key_expiration,
206 do: expiration(default: :timer.minutes(2), interval: :timer.seconds(60))
207
208 defp seconds_valid_interval,
209 do: :timer.seconds(Config.get!([Pleroma.Captcha, :seconds_valid]))
210
211 @spec build_cachex(String.t(), keyword()) :: map()
212 def build_cachex(type, opts),
213 do: %{
214 id: String.to_atom("cachex_" <> type),
215 start: {Cachex, :start_link, [String.to_atom(type <> "_cache"), opts]},
216 type: :worker
217 }
218
219 defp shout_enabled?, do: Config.get([:shout, :enabled])
220
221 defp dont_run_in_test(env) when env in [:test, :benchmark], do: []
222
223 defp dont_run_in_test(_) do
224 [
225 {Registry,
226 [
227 name: Pleroma.Web.Streamer.registry(),
228 keys: :duplicate,
229 partitions: System.schedulers_online()
230 ]}
231 ] ++ background_migrators()
232 end
233
234 defp background_migrators do
235 [
236 Pleroma.Migrators.HashtagsTableMigrator
237 ]
238 end
239
240 defp shout_child(true) do
241 [
242 Pleroma.Web.ShoutChannel.ShoutChannelState,
243 {Phoenix.PubSub, [name: Pleroma.PubSub, adapter: Phoenix.PubSub.PG2]}
244 ]
245 end
246
247 defp shout_child(_), do: []
248
249 defp task_children(:test) do
250 [
251 %{
252 id: :web_push_init,
253 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
254 restart: :temporary
255 }
256 ]
257 end
258
259 defp task_children(_) do
260 [
261 %{
262 id: :web_push_init,
263 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
264 restart: :temporary
265 },
266 %{
267 id: :internal_fetch_init,
268 start: {Task, :start_link, [&Pleroma.Web.ActivityPub.InternalFetchActor.init/0]},
269 restart: :temporary
270 }
271 ]
272 end
273
274 # start hackney and gun pools in tests
275 defp http_children(_, :test) do
276 http_children(Tesla.Adapter.Hackney, nil) ++ http_children(Tesla.Adapter.Gun, nil)
277 end
278
279 defp http_children(Tesla.Adapter.Hackney, _) do
280 pools = [:federation, :media]
281
282 pools =
283 if Config.get([Pleroma.Upload, :proxy_remote]) do
284 [:upload | pools]
285 else
286 pools
287 end
288
289 for pool <- pools do
290 options = Config.get([:hackney_pools, pool])
291 :hackney_pool.child_spec(pool, options)
292 end
293 end
294
295 defp http_children(Tesla.Adapter.Gun, _) do
296 Pleroma.Gun.ConnectionPool.children() ++
297 [{Task, &Pleroma.HTTP.AdapterHelper.Gun.limiter_setup/0}]
298 end
299
300 defp http_children(_, _), do: []
301
302 @spec limiters_setup() :: :ok
303 def limiters_setup do
304 config = Config.get(ConcurrentLimiter, [])
305
306 [Pleroma.Web.RichMedia.Helpers, Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy]
307 |> Enum.each(fn module ->
308 mod_config = Keyword.get(config, module, [])
309
310 max_running = Keyword.get(mod_config, :max_running, 5)
311 max_waiting = Keyword.get(mod_config, :max_waiting, 5)
312
313 ConcurrentLimiter.new(module, max_running, max_waiting)
314 end)
315 end
316 end