1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Application do
14 @name Mix.Project.config()[:name]
15 @version Mix.Project.config()[:version]
16 @repository Mix.Project.config()[:source_url]
20 def version, do: @version
21 def named_version, do: @name <> " " <> @version
22 def repository, do: @repository
25 if Process.whereis(Pleroma.Web.Endpoint) do
26 case Config.get([:http, :user_agent], :default) do
28 info = "#{Pleroma.Web.base_url()} <#{Config.get([:instance, :email], "")}>"
29 named_version() <> "; " <> info
35 # fallback, if endpoint is not started yet
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!()
59 Pleroma.Docs.JSON.compile()
62 adapter = Application.get_env(:tesla, :adapter)
64 if adapter == Tesla.Adapter.Gun do
65 if version = Pleroma.OTPVersion.version() do
69 |> Enum.map(&String.to_integer/1)
72 if (major == 22 and minor < 2) or major < 22 do
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.
80 !!!OTP VERSION WARNING!!!
81 To support correct handling of unordered certificates chains - OTP version must be > 22.2.
86 # Define workers and child supervisors to be supervised
92 Pleroma.Web.Plugs.RateLimiter.Supervisor
95 http_children(adapter, @env) ++
98 Pleroma.JobQueueMonitor,
99 {Majic.Pool, [name: Pleroma.MajicPool, pool_size: Config.get([:majic_pool, :size], 2)]},
100 {Oban, Config.get(Oban)}
102 task_children(@env) ++
103 dont_run_in_test(@env) ++
104 chat_child(chat_enabled?()) ++
106 Pleroma.Web.Endpoint,
107 Pleroma.Gopher.Server
110 # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
111 # for other strategies and supported options
112 opts = [strategy: :one_for_one, name: Pleroma.Supervisor]
113 result = Supervisor.start_link(children, opts)
115 set_postgres_server_version()
120 defp set_postgres_server_version do
122 with %{rows: [[version]]} <- Ecto.Adapters.SQL.query!(Pleroma.Repo, "show server_version"),
123 {num, _} <- Float.parse(version) do
128 "Could not get the postgres version: #{inspect(e)}.\nSetting the default value of 9.6"
134 :persistent_term.put({Pleroma.Repo, :postgres_version}, version)
137 def load_custom_modules do
138 dir = Config.get([:modules, :runtime_dir])
140 if dir && File.exists?(dir) do
142 |> Pleroma.Utils.compile_dir()
144 {:error, _errors, _warnings} ->
145 raise "Invalid custom modules"
147 {:ok, modules, _warnings} ->
149 Enum.each(modules, fn mod ->
150 Logger.info("Custom module loaded: #{inspect(mod)}")
159 defp setup_instrumenters do
160 require Prometheus.Registry
162 if Application.get_env(:prometheus, Pleroma.Repo.Instrumenter) do
166 [:pleroma, :repo, :query],
167 &Pleroma.Repo.Instrumenter.handle_event/4,
171 Pleroma.Repo.Instrumenter.setup()
174 Pleroma.Web.Endpoint.MetricsExporter.setup()
175 Pleroma.Web.Endpoint.PipelineInstrumenter.setup()
177 # Note: disabled until prometheus-phx is integrated into prometheus-phoenix:
178 # Pleroma.Web.Endpoint.Instrumenter.setup()
179 PrometheusPhx.setup()
182 defp cachex_children do
184 build_cachex("used_captcha", ttl_interval: seconds_valid_interval()),
185 build_cachex("user", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
186 build_cachex("object", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
187 build_cachex("rich_media", default_ttl: :timer.minutes(120), limit: 5000),
188 build_cachex("scrubber", limit: 2500),
189 build_cachex("idempotency", expiration: idempotency_expiration(), limit: 2500),
190 build_cachex("web_resp", limit: 2500),
191 build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
192 build_cachex("failed_proxy_url", limit: 2500),
193 build_cachex("banned_urls", default_ttl: :timer.hours(24 * 30), limit: 5_000),
194 build_cachex("chat_message_id_idempotency_key",
195 expiration: chat_message_id_idempotency_key_expiration(),
201 defp emoji_packs_expiration,
202 do: expiration(default: :timer.seconds(5 * 60), interval: :timer.seconds(60))
204 defp idempotency_expiration,
205 do: expiration(default: :timer.seconds(6 * 60 * 60), interval: :timer.seconds(60))
207 defp chat_message_id_idempotency_key_expiration,
208 do: expiration(default: :timer.minutes(2), interval: :timer.seconds(60))
210 defp seconds_valid_interval,
211 do: :timer.seconds(Config.get!([Pleroma.Captcha, :seconds_valid]))
213 @spec build_cachex(String.t(), keyword()) :: map()
214 def build_cachex(type, opts),
216 id: String.to_atom("cachex_" <> type),
217 start: {Cachex, :start_link, [String.to_atom(type <> "_cache"), opts]},
221 defp chat_enabled?, do: Config.get([:chat, :enabled])
223 defp dont_run_in_test(env) when env in [:test, :benchmark], do: []
225 defp dont_run_in_test(_) do
229 name: Pleroma.Web.Streamer.registry(),
231 partitions: System.schedulers_online()
236 defp chat_child(true) do
238 Pleroma.Web.ChatChannel.ChatChannelState,
239 {Phoenix.PubSub, [name: Pleroma.PubSub, adapter: Phoenix.PubSub.PG2]}
243 defp chat_child(_), do: []
245 defp task_children(:test) do
249 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
255 defp task_children(_) do
259 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
263 id: :internal_fetch_init,
264 start: {Task, :start_link, [&Pleroma.Web.ActivityPub.InternalFetchActor.init/0]},
270 # start hackney and gun pools in tests
271 defp http_children(_, :test) do
272 http_children(Tesla.Adapter.Hackney, nil) ++ http_children(Tesla.Adapter.Gun, nil)
275 defp http_children(Tesla.Adapter.Hackney, _) do
276 pools = [:federation, :media]
279 if Config.get([Pleroma.Upload, :proxy_remote]) do
286 options = Config.get([:hackney_pools, pool])
287 :hackney_pool.child_spec(pool, options)
291 defp http_children(Tesla.Adapter.Gun, _) do
292 Pleroma.Gun.ConnectionPool.children() ++
293 [{Task, &Pleroma.HTTP.AdapterHelper.Gun.limiter_setup/0}]
296 defp http_children(_, _), do: []
298 @spec limiters_setup() :: :ok
299 def limiters_setup do
300 config = Config.get(ConcurrentLimiter, [])
302 [Pleroma.Web.RichMedia.Helpers, Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy]
303 |> Enum.each(fn module ->
304 mod_config = Keyword.get(config, module, [])
306 max_running = Keyword.get(mod_config, :max_running, 5)
307 max_waiting = Keyword.get(mod_config, :max_waiting, 5)
309 ConcurrentLimiter.new(module, max_running, max_waiting)