farmbot_os/farmbot_os/platform/target/network/dns_task.ex

65 lines
1.8 KiB
Elixir
Raw Normal View History

2018-08-16 17:16:38 -06:00
defmodule Farmbot.Target.Network.DnsTask do
require Farmbot.Logger
2018-08-16 17:16:38 -06:00
use GenServer
import Farmbot.Target.Network, only: [test_dns: 0]
import Farmbot.Config, only: [get_config_value: 3]
@default_timeout_ms 45_000
2018-08-16 17:16:38 -06:00
def start_link(args) do
GenServer.start_link(__MODULE__, args, name: __MODULE__)
2018-08-16 17:16:38 -06:00
end
def init([]) do
# Block and reset if after 10 tries
# resolution doesn't work.
block_check(true)
{:ok, nil, @default_timeout_ms}
2018-08-16 17:16:38 -06:00
end
def handle_info(:timeout, state) do
2018-08-16 17:16:38 -06:00
# Block and don't reset if after 10 tries
# resolution doesn't work.
block_check()
{:noreply, state, @default_timeout_ms}
2018-08-16 17:16:38 -06:00
end
defp block_check(last_result, reset \\ false, tries \\ 10)
2018-08-16 17:16:38 -06:00
defp block_check(last_result, false, 0) do
2018-08-16 17:16:38 -06:00
server = get_config_value(:string, "authorization", "server")
Farmbot.Logger.error(1, "Could not resolve #{server} after 10 tries.")
2018-08-16 17:16:38 -06:00
end
defp block_check(_last_result, true, 0) do
2018-08-16 17:16:38 -06:00
server = get_config_value(:string, "authorization", "server")
Farmbot.Logger.error(1, "Tried 10 times to resolve DNS requests.")
2018-08-16 17:16:38 -06:00
msg = """
FarmBot is unable to make DNS requests to #{server} after
10 tries. It is possible your network has a firewall blocking this
url, or your FarmBot is configured incorrectly.
"""
2018-08-16 17:16:38 -06:00
Farmbot.System.factory_reset(msg)
:error
2018-08-16 17:16:38 -06:00
end
defp block_check(last_result, reset, tries) do
2018-08-16 17:16:38 -06:00
server = get_config_value(:string, "authorization", "server")
2018-08-16 17:16:38 -06:00
case test_dns() do
{:ok, _} ->
:ok
2018-08-16 17:16:38 -06:00
{:error, :nxdomain} ->
Process.sleep(10_000)
Farmbot.Logger.error(1, "Trying to resolve #{server} #{tries - 1} more times.")
block_check(reset, tries - 1)
2018-08-16 17:16:38 -06:00
err ->
Farmbot.Logger.error(1, "Failed to resolve #{server}: #{inspect(err)}")
block_check(reset, tries)
2018-08-16 17:16:38 -06:00
end
end
end