Skip to content

Commit ee3f387

Browse files
authored
[RTC-515] Fix ensure_epmd_started! (#190)
* [RTC-515] Fix `ensure_epmd_started!` * Fix typo
1 parent 5a96434 commit ee3f387

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

lib/jellyfish/application.ex

+28-6
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ defmodule Jellyfish.Application do
77

88
require Logger
99

10-
# seconds
10+
# in seconds
1111
@resource_manager_opts %{interval: 600, recording_timeout: 3_600}
1212

13+
# in milliseconds
14+
@epmd_timeout 5_000
15+
@epmd_pgrep_interval 500
16+
1317
@impl true
1418
def start(_type, _args) do
1519
scrape_interval = Application.fetch_env!(:jellyfish, :webrtc_metrics_scrape_interval)
@@ -104,20 +108,38 @@ defmodule Jellyfish.Application do
104108
end
105109

106110
defp ensure_epmd_started!() do
107-
case System.cmd("epmd", ["-daemon"]) do
108-
{_output, 0} ->
109-
:ok
111+
try do
112+
{_output, 0} = System.cmd("epmd", ["-daemon"])
113+
:ok = Task.async(&ensure_epmd_running/0) |> Task.await(@epmd_timeout)
110114

111-
_other ->
115+
:ok
116+
catch
117+
_exit_or_error, _e ->
112118
raise """
113119
Couldn't start epmd daemon.
114-
Epmd is required to run Jellyfish in a distributed mode.
120+
Epmd is required to run Jellyfish in distributed mode.
115121
You can try to start it manually with:
116122
117123
epmd -daemon
118124
119125
and run Jellyfish again.
120126
"""
121127
end
128+
129+
:ok
130+
end
131+
132+
defp ensure_epmd_running() do
133+
with {:pgrep, {_output, 0}} <- {:pgrep, System.cmd("pgrep", ["epmd"])},
134+
{:epmd, {_output, 0}} <- {:epmd, System.cmd("epmd", ["-names"])} do
135+
:ok
136+
else
137+
{:pgrep, _other} ->
138+
Process.sleep(@epmd_pgrep_interval)
139+
ensure_epmd_running()
140+
141+
{:epmd, _other} ->
142+
:error
143+
end
122144
end
123145
end

0 commit comments

Comments
 (0)