664cf578e88f8da497edba186e6acfab2032afc7
[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 use Application
7
8 @name Mix.Project.config()[:name]
9 @version Mix.Project.config()[:version]
10 @repository Mix.Project.config()[:source_url]
11 def name, do: @name
12 def version, do: @version
13 def named_version, do: @name <> " " <> @version
14 def repository, do: @repository
15
16 def user_agent do
17 info = "#{Pleroma.Web.base_url()} <#{Pleroma.Config.get([:instance, :email], "")}>"
18 named_version() <> "; " <> info
19 end
20
21 # See http://elixir-lang.org/docs/stable/elixir/Application.html
22 # for more information on OTP Applications
23 def start(_type, _args) do
24 import Cachex.Spec
25
26 Pleroma.Config.DeprecationWarnings.warn()
27 setup_instrumenters()
28
29 # Define workers and child supervisors to be supervised
30 children =
31 [
32 # Start the Ecto repository
33 %{id: Pleroma.Repo, start: {Pleroma.Repo, :start_link, []}, type: :supervisor},
34 %{id: Pleroma.Emoji, start: {Pleroma.Emoji, :start_link, []}},
35 %{id: Pleroma.Captcha, start: {Pleroma.Captcha, :start_link, []}},
36 %{
37 id: :cachex_used_captcha_cache,
38 start:
39 {Cachex, :start_link,
40 [
41 :used_captcha_cache,
42 [
43 ttl_interval:
44 :timer.seconds(Pleroma.Config.get!([Pleroma.Captcha, :seconds_valid]))
45 ]
46 ]}
47 },
48 %{
49 id: :cachex_user,
50 start:
51 {Cachex, :start_link,
52 [
53 :user_cache,
54 [
55 default_ttl: 25_000,
56 ttl_interval: 1000,
57 limit: 2500
58 ]
59 ]}
60 },
61 %{
62 id: :cachex_object,
63 start:
64 {Cachex, :start_link,
65 [
66 :object_cache,
67 [
68 default_ttl: 25_000,
69 ttl_interval: 1000,
70 limit: 2500
71 ]
72 ]}
73 },
74 %{
75 id: :cachex_rich_media,
76 start:
77 {Cachex, :start_link,
78 [
79 :rich_media_cache,
80 [
81 default_ttl: :timer.minutes(120),
82 limit: 5000
83 ]
84 ]}
85 },
86 %{
87 id: :cachex_scrubber,
88 start:
89 {Cachex, :start_link,
90 [
91 :scrubber_cache,
92 [
93 limit: 2500
94 ]
95 ]}
96 },
97 %{
98 id: :cachex_idem,
99 start:
100 {Cachex, :start_link,
101 [
102 :idempotency_cache,
103 [
104 expiration:
105 expiration(
106 default: :timer.seconds(6 * 60 * 60),
107 interval: :timer.seconds(60)
108 ),
109 limit: 2500
110 ]
111 ]}
112 },
113 %{id: Pleroma.FlakeId, start: {Pleroma.FlakeId, :start_link, []}},
114 %{
115 id: Pleroma.ScheduledActivityWorker,
116 start: {Pleroma.ScheduledActivityWorker, :start_link, []}
117 },
118 %{
119 id: Pleroma.QuantumScheduler,
120 start: {Pleroma.QuantumScheduler, :start_link, []}
121 }
122 ] ++
123 hackney_pool_children() ++
124 [
125 %{
126 id: Pleroma.Web.Federator.RetryQueue,
127 start: {Pleroma.Web.Federator.RetryQueue, :start_link, []}
128 },
129 %{
130 id: Pleroma.Web.OAuth.Token.CleanWorker,
131 start: {Pleroma.Web.OAuth.Token.CleanWorker, :start_link, []}
132 },
133 %{
134 id: Pleroma.Stats,
135 start: {Pleroma.Stats, :start_link, []}
136 },
137 %{
138 id: :web_push_init,
139 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
140 restart: :temporary
141 },
142 %{
143 id: :federator_init,
144 start: {Task, :start_link, [&Pleroma.Web.Federator.init/0]},
145 restart: :temporary
146 }
147 ] ++
148 streamer_child() ++
149 chat_child() ++
150 [
151 # Start the endpoint when the application starts
152 %{
153 id: Pleroma.Web.Endpoint,
154 start: {Pleroma.Web.Endpoint, :start_link, []},
155 type: :supervisor
156 },
157 %{id: Pleroma.Gopher.Server, start: {Pleroma.Gopher.Server, :start_link, []}}
158 ]
159
160 # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
161 # for other strategies and supported options
162 opts = [strategy: :one_for_one, name: Pleroma.Supervisor]
163 result = Supervisor.start_link(children, opts)
164 :ok = after_supervisor_start()
165 result
166 end
167
168 defp setup_instrumenters do
169 require Prometheus.Registry
170
171 if Application.get_env(:prometheus, Pleroma.Repo.Instrumenter) do
172 :ok =
173 :telemetry.attach(
174 "prometheus-ecto",
175 [:pleroma, :repo, :query],
176 &Pleroma.Repo.Instrumenter.handle_event/4,
177 %{}
178 )
179
180 Pleroma.Repo.Instrumenter.setup()
181 end
182
183 Prometheus.Registry.register_collector(:prometheus_process_collector)
184 Pleroma.Web.Endpoint.MetricsExporter.setup()
185 Pleroma.Web.Endpoint.PipelineInstrumenter.setup()
186 Pleroma.Web.Endpoint.Instrumenter.setup()
187 end
188
189 def enabled_hackney_pools do
190 [:media] ++
191 if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Hackney do
192 [:federation]
193 else
194 []
195 end ++
196 if Pleroma.Config.get([Pleroma.Uploader, :proxy_remote]) do
197 [:upload]
198 else
199 []
200 end
201 end
202
203 if Mix.env() == :test do
204 defp streamer_child, do: []
205 defp chat_child, do: []
206 else
207 defp streamer_child do
208 [%{id: Pleroma.Web.Streamer, start: {Pleroma.Web.Streamer, :start_link, []}}]
209 end
210
211 defp chat_child do
212 if Pleroma.Config.get([:chat, :enabled]) do
213 [
214 %{
215 id: Pleroma.Web.ChatChannel.ChatChannelState,
216 start: {Pleroma.Web.ChatChannel.ChatChannelState, :start_link, []}
217 }
218 ]
219 else
220 []
221 end
222 end
223 end
224
225 defp hackney_pool_children do
226 for pool <- enabled_hackney_pools() do
227 options = Pleroma.Config.get([:hackney_pools, pool])
228 :hackney_pool.child_spec(pool, options)
229 end
230 end
231
232 defp after_supervisor_start do
233 with digest_config <- Application.get_env(:pleroma, :email_notifications)[:digest],
234 true <- digest_config[:active],
235 %Crontab.CronExpression{} = schedule <-
236 Crontab.CronExpression.Parser.parse!(digest_config[:schedule]) do
237 Pleroma.QuantumScheduler.new_job()
238 |> Quantum.Job.set_name(:digest_emails)
239 |> Quantum.Job.set_schedule(schedule)
240 |> Quantum.Job.set_task(&Pleroma.DigestEmailWorker.run/0)
241 |> Pleroma.QuantumScheduler.add_job()
242 end
243
244 :ok
245 end
246 end