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