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