Skip to content

Commit

Permalink
Merge pull request #2 from kbaird/TypeSpecs
Browse files Browse the repository at this point in the history
Type specs
  • Loading branch information
msawka committed Aug 19, 2015
2 parents 10dafca + 9e5f8ca commit 740d3f1
Show file tree
Hide file tree
Showing 16 changed files with 251 additions and 251 deletions.
2 changes: 1 addition & 1 deletion lib/overseer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule OpenAperture.Overseer do
Note also that the `:transient` type is of little practical use, since when a
supervision tree terminates, the reason is set to `:shutdown`, not `:normal`.
"""
@spec start(atom, [any]) :: :ok | {:error, String.t()}
@spec start(atom, [any]) :: :ok | {:error, String.t}
def start(_type, _args) do
import Supervisor.Spec, warn: false
Logger.info("Starting OpenAperture.Overseer...")
Expand Down
104 changes: 52 additions & 52 deletions lib/overseer/clusters/cluster_monitor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule OpenAperture.Overseer.Clusters.ClusterMonitor do

@moduledoc """
This module contains the GenServer for monitoring a cluster in the associated exchange
"""
"""

@doc """
Specific start_link implementation
Expand All @@ -23,7 +23,7 @@ defmodule OpenAperture.Overseer.Clusters.ClusterMonitor do
{:ok, pid} | {:error, reason}
"""
@spec start_link(Map) :: {:ok, pid} | {:error, String.t()}
@spec start_link(map) :: {:ok, pid} | {:error, String.t}
def start_link(cluster) do
Logger.debug("#{@logprefix}[#{cluster["etcd_token"]}] Starting...")
GenServer.start_link(__MODULE__, %{cluster: cluster, etcd_token: cluster["etcd_token"]})
Expand Down Expand Up @@ -56,7 +56,7 @@ defmodule OpenAperture.Overseer.Clusters.ClusterMonitor do
{:noreply, state}
"""
@spec handle_cast({:monitor}, Map) :: {:noreply, Map}
@spec handle_cast({:monitor}, map) :: {:noreply, map}
def handle_cast({:monitor}, state) do
monitor_cluster(state[:etcd_token])
monitor_cluster_units(state[:etcd_token])
Expand All @@ -79,28 +79,28 @@ defmodule OpenAperture.Overseer.Clusters.ClusterMonitor do
handler = FleetManagerPublisher.list_machines!(etcd_token, exchange_id)
case RpcHandler.get_response(handler) do
{:error, reason} -> Logger.error("#{@logprefix}[#{etcd_token}] Received the following error retrieving node_info: #{inspect reason}")
{:ok, nil} ->
{:ok, nil} ->
Logger.error("#{@logprefix}[#{etcd_token}] Unable to perform host checking...invalid hosts were found in exchange #{exchange_id}!")

event = %{
unique: true,
type: :docker_disk_space_percent,
severity: :warning,
type: :docker_disk_space_percent,
severity: :warning,
data: %{host_cnt: 0},
message: "EtcdCluster #{etcd_token} has no associated hosts"
}
}
SystemEvent.create_system_event!(ManagerApi.get_api, event)
{:ok, []} ->
{:ok, []} ->
Logger.error("#{@logprefix}[#{etcd_token}] Unable to perform host checking...no hosts were found in exchange #{exchange_id}!")

event = %{
unique: true,
type: :docker_disk_space_percent,
severity: :warning,
type: :docker_disk_space_percent,
severity: :warning,
data: %{host_cnt: 0},
message: "EtcdCluster #{etcd_token} has no associated hosts"
}
SystemEvent.create_system_event!(ManagerApi.get_api, event)
}
SystemEvent.create_system_event!(ManagerApi.get_api, event)
{:ok, hosts} -> monitor_hosts(etcd_token, hosts)
end
end
Expand All @@ -115,7 +115,7 @@ defmodule OpenAperture.Overseer.Clusters.ClusterMonitor do
The `hosts` option defines the list of hosts in the cluster
"""
@spec monitor_hosts(String.t, List) :: term
@spec monitor_hosts(String.t, list) :: term
def monitor_hosts(etcd_token, hosts) do
hostnames = Enum.reduce hosts, [], fn(host, hostnames) ->
hostnames ++ [host["primaryIP"]]
Expand All @@ -137,9 +137,9 @@ defmodule OpenAperture.Overseer.Clusters.ClusterMonitor do
The `node_info` option defines the Map of all host information
"""
@spec monitor_host([], Map, String.t) :: term
@spec monitor_host([], map, String.t) :: term
def monitor_host([], _node_info, _etcd_token) do
Logger.debug("Finished reviewing node_info")
Logger.debug("Finished reviewing node_info")
end

@doc """
Expand All @@ -150,19 +150,19 @@ defmodule OpenAperture.Overseer.Clusters.ClusterMonitor do
The `hosts` option defines the list of hosts to review
The `node_info` option defines the Map of all host information
"""
@spec monitor_host(List, Map, String.t) :: term
@spec monitor_host(list, map, String.t) :: term
def monitor_host([returned_hostname | remaining_hostnames], node_info, etcd_token) do
info = node_info[returned_hostname]

Logger.debug("Evaluating hostname #{returned_hostname} in node_info #{inspect node_info}")

event = cond do
event = cond do
info["docker_disk_space_percent"] == nil -> %{
unique: true,
type: :docker_disk_space_percent,
severity: :error,
unique: true,
type: :docker_disk_space_percent,
severity: :error,
data: %{
docker_disk_space_percent: nil,
hostname: returned_hostname,
Expand All @@ -171,28 +171,28 @@ defmodule OpenAperture.Overseer.Clusters.ClusterMonitor do
message: "Host #{returned_hostname} is not reporting the Docker disk space utilization %!"
}
info["docker_disk_space_percent"] > 90 -> %{
unique: true,
type: :docker_disk_space_percent,
severity: :error,
unique: true,
type: :docker_disk_space_percent,
severity: :error,
data: %{
docker_disk_space_percent: info["docker_disk_space_percent"],
hostname: returned_hostname,
etcd_token: etcd_token
},
message: "Host #{returned_hostname} is reporting a Docker disk space utilization of #{info["docker_disk_space_percent"]}%!"
}
}
info["docker_disk_space_percent"] > 80 -> %{
unique: true,
type: :docker_disk_space_percent,
severity: :warning,
type: :docker_disk_space_percent,
severity: :warning,
data: %{
docker_disk_space_percent: info["docker_disk_space_percent"],
hostname: returned_hostname,
etcd_token: etcd_token
},
message: "Host #{returned_hostname} is reporting a Docker disk space utilization of #{info["docker_disk_space_percent"]}%!"
}
true -> nil
}
true -> nil
end

if event != nil do
Expand All @@ -211,7 +211,7 @@ defmodule OpenAperture.Overseer.Clusters.ClusterMonitor do
## Option Values
The `etcd_token` option defines the EtcdToken associated with the cluster
"""
@spec monitor_cluster_units(String.t) :: term
def monitor_cluster_units(etcd_token) do
Expand All @@ -221,23 +221,23 @@ defmodule OpenAperture.Overseer.Clusters.ClusterMonitor do
case RpcHandler.get_response(handler) do
{:error, reason} -> Logger.error("#{@logprefix}[#{etcd_token}] Received the following error retrieving unit states: #{inspect reason}")
#i.e. build slaves
{:ok, []} -> Logger.debug("#{@logprefix}[#{etcd_token}] Cluster #{etcd_token} is not running any units")
{:ok, []} -> Logger.debug("#{@logprefix}[#{etcd_token}] Cluster #{etcd_token} is not running any units")
#load error occurred
{:ok, nil} ->
{:ok, nil} ->
Logger.error("#{@logprefix}[#{etcd_token}] Unable to perform states checking...invalid states were found in cluster #{etcd_token}!")

