2ae052069da301d0032579b6a282f5bd7e40d5e9
[akkoma] / lib / pleroma / application.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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 setup_instrumenters()
37 load_custom_modules()
38
39 # Define workers and child supervisors to be supervised
40 children =
41 [
42 Pleroma.Repo,
43 Pleroma.Scheduler,
44 Pleroma.Config.TransferTask,
45 Pleroma.Emoji,
46 Pleroma.Captcha,
47 Pleroma.Daemons.ScheduledActivityDaemon,
48 Pleroma.Daemons.ActivityExpirationDaemon,
49 Pleroma.Plugs.RateLimiter.Supervisor
50 ] ++
51 cachex_children() ++
52 hackney_pool_children() ++
53 [
54 Pleroma.Stats,
55 Pleroma.JobQueueMonitor,
56 {Oban, Pleroma.Config.get(Oban)}
57 ] ++
58 task_children(@env) ++
59 oauth_cleanup_child(oauth_cleanup_enabled?()) ++
60 streamer_child(@env) ++
61 chat_child(@env, chat_enabled?()) ++
62 [
63 Pleroma.Web.Endpoint,
64 Pleroma.Gopher.Server
65 ]
66
67 # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
68 # for other strategies and supported options
69 opts = [strategy: :one_for_one, name: Pleroma.Supervisor]
70 Supervisor.start_link(children, opts)
71 end
72
73 def load_custom_modules do
74 dir = Pleroma.Config.get([:modules, :runtime_dir])
75
76 if dir && File.exists?(dir) do
77 dir
78 |> Pleroma.Utils.compile_dir()
79 |> case do
80 {:error, _errors, _warnings} ->
81 raise "Invalid custom modules"
82
83 {:ok, modules, _warnings} ->
84 if @env != :test do
85 Enum.each(modules, fn mod ->
86 Logger.info("Custom module loaded: #{inspect(mod)}")
87 end)
88 end
89
90 :ok
91 end
92 end
93 end
94
95 defp setup_instrumenters do
96 require Prometheus.Registry
97
98 if Application.get_env(:prometheus, Pleroma.Repo.Instrumenter) do
99 :ok =
100 :telemetry.attach(
101 "prometheus-ecto",
102 [:pleroma, :repo, :query],
103 &Pleroma.Repo.Instrumenter.handle_event/4,
104 %{}
105 )
106
107 Pleroma.Repo.Instrumenter.setup()
108 end
109
110 Pleroma.Web.Endpoint.MetricsExporter.setup()
111 Pleroma.Web.Endpoint.PipelineInstrumenter.setup()
112 Pleroma.Web.Endpoint.Instrumenter.setup()
113 end
114
115 def enabled_hackney_pools do
116 [:media] ++
117 if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Hackney do
118 [:federation]
119 else
120 []
121 end ++
122 if Pleroma.Config.get([Pleroma.Upload, :proxy_remote]) do
123 [:upload]
124 else
125 []
126 end
127 end
128
129 defp cachex_children do
130 [
131 build_cachex("used_captcha", ttl_interval: seconds_valid_interval()),
132 build_cachex("user", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
133 build_cachex("object", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
134 build_cachex("rich_media", default_ttl: :timer.minutes(120), limit: 5000),
135 build_cachex("scrubber", limit: 2500),
136 build_cachex("idempotency", expiration: idempotency_expiration(), limit: 2500),
137 build_cachex("web_resp", limit: 2500),
138 build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
139 build_cachex("failed_proxy_url", limit: 2500)
140 ]
141 end
142
143 defp emoji_packs_expiration,
144 do: expiration(default: :timer.seconds(5 * 60), interval: :timer.seconds(60))
145
146 defp idempotency_expiration,
147 do: expiration(default: :timer.seconds(6 * 60 * 60), interval: :timer.seconds(60))
148
149 defp seconds_valid_interval,
150 do: :timer.seconds(Pleroma.Config.get!([Pleroma.Captcha, :seconds_valid]))
151
152 defp build_cachex(type, opts),
153 do: %{
154 id: String.to_atom("cachex_" <> type),
155 start: {Cachex, :start_link, [String.to_atom(type <> "_cache"), opts]},
156 type: :worker
157 }
158
159 defp chat_enabled?, do: Pleroma.Config.get([:chat, :enabled])
160
161 defp oauth_cleanup_enabled?,
162 do: Pleroma.Config.get([:oauth2, :clean_expired_tokens], false)
163
164 defp streamer_child(:test), do: []
165
166 defp streamer_child(_) do
167 [Pleroma.Web.Streamer.supervisor()]
168 end
169
170 defp oauth_cleanup_child(true),
171 do: [Pleroma.Web.OAuth.Token.CleanWorker]
172
173 defp oauth_cleanup_child(_), do: []
174
175 defp chat_child(_env, true) do
176 [Pleroma.Web.ChatChannel.ChatChannelState]
177 end
178
179 defp chat_child(_, _), do: []
180
181 defp hackney_pool_children do
182 for pool <- enabled_hackney_pools() do
183 options = Pleroma.Config.get([:hackney_pools, pool])
184 :hackney_pool.child_spec(pool, options)
185 end
186 end
187
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
198 defp task_children(_) do
199 [
200 %{
201 id: :web_push_init,
202 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
203 restart: :temporary
204 },
205 %{
206 id: :internal_fetch_init,
207 start: {Task, :start_link, [&Pleroma.Web.ActivityPub.InternalFetchActor.init/0]},
208 restart: :temporary
209 }
210 ]
211 end
212 end