Merge branch 'develop' into issue/1276
[akkoma] / lib / pleroma / application.ex
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
5 defmodule Pleroma.Application do
6 import Cachex.Spec
7 use Application
8 require Logger
9
10 @name Mix.Project.config()[:name]
11 @version Mix.Project.config()[:version]
12 @repository Mix.Project.config()[:source_url]
13 @env Mix.env()
14
15 def name, do: @name
16 def version, do: @version
17 def named_version, do: @name <> " " <> @version
18 def repository, do: @repository
19
20 def user_agent do
21 case Pleroma.Config.get([:http, :user_agent], :default) do
22 :default ->
23 info = "#{Pleroma.Web.base_url()} <#{Pleroma.Config.get([:instance, :email], "")}>"
24 named_version() <> "; " <> info
25
26 custom ->
27 custom
28 end
29 end
30
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 Pleroma.HTML.compile_scrubbers()
35 Pleroma.Config.DeprecationWarnings.warn()
36 Pleroma.Plugs.HTTPSecurityPlug.warn_if_disabled()
37 Pleroma.Repo.check_migrations_applied!()
38 setup_instrumenters()
39 load_custom_modules()
40
41 # Define workers and child supervisors to be supervised
42 children =
43 [
44 Pleroma.Repo,
45 Pleroma.Config.TransferTask,
46 Pleroma.Emoji,
47 Pleroma.Captcha,
48 Pleroma.Plugs.RateLimiter.Supervisor
49 ] ++
50 cachex_children() ++
51 hackney_pool_children() ++
52 [
53 Pleroma.Stats,
54 Pleroma.JobQueueMonitor,
55 {Oban, Pleroma.Config.get(Oban)}
56 ] ++
57 task_children(@env) ++
58 streamer_child(@env) ++
59 chat_child(@env, chat_enabled?()) ++
60 [
61 Pleroma.Web.Endpoint,
62 Pleroma.Gopher.Server
63 ]
64
65 # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
66 # for other strategies and supported options
67 opts = [strategy: :one_for_one, name: Pleroma.Supervisor]
68 Supervisor.start_link(children, opts)
69 end
70
71 def load_custom_modules do
72 dir = Pleroma.Config.get([:modules, :runtime_dir])
73
74 if dir && File.exists?(dir) do
75 dir
76 |> Pleroma.Utils.compile_dir()
77 |> case do
78 {:error, _errors, _warnings} ->
79 raise "Invalid custom modules"
80
81 {:ok, modules, _warnings} ->
82 if @env != :test do
83 Enum.each(modules, fn mod ->
84 Logger.info("Custom module loaded: #{inspect(mod)}")
85 end)
86 end
87
88 :ok
89 end
90 end
91 end
92
93 defp setup_instrumenters do
94 require Prometheus.Registry
95
96 if Application.get_env(:prometheus, Pleroma.Repo.Instrumenter) do
97 :ok =
98 :telemetry.attach(
99 "prometheus-ecto",
100 [:pleroma, :repo, :query],
101 &Pleroma.Repo.Instrumenter.handle_event/4,
102 %{}
103 )
104
105 Pleroma.Repo.Instrumenter.setup()
106 end
107
108 Pleroma.Web.Endpoint.MetricsExporter.setup()
109 Pleroma.Web.Endpoint.PipelineInstrumenter.setup()
110 Pleroma.Web.Endpoint.Instrumenter.setup()
111 end
112
113 def enabled_hackney_pools do
114 [:media] ++
115 if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Hackney do
116 [:federation]
117 else
118 []
119 end ++
120 if Pleroma.Config.get([Pleroma.Upload, :proxy_remote]) do
121 [:upload]
122 else
123 []
124 end
125 end
126
127 defp cachex_children do
128 [
129 build_cachex("used_captcha", ttl_interval: seconds_valid_interval()),
130 build_cachex("user", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
131 build_cachex("object", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
132 build_cachex("rich_media", default_ttl: :timer.minutes(120), limit: 5000),
133 build_cachex("scrubber", limit: 2500),
134 build_cachex("idempotency", expiration: idempotency_expiration(), limit: 2500),
135 build_cachex("web_resp", limit: 2500),
136 build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
137 build_cachex("failed_proxy_url", limit: 2500)
138 ]
139 end
140
141 defp emoji_packs_expiration,
142 do: expiration(default: :timer.seconds(5 * 60), interval: :timer.seconds(60))
143
144 defp idempotency_expiration,
145 do: expiration(default: :timer.seconds(6 * 60 * 60), interval: :timer.seconds(60))
146
147 defp seconds_valid_interval,
148 do: :timer.seconds(Pleroma.Config.get!([Pleroma.Captcha, :seconds_valid]))
149
150 defp build_cachex(type, opts),
151 do: %{
152 id: String.to_atom("cachex_" <> type),
153 start: {Cachex, :start_link, [String.to_atom(type <> "_cache"), opts]},
154 type: :worker
155 }
156
157 defp chat_enabled?, do: Pleroma.Config.get([:chat, :enabled])
158
159 defp streamer_child(:test), do: []
160
161 defp streamer_child(_) do
162 [Pleroma.Web.Streamer.supervisor()]
163 end
164
165 defp chat_child(_env, true) do
166 [Pleroma.Web.ChatChannel.ChatChannelState]
167 end
168
169 defp chat_child(_, _), do: []
170
171 defp hackney_pool_children do
172 for pool <- enabled_hackney_pools() do
173 options = Pleroma.Config.get([:hackney_pools, pool])
174 :hackney_pool.child_spec(pool, options)
175 end
176 end
177
178 defp task_children(:test) do
179 [
180 %{
181 id: :web_push_init,
182 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
183 restart: :temporary
184 }
185 ]
186 end
187
188 defp task_children(_) do
189 [
190 %{
191 id: :web_push_init,
192 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
193 restart: :temporary
194 },
195 %{
196 id: :internal_fetch_init,
197 start: {Task, :start_link, [&Pleroma.Web.ActivityPub.InternalFetchActor.init/0]},
198 restart: :temporary
199 }
200 ]
201 end
202 end