Switch to manual Supervisor child specifications instead of Supervisor.Spec
[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 hackney_pool_children() ++
120 [
121 %{
122 id: Pleroma.Web.Federator.RetryQueue,
123 start: {Pleroma.Web.Federator.RetryQueue, :start_link, []}
124 },
125 %{
126 id: Pleroma.Web.OAuth.Token.CleanWorker,
127 start: {Pleroma.Web.OAuth.Token.CleanWorker, :start_link, []}
128 },
129 %{
130 id: Pleroma.Stats,
131 start: {Pleroma.Stats, :start_link, []}
132 },
133 %{
134 id: :web_push_init,
135 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
136 restart: :temporary
137 },
138 %{
139 id: :federator_init,
140 start: {Task, :start_link, [&Pleroma.Web.Federator.init/0]},
141 restart: :temporary
142 }
143 ] ++
144 streamer_child() ++
145 chat_child() ++
146 [
147 # Start the endpoint when the application starts
148 %{
149 id: Pleroma.Web.Endpoint,
150 start: {Pleroma.Web.Endpoint, :start_link, []},
151 type: :supervisor
152 },
153 %{id: Pleroma.Gopher.Server, start: {Pleroma.Gopher.Server, :start_link, []}}
154 ]
155
156 # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
157 # for other strategies and supported options
158 opts = [strategy: :one_for_one, name: Pleroma.Supervisor]
159 Supervisor.start_link(children, opts)
160 end
161
162 defp setup_instrumenters do
163 require Prometheus.Registry
164
165 if Application.get_env(:prometheus, Pleroma.Repo.Instrumenter) do
166 :ok =
167 :telemetry.attach(
168 "prometheus-ecto",
169 [:pleroma, :repo, :query],
170 &Pleroma.Repo.Instrumenter.handle_event/4,
171 %{}
172 )
173
174 Pleroma.Repo.Instrumenter.setup()
175 end
176
177 Prometheus.Registry.register_collector(:prometheus_process_collector)
178 Pleroma.Web.Endpoint.MetricsExporter.setup()
179 Pleroma.Web.Endpoint.PipelineInstrumenter.setup()
180 Pleroma.Web.Endpoint.Instrumenter.setup()
181 end
182
183 def enabled_hackney_pools do
184 [:media] ++
185 if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Hackney do
186 [:federation]
187 else
188 []
189 end ++
190 if Pleroma.Config.get([Pleroma.Uploader, :proxy_remote]) do
191 [:upload]
192 else
193 []
194 end
195 end
196
197 if Mix.env() == :test do
198 defp streamer_child, do: []
199 defp chat_child, do: []
200 else
201 defp streamer_child do
202 [%{id: Pleroma.Web.Streamer, start: {Pleroma.Web.Streamer, :start_link, []}}]
203 end
204
205 defp chat_child do
206 if Pleroma.Config.get([:chat, :enabled]) do
207 [
208 %{
209 id: Pleroma.Web.ChatChannel.ChatChannelState,
210 start: {Pleroma.Web.ChatChannel.ChatChannelState, :start_link, []}
211 }
212 ]
213 else
214 []
215 end
216 end
217 end
218
219 defp hackney_pool_children do
220 for pool <- enabled_hackney_pools() do
221 options = Pleroma.Config.get([:hackney_pools, pool])
222 :hackney_pool.child_spec(pool, options)
223 end
224 end
225 end