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.Endpoint.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 Logger.info("Starting Finch")
63 Finch.start_link(name: MyFinch)
65 # Define workers and child supervisors to be supervised
71 Pleroma.Web.Plugs.RateLimiter.Supervisor
76 Pleroma.JobQueueMonitor,
77 {Majic.Pool, [name: Pleroma.MajicPool, pool_size: Config.get([:majic_pool, :size], 2)]},
78 {Oban, Config.get(Oban)},
81 elasticsearch_children() ++
82 task_children(@mix_env) ++
83 dont_run_in_test(@mix_env) ++
84 shout_child(shout_enabled?())
86 # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
87 # for other strategies and supported options
88 opts = [strategy: :one_for_one, name: Pleroma.Supervisor]
89 result = Supervisor.start_link(children, opts)
91 set_postgres_server_version()
96 defp set_postgres_server_version do
98 with %{rows: [[version]]} <- Ecto.Adapters.SQL.query!(Pleroma.Repo, "show server_version"),
99 {num, _} <- Float.parse(version) do
104 "Could not get the postgres version: #{inspect(e)}.\nSetting the default value of 9.6"
110 :persistent_term.put({Pleroma.Repo, :postgres_version}, version)
113 def load_custom_modules do
114 dir = Config.get([:modules, :runtime_dir])
116 if dir && File.exists?(dir) do
118 |> Pleroma.Utils.compile_dir()
120 {:error, _errors, _warnings} ->
121 raise "Invalid custom modules"
123 {:ok, modules, _warnings} ->
124 if @mix_env != :test do
125 Enum.each(modules, fn mod ->
126 Logger.info("Custom module loaded: #{inspect(mod)}")
135 defp setup_instrumenters do
136 require Prometheus.Registry
138 if Application.get_env(:prometheus, Pleroma.Repo.Instrumenter) do
142 [:pleroma, :repo, :query],
143 &Pleroma.Repo.Instrumenter.handle_event/4,
147 Pleroma.Repo.Instrumenter.setup()
150 Pleroma.Web.Endpoint.MetricsExporter.setup()
151 Pleroma.Web.Endpoint.PipelineInstrumenter.setup()
153 # Note: disabled until prometheus-phx is integrated into prometheus-phoenix:
154 # Pleroma.Web.Endpoint.Instrumenter.setup()
155 PrometheusPhx.setup()
158 defp cachex_children do
160 build_cachex("used_captcha", ttl_interval: seconds_valid_interval()),
161 build_cachex("user", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
162 build_cachex("object", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
163 build_cachex("rich_media", default_ttl: :timer.minutes(120), limit: 5000),
164 build_cachex("scrubber", limit: 2500),
165 build_cachex("idempotency", expiration: idempotency_expiration(), limit: 2500),
166 build_cachex("web_resp", limit: 2500),
167 build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
168 build_cachex("failed_proxy_url", limit: 2500),
169 build_cachex("banned_urls", default_ttl: :timer.hours(24 * 30), limit: 5_000),
170 build_cachex("chat_message_id_idempotency_key",
171 expiration: chat_message_id_idempotency_key_expiration(),
177 defp emoji_packs_expiration,
178 do: expiration(default: :timer.seconds(5 * 60), interval: :timer.seconds(60))
180 defp idempotency_expiration,
181 do: expiration(default: :timer.seconds(6 * 60 * 60), interval: :timer.seconds(60))
183 defp chat_message_id_idempotency_key_expiration,
184 do: expiration(default: :timer.minutes(2), interval: :timer.seconds(60))
186 defp seconds_valid_interval,
187 do: :timer.seconds(Config.get!([Pleroma.Captcha, :seconds_valid]))
189 @spec build_cachex(String.t(), keyword()) :: map()
190 def build_cachex(type, opts),
192 id: String.to_atom("cachex_" <> type),
193 start: {Cachex, :start_link, [String.to_atom(type <> "_cache"), opts]},
197 defp shout_enabled?, do: Config.get([:shout, :enabled])
199 defp dont_run_in_test(env) when env in [:test, :benchmark], do: []
201 defp dont_run_in_test(_) do
205 name: Pleroma.Web.Streamer.registry(),
207 partitions: System.schedulers_online()
209 ] ++ background_migrators()
212 defp background_migrators do
214 Pleroma.Migrators.HashtagsTableMigrator
218 defp shout_child(true) do
220 Pleroma.Web.ShoutChannel.ShoutChannelState,
221 {Phoenix.PubSub, [name: Pleroma.PubSub, adapter: Phoenix.PubSub.PG2]}
225 defp shout_child(_), do: []
227 defp task_children(:test) do
231 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
237 defp task_children(_) do
241 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
245 id: :internal_fetch_init,
246 start: {Task, :start_link, [&Pleroma.Web.ActivityPub.InternalFetchActor.init/0]},
252 def elasticsearch_children do
253 config = Config.get([Pleroma.Search, :module])
255 if config == Pleroma.Search.Elasticsearch do
256 [Pleroma.Search.Elasticsearch.Cluster]
262 @spec limiters_setup() :: :ok
263 def limiters_setup do
264 config = Config.get(ConcurrentLimiter, [])
267 Pleroma.Web.RichMedia.Helpers,
268 Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy,
271 |> Enum.each(fn module ->
272 mod_config = Keyword.get(config, module, [])
274 max_running = Keyword.get(mod_config, :max_running, 5)
275 max_waiting = Keyword.get(mod_config, :max_waiting, 5)
277 ConcurrentLimiter.new(module, max_running, max_waiting)