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