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