event = %{
unique: true,
type: :failed_unit,
severity: :warning,
type: :failed_unit,
severity: :warning,
data: %{
etcd_token: etcd_token,
host_cnt: nil
},
message: "EtcdCluster #{etcd_token} failed to return unit states!"
}
SystemEvent.create_system_event!(ManagerApi.get_api, event)
{:ok, units} ->
}
SystemEvent.create_system_event!(ManagerApi.get_api, event)
{:ok, units} ->
monitor_unit_instances(units, etcd_token, 0)
monitor_units(units, etcd_token)
end
Expand All @@ -253,7 +253,7 @@ defmodule OpenAperture.Overseer.Clusters.ClusterMonitor do
"""
@spec monitor_unit_instances([], String.t, term) :: term
def monitor_unit_instances([], _etcd_token, _failure_count) do
Logger.debug("Finished reviewing units")
Logger.debug("Finished reviewing units")
end

@doc """
Expand All @@ -264,25 +264,25 @@ defmodule OpenAperture.Overseer.Clusters.ClusterMonitor do
The `etcd_token` option defines the EtcdToken associated with the cluster
"""
@spec monitor_unit_instances(List, String.t, term) :: term
@spec monitor_unit_instances(list, String.t, term) :: term
def monitor_unit_instances([unit| remaining_units] = all_units, etcd_token, failure_count) do
event = cond do
event = cond do
#give failed units 3 tries before generating an error
unit["systemdActiveState"] == "failed" && failure_count < 3 ->
:timer.sleep(10_000)
#TODO: need to refresh the unit state first
monitor_unit_instances(all_units, etcd_token, failure_count + 1)
unit["systemdActiveState"] == "failed" -> %{
unique: true,
type: :failed_unit,
severity: :warning,
unique: true,
type: :failed_unit,
severity: :warning,
data: %{
unit_name: unit["name"],
etcd_token: etcd_token
},
message: "Unit #{unit["name"]} is in a failed state!"
}
true -> nil
}
true -> nil
end

if event != nil do
Expand All @@ -305,7 +305,7 @@ defmodule OpenAperture.Overseer.Clusters.ClusterMonitor do
The `etcd_token` option defines the EtcdToken associated with the cluster
"""
@spec monitor_units(List, String.t) :: term
@spec monitor_units(list, String.t) :: term
def monitor_units(units, etcd_token) do
if units == nil || length(units) == 0 do
Logger.debug("#{@logprefix} There are no units in cluster #{etcd_token} to review")
Expand All @@ -327,20 +327,20 @@ defmodule OpenAperture.Overseer.Clusters.ClusterMonitor do
running_units = units_by_name[unit_name]
if length(running_units) == 0 do
event = %{
unique: true,
type: :failed_unit,
severity: :error,
unique: true,
type: :failed_unit,
severity: :error,
data: %{
unit_name: unit_name,
etcd_token: etcd_token
},
message: "Unit #{unit_name} has no running instances!"
}
}

