Merge remote-tracking branch 'upstream/develop' into admin-create-users
[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 import Supervisor.Spec
8
9 @name Mix.Project.config()[:name]
10 @version Mix.Project.config()[:version]
11 @repository Mix.Project.config()[:source_url]
12 def name, do: @name
13 def version, do: @version
14 def named_version, do: @name <> " " <> @version
15 def repository, do: @repository
16
17 def user_agent do
18 info = "#{Pleroma.Web.base_url()} <#{Pleroma.Config.get([:instance, :email], "")}>"
19 named_version() <> "; " <> info
20 end
21
22 # See http://elixir-lang.org/docs/stable/elixir/Application.html
23 # for more information on OTP Applications
24 def start(_type, _args) do
25 import Cachex.Spec
26
27 Pleroma.Config.DeprecationWarnings.warn()
28 setup_instrumenters()
29
30 # Define workers and child supervisors to be supervised
31 children =
32 [
33 # Start the Ecto repository
34 supervisor(Pleroma.Repo, []),
35 worker(Pleroma.Emoji, []),
36 worker(Pleroma.Captcha, []),
37 worker(
38 Cachex,
39 [
40 :used_captcha_cache,
41 [
42 ttl_interval: :timer.seconds(Pleroma.Config.get!([Pleroma.Captcha, :seconds_valid]))
43 ]
44 ],
45 id: :cachex_used_captcha_cache
46 ),
47 worker(
48 Cachex,
49 [
50 :user_cache,
51 [
52 default_ttl: 25_000,
53 ttl_interval: 1000,
54 limit: 2500
55 ]
56 ],
57 id: :cachex_user
58 ),
59 worker(
60 Cachex,
61 [
62 :object_cache,
63 [
64 default_ttl: 25_000,
65 ttl_interval: 1000,
66 limit: 2500
67 ]
68 ],
69 id: :cachex_object
70 ),
71 worker(
72 Cachex,
73 [
74 :rich_media_cache,
75 [
76 default_ttl: :timer.minutes(120),
77 limit: 5000
78 ]
79 ],
80 id: :cachex_rich_media
81 ),
82 worker(
83 Cachex,
84 [
85 :scrubber_cache,
86 [
87 limit: 2500
88 ]
89 ],
90 id: :cachex_scrubber
91 ),
92 worker(
93 Cachex,
94 [
95 :idempotency_cache,
96 [
97 expiration:
98 expiration(
99 default: :timer.seconds(6 * 60 * 60),
100 interval: :timer.seconds(60)
101 ),
102 limit: 2500
103 ]
104 ],
105 id: :cachex_idem
106 ),
107 worker(Pleroma.FlakeId, []),
108 worker(Pleroma.ScheduledActivityWorker, [])
109 ] ++
110 hackney_pool_children() ++
111 [
112 worker(Pleroma.Web.Federator.RetryQueue, []),
113 worker(Pleroma.Web.OAuth.Token.CleanWorker, []),
114 worker(Pleroma.Stats, []),
115 worker(Task, [&Pleroma.Web.Push.init/0], restart: :temporary, id: :web_push_init),
116 worker(Task, [&Pleroma.Web.Federator.init/0], restart: :temporary, id: :federator_init)
117 ] ++
118 streamer_child() ++
119 chat_child() ++
120 [
121 # Start the endpoint when the application starts
122 supervisor(Pleroma.Web.Endpoint, []),
123 worker(Pleroma.Gopher.Server, [])
124 ]
125
126 # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
127 # for other strategies and supported options
128 opts = [strategy: :one_for_one, name: Pleroma.Supervisor]
129 Supervisor.start_link(children, opts)
130 end
131
132 defp setup_instrumenters do
133 require Prometheus.Registry
134
135 if Application.get_env(:prometheus, Pleroma.Repo.Instrumenter) do
136 :ok =
137 :telemetry.attach(
138 "prometheus-ecto",
139 [:pleroma, :repo, :query],
140 &Pleroma.Repo.Instrumenter.handle_event/4,
141 %{}
142 )
143
144 Pleroma.Repo.Instrumenter.setup()
145 end
146
147 Prometheus.Registry.register_collector(:prometheus_process_collector)
148 Pleroma.Web.Endpoint.MetricsExporter.setup()
149 Pleroma.Web.Endpoint.PipelineInstrumenter.setup()
150 Pleroma.Web.Endpoint.Instrumenter.setup()
151 end
152
153 def enabled_hackney_pools do
154 [:media] ++
155 if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Hackney do
156 [:federation]
157 else
158 []
159 end ++
160 if Pleroma.Config.get([Pleroma.Uploader, :proxy_remote]) do
161 [:upload]
162 else
163 []
164 end
165 end
166
167 if Mix.env() == :test do
168 defp streamer_child, do: []
169 defp chat_child, do: []
170 else
171 defp streamer_child do
172 [worker(Pleroma.Web.Streamer, [])]
173 end
174
175 defp chat_child do
176 if Pleroma.Config.get([:chat, :enabled]) do
177 [worker(Pleroma.Web.ChatChannel.ChatChannelState, [])]
178 else
179 []
180 end
181 end
182 end
183
184 defp hackney_pool_children do
185 for pool <- enabled_hackney_pools() do
186 options = Pleroma.Config.get([:hackney_pools, pool])
187 :hackney_pool.child_spec(pool, options)
188 end
189 end
190 end