Fix runaway open connections on token refresh
This commit is contained in:
parent
7abbc2ff6c
commit
85532cf946
|
@ -7,7 +7,7 @@ defmodule Farmbot.Bootstrap.AuthTask do
|
|||
|
||||
# 30 minutes.
|
||||
@refresh_time 1.8e+6 |> round()
|
||||
# @refresh_time 10_000 |> round()
|
||||
# @refresh_time 5_000
|
||||
|
||||
@doc false
|
||||
def start_link() do
|
||||
|
@ -23,6 +23,10 @@ defmodule Farmbot.Bootstrap.AuthTask do
|
|||
{:ok, timer, :hibernate}
|
||||
end
|
||||
|
||||
def terminate(reason, _state) do
|
||||
Logger.error 1, "Token Refresh failed: #{inspect reason}"
|
||||
end
|
||||
|
||||
def handle_info(:refresh, _old_timer) do
|
||||
auth_task = Application.get_env(:farmbot, :behaviour)[:authorization]
|
||||
{email, pass, server} = {fetch_email(), fetch_pass(), fetch_server()}
|
||||
|
@ -56,17 +60,12 @@ defmodule Farmbot.Bootstrap.AuthTask do
|
|||
end
|
||||
|
||||
defp restart_transports do
|
||||
alias Farmbot.Bootstrap
|
||||
alias Farmbot.BotState
|
||||
bs_sup = Bootstrap.Supervisor
|
||||
tp_sup = BotState.Transport.Supervisor
|
||||
:ok = Supervisor.terminate_child(bs_sup, tp_sup)
|
||||
case Supervisor.restart_child(bs_sup, tp_sup) do
|
||||
{:ok, _} -> :ok
|
||||
{:error, :running} -> :ok
|
||||
{:error, {:already_started, _}} -> :ok
|
||||
err -> exit(err)
|
||||
transports = Application.get_env(:farmbot, :transport)
|
||||
# Logger.info 1, "restarting children: #{inspect transports}"
|
||||
for t <- transports do
|
||||
t.stop(:token_refresh)
|
||||
end
|
||||
:ok
|
||||
end
|
||||
|
||||
defp refresh_timer(pid, ms \\ @refresh_time) do
|
||||
|
|
|
@ -287,8 +287,7 @@ defmodule Farmbot.BotState do
|
|||
env: @env,
|
||||
node_name: nil,
|
||||
busy: false,
|
||||
sync_status: :sync_now,
|
||||
sync_status_two: nil,
|
||||
sync_status: :booting,
|
||||
locked: false
|
||||
},
|
||||
location_data: %{
|
||||
|
|
|
@ -20,6 +20,10 @@ defmodule Farmbot.BotState.Transport.AMQP do
|
|||
GenStage.start_link(__MODULE__, [], [name: __MODULE__])
|
||||
end
|
||||
|
||||
def stop(reason \\ :normal) do
|
||||
GenStage.stop(__MODULE__, reason)
|
||||
end
|
||||
|
||||
# GenStage callbacks
|
||||
|
||||
defmodule State do
|
||||
|
@ -73,7 +77,7 @@ defmodule Farmbot.BotState.Transport.AMQP do
|
|||
end
|
||||
|
||||
def terminate(reason, state) do
|
||||
if reason not in [:normal, :shutdown] do
|
||||
if reason not in [:normal, :shutdown, :token_refresh] do
|
||||
Logger.error 1, "AMQP Died: #{inspect reason}"
|
||||
update_config_value(:bool, "settings", "log_amqp_connected", true)
|
||||
end
|
||||
|
@ -85,7 +89,7 @@ defmodule Farmbot.BotState.Transport.AMQP do
|
|||
if state.conn, do: AMQP.Connection.close(state.conn)
|
||||
|
||||
# If the auth task is running, force it to reset.
|
||||
if Process.whereis(Farmbot.Bootstrap.AuthTask) do
|
||||
if Process.whereis(Farmbot.Bootstrap.AuthTask) && reason != :token_refresh do
|
||||
Farmbot.Bootstrap.AuthTask.force_refresh()
|
||||
end
|
||||
end
|
||||
|
|
|
@ -42,6 +42,10 @@ defmodule Farmbot.BotState.Transport.HTTP do
|
|||
GenStage.start_link(__MODULE__, [], [name: __MODULE__])
|
||||
end
|
||||
|
||||
def stop(reason) do
|
||||
GenStage.stop(__MODULE__, reason)
|
||||
end
|
||||
|
||||
def init([]) do
|
||||
s = ConfigStorage.get_config_value(:string, "authorization", "server")
|
||||
req = {'#{s}/api/public_key', []}
|
||||
|
|
|
@ -11,7 +11,7 @@ defmodule Farmbot.BotState.Transport.Supervisor do
|
|||
transports = Application.get_env(:farmbot, :transport)
|
||||
children = Enum.reduce(transports, [], fn(t, acc) ->
|
||||
if Code.ensure_loaded?(t) do
|
||||
acc ++ [worker(t, [])]
|
||||
acc ++ [worker(t, [], [id: t])]
|
||||
else
|
||||
acc
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue