Skip to content

Instantly share code, notes, and snippets.

@mikhailbot
Created November 4, 2017 22:04
Show Gist options
  • Save mikhailbot/30b7d7fb1cb9582875d8e5eeec149227 to your computer and use it in GitHub Desktop.
Save mikhailbot/30b7d7fb1cb9582875d8e5eeec149227 to your computer and use it in GitHub Desktop.
def init(initial_state) do
state = %{
ip_address: initial_state.host_ip_address,
current_status: initial_state.host_status,
previous_stuatus: initial_state.host_status,
latency: 0,
status_changes: 0
}
schedule_work()
{:ok, state}
end
def handle_call(:get_state, _from, state) do
{:reply, state, state}
end
def handle_info(:work, state) do
with {:ok, results} <- Check.host_up(state.ip_address) do
cond do
state.current_status == results.status ->
^state.latency = results.latency
state.current_status != results.status && state.current_status != "unknown" ->
state.status = "unknown"
state.status_changes = state.status_changes + 1
state.latency = results.latency
state.current_status == "unknown" && state.previous_status == results.status ->
state.current_status = results.status
state.status_changes = 0
state.latency = results.latency
state.current_status == "unknown" && state.status_changes < 3 ->
state.status_changes = state.status_changes + 1
state.latency =results.latency
state.current_status == "unknown" && state.status_changes >= 3 ->
state.current_status = results.status
state.previous_status = results.status
state.status_changes = 0
# Send notification
# Create status change event
end
else
{:error, error} ->
IO.puts error
:error
end
IO.inspect state
schedule_work()
{:noreply, state}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment