1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Application.StartUpDependencies do
@moduledoc """
Starts common and config-dependent Pleroma dependencies.
"""
alias Pleroma.Config
alias Pleroma.Web.Endpoint
require Cachex.Spec
require Logger
@type config_path() :: {atom(), atom()} | {atom(), atom(), [atom()]}
@type relation() :: {config_path(), module()}
@spec start_all(Pleroma.Application.env()) ::
:ok | {:error, {:already_started, pid()} | :max_children | term()}
def start_all(env) do
with :ok <- start_common_deps(env),
:ok <- start_config_dependent_deps(env) do
:ok
end
end
@spec adapter_module() :: module()
def adapter_module do
if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Gun do
Pleroma.Gun.GunSupervisor
else
Pleroma.HTTP.HackneySupervisor
end
end
@spec spec(module()) :: module() | {module(), keyword()}
def spec(Oban), do: {Oban, Config.get(Oban)}
def spec(Pleroma.Web.StreamerRegistry) do
{Registry,
[
name: Pleroma.Web.Streamer.registry(),
keys: :duplicate,
partitions: System.schedulers_online()
]}
end
def spec(child), do: child
@spec cachex_spec({String.t(), keyword()}) :: :supervisor.child_spec()
def cachex_spec({type, opts}) do
%{
id: String.to_atom("cachex_" <> type),
start: {Cachex, :start_link, [String.to_atom(type <> "_cache"), opts]},
type: :worker
}
end
defp start_common_deps(env) do
fun = fn child ->
DynamicSupervisor.start_child(Pleroma.Application.dynamic_supervisor(), spec(child))
end
[
Pleroma.Emoji,
Pleroma.Stats,
Pleroma.JobQueueMonitor,
{Majic.Pool, [name: Pleroma.MajicPool, pool_size: Config.get([:majic_pool, :size], 2)]},
%{
id: :web_push_init,
start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
restart: :temporary
}
]
|> add_cachex_deps()
|> maybe_add_init_internal_fetch_actor_task(env)
|> maybe_add_background_migrator(env)
|> start_while(fun)
end
defp start_config_dependent_deps(env) do
fun = fn child -> Pleroma.Application.ConfigDependentDeps.start_dependency(child) end
[
Pleroma.Web.Plugs.RateLimiter.Supervisor,
Oban,
Endpoint,
Pleroma.Gopher.Server
]
|> add_http_children(env)
|> maybe_add_streamer(env)
|> maybe_add_chat_child()
|> start_while(fun)
end
defp start_while(deps, fun) do
Enum.reduce_while(deps, :ok, fn child, acc ->
case fun.(child) do
{:ok, _} ->
{:cont, acc}
# consider this behavior is normal
:ignore ->
Logger.info("#{inspect(child)} is ignored.")
{:cont, acc}
error ->
Logger.error("Child #{inspect(child)} can't be started. #{inspect(error)}")
{:halt, error}
end
end)
end
@spec cachex_deps() :: [tuple()]
def cachex_deps do
captcha_clean_up_interval =
[Pleroma.Captcha, :seconds_valid]
|> Config.get!()
|> :timer.seconds()
[
{"used_captcha", expiration: Cachex.Spec.expiration(interval: captcha_clean_up_interval)},
{"user", expiration: cachex_expiration(25_000, 1000), limit: 2500},
{"object", expiration: cachex_expiration(25_000, 1000), limit: 2500},
{"rich_media",
expiration: Cachex.Spec.expiration(default: :timer.minutes(120)), limit: 5000},
{"scrubber", limit: 2500},
{"idempotency", expiration: cachex_expiration(21_600, 60), limit: 2500},
{"web_resp", limit: 2500},
{"emoji_packs", expiration: cachex_expiration(300, 60), limit: 10},
{"failed_proxy_url", limit: 2500},
{"banned_urls",
expiration: Cachex.Spec.expiration(default: :timer.hours(24 * 30)), limit: 5_000},
{"chat_message_id_idempotency_key",
expiration: cachex_expiration(:timer.minutes(2), :timer.seconds(60)), limit: 500_000}
]
end
defp add_cachex_deps(application_deps) do
cachex_deps()
|> Enum.reduce(application_deps, fn cachex_init_args, acc ->
[cachex_spec(cachex_init_args) | acc]
end)
end
defp cachex_expiration(default, interval) do
Cachex.Spec.expiration(default: :timer.seconds(default), interval: :timer.seconds(interval))
end
defp maybe_add_init_internal_fetch_actor_task(children, :test), do: children
defp maybe_add_init_internal_fetch_actor_task(children, _) do
[
%{
id: :internal_fetch_init,
start: {Task, :start_link, [&Pleroma.Web.ActivityPub.InternalFetchActor.init/0]},
restart: :temporary
}
| children
]
end
defp maybe_add_streamer(children, env) when env in [:test, :benchmark], do: children
defp maybe_add_streamer(children, _), do: [Pleroma.Web.Streamer.registry() | children]
defp maybe_add_background_migrator(children, env) when env in [:test, :benchmark], do: children
defp maybe_add_background_migrator(children, _) do
[Pleroma.Migrators.HashtagsTableMigrator | children]
end
defp add_http_children(children, :test) do
[Pleroma.HTTP.HackneySupervisor, Pleroma.Gun.GunSupervisor | children]
end
defp add_http_children(children, _), do: [adapter_module() | children]
defp maybe_add_chat_child(children) do
if Config.get([:chat, :enabled]) do
[Pleroma.Application.ChatSupervisor | children]
else
children
end
end
end
|