%%%=================================================================== %%% Socket Monitor %%% Monitors Wade server process and notifies supervisor when it dies %%%=================================================================== -module(sock_monitor). %% API -export([start_link/0]). %% @doc Start monitoring Wade server -spec start_link() -> {ok, pid()} | {error, term()}. start_link() -> Pid = spawn_link(fun monitor_loop/0), {ok, Pid}. %% @doc Simple monitoring loop -spec monitor_loop() -> no_return(). monitor_loop() -> case whereis(wade) of undefined -> % Wade server not found, just wait and check again % Don't restart the bot immediately io:format("Wade server not registered (this is normal for WebSocket-only usage)~n"), timer:sleep(60000), % Check every minute monitor_loop(); WadePid when is_pid(WadePid) -> % Found Wade server, monitor it io:format("Monitoring Wade server (~p)~n", [WadePid]), MonitorRef = erlang:monitor(process, WadePid), receive {'DOWN', MonitorRef, process, WadePid, Reason} -> io:format("Wade server crashed with reason: ~p~n", [Reason]), io:format("Notifying supervisor to restart bot~n"), discord_bot_light_sup ! {wade_crash}, monitor_loop() end end.