Skip to content

Commit

Permalink
Tau - move keepalive out of the tau_server supervision tree
Browse files Browse the repository at this point in the history
It's now more top level (is it ok doing this?) so it doesn't get restarted - this process should only be started once and left alone.
  • Loading branch information
samaaron committed Sep 28, 2021
1 parent 55aee00 commit a3b2db3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
1 change: 1 addition & 0 deletions app/server/beam/tau/lib/tau.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use Application

# Although we don't use the supervisor name below directly,
# it can be useful when debugging or introspecting the system.
:tau_keepalive.start_link(daemon_port)
:tau_server_sup.start_link()
end

Expand Down
16 changes: 8 additions & 8 deletions app/server/beam/tau/src/tau_server/tau_keepalive.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@

-module(tau_keepalive).

-export([start/1, init/1, loop/1]).
-export([start_link/1, init/1, loop/1]).

start(DaemonPortNum) ->
spawn(?MODULE, init, [DaemonPortNum]).
start_link(DaemonPortNum) ->
spawn_link(?MODULE, init, [DaemonPortNum]).

init(DaemonPortNum) ->
logger:info("connecting to Daemon via TCP...", []),
{ok, DaemonSocket} = gen_tcp:connect({127,0,0,1}, DaemonPortNum, [
binary,
{active, true},
{packet, 4},
{keepalive, true}
{keepalive, false}
]),
OSPid = os:getpid(),
PidMsg = osc:encode(["/tau_pid", OSPid]),
logger:info("Sending Pid ~p to Daemon...", [OSPid]),
gen_tcp:send(DaemonSocket, PidMsg),
KillSwitch = erlang:start_timer(5000, self(), trigger_kill_switch),
KillSwitch = erlang:send_after(5000, self(), trigger_kill_switch),
logger:info("Waiting for keepalive messages..."),
loop(KillSwitch).

Expand All @@ -41,8 +41,8 @@ loop(KillSwitch) ->
try osc:decode(Bin) of
{cmd, ["/system/keepalive"]} ->
logger:debug("Received keepalive message from Daemon", []),
erlang:cancel_timer(KillSwitch),
NewKillSwitch = erlang:start_timer(5000, self(), trigger_kill_switch),
ok = erlang:cancel_timer(KillSwitch, [{async, true}, {info, false}]),
NewKillSwitch = erlang:send_after(5000, self(), trigger_kill_switch),
?MODULE:loop(NewKillSwitch);
Other ->
logger:error("Unexpected message from Daemon:~p", [Other]),
Expand All @@ -53,7 +53,7 @@ loop(KillSwitch) ->
[Bin, Class, Term, Trace]),
?MODULE:loop(KillSwitch)
end;
{timeout, _Timer, trigger_kill_switch} ->
trigger_kill_switch ->
logger:info("Tau kill switch activated. Shutting down....", []),
init:stop();
Any ->
Expand Down
3 changes: 0 additions & 3 deletions app/server/beam/tau/src/tau_server/tau_server_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ start_link(CueServer, MIDIServer, LinkServer) ->
init(Parent, CueServer, MIDIServer, LinkServer) ->
register(?SERVER, self()),
APIPort = application:get_env(?APPLICATION, api_port, undefined),
DaemonPort = application:get_env(?APPLICATION, daemon_port, undefined),

logger:info("~n"
"+--------------------------------------+~n"
" This is the Sonic Pi API Server ~n"
Expand All @@ -87,7 +85,6 @@ init(Parent, CueServer, MIDIServer, LinkServer) ->

{ok, APISocket} = gen_udp:open(APIPort, [binary, {ip, loopback}]),

_KeepAlivePid = tau_keepalive:start(DaemonPort),

%% tell parent we have allocated resources and are up and running
proc_lib:init_ack(Parent, {ok, self()}),
Expand Down

0 comments on commit a3b2db3

Please sign in to comment.