-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zach Robert
committed
Jun 7, 2022
1 parent
361d30f
commit 8c0d725
Showing
3 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
defmodule SituationRoom.ChecksGenServer 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.Site.LiveCheck.run(site.endpoint) do | ||
{:ok, %{response_time: res_time, status: status}} -> | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule SituationRoom.ChecksSupervisor 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.Sites.get_all_sites() do | ||
Supervisor.child_spec({SituationRoom.ChecksGenServer, site}, id: site.id) | ||
end | ||
|
||
Supervisor.init(children, strategy: :one_for_one) | ||
end | ||
|
||
# def add_child(site) do | ||
# Supervisor.start_child() | ||
# end | ||
|
||
# def remove_child() do | ||
# Supervisor.delete_child() | ||
# end | ||
end |