logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

application.ex (9630B)


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