Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

276 lines
8.0KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Application do
  5. use Application
  6. import Cachex.Spec
  7. alias Pleroma.Config
  8. require Logger
  9. @name Mix.Project.config()[:name]
  10. @version Mix.Project.config()[:version]
  11. @repository Mix.Project.config()[:source_url]
  12. @env Mix.env()
  13. def name, do: @name
  14. def version, do: @version
  15. def named_version, do: @name <> " " <> @version
  16. def repository, do: @repository
  17. def user_agent do
  18. if Process.whereis(Pleroma.Web.Endpoint) do
  19. case Config.get([:http, :user_agent], :default) do
  20. :default ->
  21. info = "#{Pleroma.Web.base_url()} <#{Config.get([:instance, :email], "")}>"
  22. named_version() <> "; " <> info
  23. custom ->
  24. custom
  25. end
  26. else
  27. # fallback, if endpoint is not started yet
  28. "Pleroma Data Loader"
  29. end
  30. end
  31. # See http://elixir-lang.org/docs/stable/elixir/Application.html
  32. # for more information on OTP Applications
  33. def start(_type, _args) do
  34. # Scrubbers are compiled at runtime and therefore will cause a conflict
  35. # every time the application is restarted, so we disable module
  36. # conflicts at runtime
  37. Code.compiler_options(ignore_module_conflict: true)
  38. # Disable warnings_as_errors at runtime, it breaks Phoenix live reload
  39. # due to protocol consolidation warnings
  40. Code.compiler_options(warnings_as_errors: false)
  41. Pleroma.Telemetry.Logger.attach()
  42. Config.Holder.save_default()
  43. Pleroma.HTML.compile_scrubbers()
  44. Pleroma.Config.Oban.warn()
  45. Config.DeprecationWarnings.warn()
  46. Pleroma.Web.Plugs.HTTPSecurityPlug.warn_if_disabled()
  47. Pleroma.ApplicationRequirements.verify!()
  48. setup_instrumenters()
  49. load_custom_modules()
  50. Pleroma.Docs.JSON.compile()
  51. adapter = Application.get_env(:tesla, :adapter)
  52. if adapter == Tesla.Adapter.Gun do
  53. if version = Pleroma.OTPVersion.version() do
  54. [major, minor] =
  55. version
  56. |> String.split(".")
  57. |> Enum.map(&String.to_integer/1)
  58. |> Enum.take(2)
  59. if (major == 22 and minor < 2) or major < 22 do
  60. raise "
  61. !!!OTP VERSION WARNING!!!
  62. 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.
  63. "
  64. end
  65. else
  66. raise "
  67. !!!OTP VERSION WARNING!!!
  68. To support correct handling of unordered certificates chains - OTP version must be > 22.2.
  69. "
  70. end
  71. end
  72. # Define workers and child supervisors to be supervised
  73. children =
  74. [
  75. Pleroma.Repo,
  76. Config.TransferTask,
  77. Pleroma.Emoji,
  78. Pleroma.Web.Plugs.RateLimiter.Supervisor
  79. ] ++
  80. cachex_children() ++
  81. http_children(adapter, @env) ++
  82. [
  83. Pleroma.Stats,
  84. Pleroma.JobQueueMonitor,
  85. {Majic.Pool, [name: Pleroma.MajicPool, pool_size: Config.get([:majic_pool, :size], 2)]},
  86. {Oban, Config.get(Oban)}
  87. ] ++
  88. task_children(@env) ++
  89. dont_run_in_test(@env) ++
  90. chat_child(chat_enabled?()) ++
  91. [
  92. Pleroma.Web.Endpoint,
  93. Pleroma.Gopher.Server
  94. ]
  95. # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
  96. # for other strategies and supported options
  97. opts = [strategy: :one_for_one, name: Pleroma.Supervisor]
  98. Supervisor.start_link(children, opts)
  99. end
  100. def load_custom_modules do
  101. dir = Config.get([:modules, :runtime_dir])
  102. if dir && File.exists?(dir) do
  103. dir
  104. |> Pleroma.Utils.compile_dir()
  105. |> case do
  106. {:error, _errors, _warnings} ->
  107. raise "Invalid custom modules"
  108. {:ok, modules, _warnings} ->
  109. if @env != :test do
  110. Enum.each(modules, fn mod ->
  111. Logger.info("Custom module loaded: #{inspect(mod)}")
  112. end)
  113. end
  114. :ok
  115. end
  116. end
  117. end
  118. defp setup_instrumenters do
  119. require Prometheus.Registry
  120. if Application.get_env(:prometheus, Pleroma.Repo.Instrumenter) do
  121. :ok =
  122. :telemetry.attach(
  123. "prometheus-ecto",
  124. [:pleroma, :repo, :query],
  125. &Pleroma.Repo.Instrumenter.handle_event/4,
  126. %{}
  127. )
  128. Pleroma.Repo.Instrumenter.setup()
  129. end
  130. Pleroma.Web.Endpoint.MetricsExporter.setup()
  131. Pleroma.Web.Endpoint.PipelineInstrumenter.setup()
  132. # Note: disabled until prometheus-phx is integrated into prometheus-phoenix:
  133. # Pleroma.Web.Endpoint.Instrumenter.setup()
  134. PrometheusPhx.setup()
  135. end
  136. defp cachex_children do
  137. [
  138. build_cachex("used_captcha", ttl_interval: seconds_valid_interval()),
  139. build_cachex("user", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
  140. build_cachex("object", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
  141. build_cachex("rich_media", default_ttl: :timer.minutes(120), limit: 5000),
  142. build_cachex("scrubber", limit: 2500),
  143. build_cachex("idempotency", expiration: idempotency_expiration(), limit: 2500),
  144. build_cachex("web_resp", limit: 2500),
  145. build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
  146. build_cachex("failed_proxy_url", limit: 2500),
  147. build_cachex("banned_urls", default_ttl: :timer.hours(24 * 30), limit: 5_000),
  148. build_cachex("chat_message_id_idempotency_key",
  149. expiration: chat_message_id_idempotency_key_expiration(),
  150. limit: 500_000
  151. )
  152. ]
  153. end
  154. defp emoji_packs_expiration,
  155. do: expiration(default: :timer.seconds(5 * 60), interval: :timer.seconds(60))
  156. defp idempotency_expiration,
  157. do: expiration(default: :timer.seconds(6 * 60 * 60), interval: :timer.seconds(60))
  158. defp chat_message_id_idempotency_key_expiration,
  159. do: expiration(default: :timer.minutes(2), interval: :timer.seconds(60))
  160. defp seconds_valid_interval,
  161. do: :timer.seconds(Config.get!([Pleroma.Captcha, :seconds_valid]))
  162. @spec build_cachex(String.t(), keyword()) :: map()
  163. def build_cachex(type, opts),
  164. do: %{
  165. id: String.to_atom("cachex_" <> type),
  166. start: {Cachex, :start_link, [String.to_atom(type <> "_cache"), opts]},
  167. type: :worker
  168. }
  169. defp chat_enabled?, do: Config.get([:chat, :enabled])
  170. defp dont_run_in_test(env) when env in [:test, :benchmark], do: []
  171. defp dont_run_in_test(_) do
  172. [
  173. {Registry,
  174. [
  175. name: Pleroma.Web.Streamer.registry(),
  176. keys: :duplicate,
  177. partitions: System.schedulers_online()
  178. ]}
  179. ]
  180. end
  181. defp chat_child(true) do
  182. [
  183. Pleroma.Web.ChatChannel.ChatChannelState,
  184. {Phoenix.PubSub, [name: Pleroma.PubSub, adapter: Phoenix.PubSub.PG2]}
  185. ]
  186. end
  187. defp chat_child(_), do: []
  188. defp task_children(:test) do
  189. [
  190. %{
  191. id: :web_push_init,
  192. start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
  193. restart: :temporary
  194. }
  195. ]
  196. end
  197. defp task_children(_) do
  198. [
  199. %{
  200. id: :web_push_init,
  201. start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
  202. restart: :temporary
  203. },
  204. %{
  205. id: :internal_fetch_init,
  206. start: {Task, :start_link, [&Pleroma.Web.ActivityPub.InternalFetchActor.init/0]},
  207. restart: :temporary
  208. }
  209. ]
  210. end
  211. # start hackney and gun pools in tests
  212. defp http_children(_, :test) do
  213. http_children(Tesla.Adapter.Hackney, nil) ++ http_children(Tesla.Adapter.Gun, nil)
  214. end
  215. defp http_children(Tesla.Adapter.Hackney, _) do
  216. pools = [:federation, :media]
  217. pools =
  218. if Config.get([Pleroma.Upload, :proxy_remote]) do
  219. [:upload | pools]
  220. else
  221. pools
  222. end
  223. for pool <- pools do
  224. options = Config.get([:hackney_pools, pool])
  225. :hackney_pool.child_spec(pool, options)
  226. end
  227. end
  228. defp http_children(Tesla.Adapter.Gun, _) do
  229. Pleroma.Gun.ConnectionPool.children() ++
  230. [{Task, &Pleroma.HTTP.AdapterHelper.Gun.limiter_setup/0}]
  231. end
  232. defp http_children(_, _), do: []
  233. end