-
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.
[feat] performing checks as gen server
- Loading branch information
Zach Robert
committed
Jun 7, 2022
1 parent
505ef20
commit c0f4caa
Showing
3 changed files
with
65 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,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 |
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,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 |