server.ex 1.11 KB
Newer Older
1 2 3
defmodule LakeEffect.Jobs.Server do
  @moduledoc false
  use GenServer
4
  alias LakeEffect.WeatherReport
5 6 7 8 9

  def start_link() do
    GenServer.start_link(__MODULE__, [], name: :jobs_server)
  end

10
  @impl true
11 12
  def init(_) do
    if enabled?() do
13
      schedule_socket_job()
14 15 16 17 18 19
      {:ok, nil}
    else
      :ignore
    end
  end

Frank Kumro's avatar
Frank Kumro committed
20 21 22 23
  # @impl true
  # def handle_info(:perform, state) do
  #   WeatherReport.collect()
  #   |> WeatherReport.save()
24

Frank Kumro's avatar
Frank Kumro committed
25
  #   schedule_next_job()
26

Frank Kumro's avatar
Frank Kumro committed
27 28
  #   {:noreply, state}
  # end
29

30 31 32 33 34 35 36 37 38 39
  @impl true
  def handle_info(:send_via_socket, state) do
    WeatherReport.collect()
    |> LakeEffect.Clients.ThunderSnow.send_via_socket()

    schedule_socket_job()

    {:noreply, state}
  end

Frank Kumro's avatar
Frank Kumro committed
40 41 42 43
  # defp schedule_initial_job() do
  #   # wait 60 seconds to allow networking to connect
  #   Process.send_after(self(), :perform, 60_000)
  # end
44

Frank Kumro's avatar
Frank Kumro committed
45 46 47
  # defp schedule_next_job() do
  #   Process.send_after(self(), :perform, 30_000)
  # end
48

49 50 51 52
  defp schedule_socket_job() do
    Process.send_after(self(), :send_via_socket, 1_000)
  end

53 54 55 56
  defp enabled?() do
    Application.get_env(:lake_effect, __MODULE__)[:enabled]
  end
end