Skip to content

Commit

Permalink
[feat] performing checks as gen server
Browse files Browse the repository at this point in the history
  • Loading branch information
Zach Robert committed Jun 7, 2022
1 parent 505ef20 commit c0f4caa
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/situation_room/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ defmodule SituationRoom.Application do
# Start the PubSub system
{Phoenix.PubSub, name: SituationRoom.PubSub},
# Start the Endpoint (http/https)
SituationRoomWeb.Endpoint
SituationRoomWeb.Endpoint,
# Start a worker by calling: SituationRoom.Worker.start_link(arg)
# {SituationRoom.Worker, arg}
SituationRoom.CheckSup
]

# See https://hexdocs.pm/elixir/Supervisor.html
Expand Down
43 changes: 43 additions & 0 deletions lib/situation_room/check/check_gen.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule SituationRoom.CheckGen do
@moduledoc """
The Genserver for checking sites
"""
use GenServer
require Logger
alias SituationRoom.Site.Check

def start_link(args) do
GenServer.start_link(__MODULE__, args)
end

def init(site) do
schedule_site_check(site.interval)
{:ok, site}
end

def handle_info(:run_check, site) do
handle_check(site)
schedule_site_check(site.interval)
{:noreply, site}
end

defp handle_check(site) do
case SituationRoom.Check.run(site.endpoint) do
{:ok, %{"response_time" => res_time, "status" => status}} ->
IO.inspect(
Check.create_check(%{
"site_id" => site.id,
"response_time" => res_time,
"status_code" => status
})
)

{:error, reason} ->
reason
end
end

defp schedule_site_check(interval) do
Process.send_after(self(), :run_check, interval)
end
end
20 changes: 20 additions & 0 deletions lib/situation_room/check/check_sup.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule SituationRoom.CheckSup do
@moduledoc """
Supervisor for Site Check GenServers
"""
use Supervisor

def start_link(opts) do
Supervisor.start_link(__MODULE__, opts)
end

@impl true
def init(_opts) do
children =
for site <- SituationRoom.Site.get_all_sites() do
Supervisor.child_spec({SituationRoom.CheckGen, site}, id: site.id)
end

Supervisor.init(children, strategy: :one_for_one)
end
end

0 comments on commit c0f4caa

Please sign in to comment.