From a3b2db35827798c7508fb8f46a807410c0ee98cb Mon Sep 17 00:00:00 2001 From: Sam Aaron Date: Tue, 28 Sep 2021 15:19:46 +0100 Subject: [PATCH] Tau - move keepalive out of the tau_server supervision tree 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. --- app/server/beam/tau/lib/tau.ex | 1 + .../beam/tau/src/tau_server/tau_keepalive.erl | 16 ++++++++-------- .../beam/tau/src/tau_server/tau_server_api.erl | 3 --- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/app/server/beam/tau/lib/tau.ex b/app/server/beam/tau/lib/tau.ex index 544cb6ec79..9e4c48fd3e 100644 --- a/app/server/beam/tau/lib/tau.ex +++ b/app/server/beam/tau/lib/tau.ex @@ -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 diff --git a/app/server/beam/tau/src/tau_server/tau_keepalive.erl b/app/server/beam/tau/src/tau_server/tau_keepalive.erl index 3c38c10518..0543d296b4 100644 --- a/app/server/beam/tau/src/tau_server/tau_keepalive.erl +++ b/app/server/beam/tau/src/tau_server/tau_keepalive.erl @@ -14,10 +14,10 @@ -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...", []), @@ -25,13 +25,13 @@ init(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). @@ -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]), @@ -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 -> diff --git a/app/server/beam/tau/src/tau_server/tau_server_api.erl b/app/server/beam/tau/src/tau_server/tau_server_api.erl index ce547b6558..d036260905 100644 --- a/app/server/beam/tau/src/tau_server/tau_server_api.erl +++ b/app/server/beam/tau/src/tau_server/tau_server_api.erl @@ -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" @@ -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()}),