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