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