Merge branch 'issue/1348' 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.Daemons.ScheduledActivityDaemon,
39 Pleroma.Daemons.ActivityExpirationDaemon,
40 Pleroma.Plugs.RateLimiter.Supervisor
41 ] ++
42 cachex_children() ++
43 hackney_pool_children() ++
44 [
45 Pleroma.Stats,
46 Pleroma.JobQueueMonitor,
47 {Oban, Pleroma.Config.get(Oban)}
48 ] ++
49 task_children(@env) ++
50 oauth_cleanup_child(oauth_cleanup_enabled?()) ++
51 streamer_child(@env) ++
52 chat_child(@env, chat_enabled?()) ++
53 [
54 Pleroma.Web.Endpoint,
55 Pleroma.Gopher.Server
56 ]
57
58 # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
59 # for other strategies and supported options
60 opts = [strategy: :one_for_one, name: Pleroma.Supervisor]
61 Supervisor.start_link(children, opts)
62 end
63
64 defp setup_instrumenters do
65 require Prometheus.Registry
66
67 if Application.get_env(:prometheus, Pleroma.Repo.Instrumenter) do
68 :ok =
69 :telemetry.attach(
70 "prometheus-ecto",
71 [:pleroma, :repo, :query],
72 &Pleroma.Repo.Instrumenter.handle_event/4,
73 %{}
74 )
75
76 Pleroma.Repo.Instrumenter.setup()
77 end
78
79 Pleroma.Web.Endpoint.MetricsExporter.setup()
80 Pleroma.Web.Endpoint.PipelineInstrumenter.setup()
81 Pleroma.Web.Endpoint.Instrumenter.setup()
82 end
83
84 def enabled_hackney_pools do
85 [:media] ++
86 if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Hackney do
87 [:federation]
88 else
89 []
90 end ++
91 if Pleroma.Config.get([Pleroma.Upload, :proxy_remote]) do
92 [:upload]
93 else
94 []
95 end
96 end
97
98 defp cachex_children do
99 [
100 build_cachex("used_captcha", ttl_interval: seconds_valid_interval()),
101 build_cachex("user", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
102 build_cachex("object", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
103 build_cachex("rich_media", default_ttl: :timer.minutes(120), limit: 5000),
104 build_cachex("scrubber", limit: 2500),
105 build_cachex("idempotency", expiration: idempotency_expiration(), limit: 2500),
106 build_cachex("web_resp", limit: 2500),
107 build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
108 build_cachex("failed_proxy_url", limit: 2500)
109 ]
110 end
111
112 defp emoji_packs_expiration,
113 do: expiration(default: :timer.seconds(5 * 60), interval: :timer.seconds(60))
114
115 defp idempotency_expiration,
116 do: expiration(default: :timer.seconds(6 * 60 * 60), interval: :timer.seconds(60))
117
118 defp seconds_valid_interval,
119 do: :timer.seconds(Pleroma.Config.get!([Pleroma.Captcha, :seconds_valid]))
120
121 defp build_cachex(type, opts),
122 do: %{
123 id: String.to_atom("cachex_" <> type),
124 start: {Cachex, :start_link, [String.to_atom(type <> "_cache"), opts]},
125 type: :worker
126 }
127
128 defp chat_enabled?, do: Pleroma.Config.get([:chat, :enabled])
129
130 defp oauth_cleanup_enabled?,
131 do: Pleroma.Config.get([:oauth2, :clean_expired_tokens], false)
132
133 defp streamer_child(:test), do: []
134
135 defp streamer_child(_) do
136 [Pleroma.Web.Streamer.supervisor()]
137 end
138
139 defp oauth_cleanup_child(true),
140 do: [Pleroma.Web.OAuth.Token.CleanWorker]
141
142 defp oauth_cleanup_child(_), do: []
143
144 defp chat_child(:test, _), do: []
145
146 defp chat_child(_env, true) do
147 [Pleroma.Web.ChatChannel.ChatChannelState]
148 end
149
150 defp chat_child(_, _), do: []
151
152 defp hackney_pool_children do
153 for pool <- enabled_hackney_pools() do
154 options = Pleroma.Config.get([:hackney_pools, pool])
155 :hackney_pool.child_spec(pool, options)
156 end
157 end
158
159 defp task_children(:test) do
160 [
161 %{
162 id: :web_push_init,
163 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
164 restart: :temporary
165 }
166 ]
167 end
168
169 defp task_children(_) do
170 [
171 %{
172 id: :web_push_init,
173 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
174 restart: :temporary
175 },
176 %{
177 id: :internal_fetch_init,
178 start: {Task, :start_link, [&Pleroma.Web.ActivityPub.InternalFetchActor.init/0]},
179 restart: :temporary
180 }
181 ]
182 end
183 end