Logger.error("#{@logprefix} A system event was generated for unit #{unit_name}: #{inspect event}")
SystemEvent.create_system_event!(ManagerApi.get_api, event)
SystemEvent.create_system_event!(ManagerApi.get_api, event)
end
end
end
end
end
end
28 changes: 14 additions & 14 deletions lib/overseer/clusters/clusters_monitor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule OpenAperture.Overseer.Clusters.ClustersMonitor do

@moduledoc """
This module contains the GenServer for monitoring all of the clusters in the associated exchange
"""
"""

@doc """
Specific start_link implementation
Expand All @@ -23,11 +23,11 @@ defmodule OpenAperture.Overseer.Clusters.ClustersMonitor do
{:ok, pid} | {:error, reason}
"""
@spec start_link() :: {:ok, pid} | {:error, String.t()}
@spec start_link() :: {:ok, pid} | {:error, String.t}
def start_link() do
Logger.debug("#{@logprefix} Starting...")
case GenServer.start_link(__MODULE__, %{clusters: %{}}, name: __MODULE__) do
{:ok, pid} ->
{:ok, pid} ->
if Application.get_env(:autostart, :clusters_monitor, true) do
GenServer.cast(pid, {:monitor})
end
Expand All @@ -47,7 +47,7 @@ defmodule OpenAperture.Overseer.Clusters.ClustersMonitor do
{:noreply, state}
"""
@spec handle_cast({:monitor}, Map) :: {:noreply, Map}
@spec handle_cast({:monitor}, map) :: {:noreply, map}
def handle_cast({:monitor}, state) do
#sleep for up to half an hour
sleep_seconds = :random.uniform(1800)
Expand All @@ -72,7 +72,7 @@ defmodule OpenAperture.Overseer.Clusters.ClustersMonitor do
The `state` option is the GenServer's state
"""
@spec monitor_cached_clusters(Map) :: Map
@spec monitor_cached_clusters(map) :: map
def monitor_cached_clusters(state) do
Enum.reduce Map.values(state[:clusters]), nil, fn monitor, _result ->
ClusterMonitor.monitor(monitor)
Expand All @@ -90,16 +90,16 @@ defmodule OpenAperture.Overseer.Clusters.ClustersMonitor do
state
"""
@spec monitor_clusters(Map) :: Map
@spec monitor_clusters(map) :: map
def monitor_clusters(state) do
exchange_id = Configuration.get_current_exchange_id

clusters = MessagingExchange.exchange_clusters!(ManagerApi.get_api, exchange_id)
cond do
clusters == nil ->
clusters == nil ->
Logger.error("#{@logprefix} Unable to load clusters associated with exchange #{exchange_id}!")
state
clusters == [] ->
clusters == [] ->
Logger.debug("#{@logprefix} There are no clusters associated to exchange #{exchange_id}")
stop_monitoring_clusters(state)
true ->
Expand All @@ -122,23 +122,23 @@ defmodule OpenAperture.Overseer.Clusters.ClustersMonitor do
state
"""
@spec monitor_cluster(Map, Map) :: Map
@spec monitor_cluster(map, map) :: map
def monitor_cluster(state, cluster) do
if state[:clusters][cluster["etcd_token"]] == nil do
case ClusterMonitor.start_link(cluster) do
{:ok, monitor} ->
{:ok, monitor} ->
Logger.debug("#{@logprefix} Starting a new monitor for cluster #{cluster["etcd_token"]}")
cluster_cache = state[:clusters]
cluster_cache = Map.put(cluster_cache, cluster["etcd_token"], monitor)
Map.put(state, :clusters, cluster_cache)
{:error, reason} ->
{:error, reason} ->
Logger.error("#{@logprefix} Failed to start Cluster monitor for cluster #{cluster["etcd_token"]}: #{inspect reason}")
state
end
else
Logger.debug("#{@logprefix} A monitor already exists for cluster #{cluster["etcd_token"]}")
state
end
end
end

@doc """
Expand All @@ -154,7 +154,7 @@ defmodule OpenAperture.Overseer.Clusters.ClustersMonitor do
state
"""
@spec stop_monitoring_clusters(Map, List) :: Map
@spec stop_monitoring_clusters(map, list) :: map
def stop_monitoring_clusters(state, clusters \\ nil) do
if clusters == nil do
clusters = Map.keys(state[:clusters])
Expand All @@ -176,4 +176,4 @@ defmodule OpenAperture.Overseer.Clusters.ClustersMonitor do
state
end
end
end
end
Loading

0 comments on commit 740d3f1

Please sign in to comment.