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