<commit_before>defmodule Farmbot.Bootstrap.AuthTask do
  @moduledoc "Background worker that refreshes a token every 30 minutes."
  use GenServer
  use Farmbot.Logger
  alias Farmbot.System.ConfigStorage

  # 30 minutes.
  @refresh_time 1.8e+6 |> round()

  @doc false
  def start_link() do
    GenServer.start_link(__MODULE__, [], [name: __MODULE__])
  end

  def init([]) do
    timer = Process.send_after(self(), :refresh, @refresh_time)
    {:ok, timer, :hibernate}
  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()}
    # Logger.busy(3, "refreshing token: #{auth_task} - #{email} - #{server}")
    Farmbot.System.GPIO.Leds.led_status_err()
    case auth_task.authorize(email, pass, server) do
      {:ok, token} ->
        # Logger.success(3, "Successful authorization: #{auth_task} - #{email} - #{server}")
        ConfigStorage.update_config_value(:bool, "settings", "first_boot", false)
        ConfigStorage.update_config_value(:string, "authorization", "token", token)
        Farmbot.System.GPIO.Leds.led_status_ok()
        restart_transports()
        refresh_timer(self())
      {:error, reason} ->
        Logger.error(1, "Token failed to reauthorize: #{auth_task} - #{email} - #{server} #{inspect reason}")
        {:stop, reason, old_timer}
    end<commit_msg>Make auth task keep trying to refresh<commit_after>defmodule Farmbot.Bootstrap.AuthTask do
  @moduledoc "Background worker that refreshes a token every 30 minutes."
  use GenServer
  use Farmbot.Logger
  alias Farmbot.System.ConfigStorage

  # 30 minutes.
  @refresh_time 1.8e+6 |> round()

  @doc false
  def start_link() do
    GenServer.start_link(__MODULE__, [], [name: __MODULE__])
  end

  def init([]) do
    timer = Process.send_after(self(), :refresh, @refresh_time)
    {:ok, timer, :hibernate}
  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()}
    # Logger.busy(3, "refreshing token: #{auth_task} - #{email} - #{server}")
    Farmbot.System.GPIO.Leds.led_status_err()
    case auth_task.authorize(email, pass, server) do
      {:ok, token} ->
        # Logger.success(3, "Successful authorization: #{auth_task} - #{email} - #{server}")
        ConfigStorage.update_config_value(:bool, "settings", "first_boot", false)
        ConfigStorage.update_config_value(:string, "authorization", "token", token)
        Farmbot.System.GPIO.Leds.led_status_ok()
        restart_transports()
        refresh_timer(self())
      {:error, reason} ->
        Logger.error(1, "Token failed to reauthorize: #{auth_task} - #{email} - #{server} #{inspect reason}")
        refresh_timer(self())
    end
{'fraction_non_alphanumeric': 0.1421585749214111, 'fraction_numerical': 0.006985679357317499, 'mean_word_length': 3.8624787775891343, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3291650', 'n_tokens_mistral': 949, 'n_tokens_neox': 924, 'n_words': 211}