Permalink
Browse files

create CerebrateRpc gen_server to act as the api to the check data

  • Loading branch information...
1 parent 347b34a commit f45d829dbad68bddb237d46bcfb5e1ac2fd8b768 @clofresh committed Apr 13, 2012
Showing with 57 additions and 29 deletions.
  1. +21 −18 src/cerebrate.ex
  2. +36 −11 src/cerebrate_checks.ex
View
@@ -53,6 +53,10 @@ defmodule Cerebrate do
{
:cerebrate_dnssd, {CerebrateDnssd, :start_link, [config]},
:transient, 60, :worker, [:cerebrate]
+ },
+ {
+ :cerebrate_rpc, {CerebrateRpc, :start_link, [config]},
+ :permanent, 60, :worker, [:cerebrate]
}
]}}
end
@@ -67,27 +71,26 @@ defmodule CerebrateWeb do
end
def handle(req, state) do
- Process.whereis(:collector) <- {:query, Process.self()}
- {:ok, req2} = receive do
- match: data
- output = Enum.map data, fn({metric, value}) ->
- [metric, float_to_list(value)]
- end
+ data = CerebrateRpc.check_data
- Process.whereis(:cerebrate_dnssd) <- {:query, Process.self()}
- peers = receive do
- match: dnssd_state
- Enum.map Erlang.dict.fetch(:peers, dnssd_state), fn({name, type, domain}) ->
- [name, type, domain]
- end
- after: 2000
- raise "Could not get peers"
+ output = Enum.map data, fn({metric, value}) ->
+ [metric, float_to_list(value)]
+ end
+
+ Process.whereis(:cerebrate_dnssd) <- {:query, Process.self()}
+ peers = receive do
+ match: dnssd_state
+ Enum.map Erlang.dict.fetch(:peers, dnssd_state), fn({name, type, domain}) ->
+ [name, type, domain]
end
- IO.inspect peers
- Erlang.cowboy_http_req.reply(200, [], ["Data:", output, peers], req)
- after: 5000
- Erlang.cowboy_http_req.reply(500, [], "Timed out", req)
+ after: 2000
+ raise "Could not get peers"
end
+ IO.inspect peers
+ reply = ["Data:", output, peers]
+ IO.puts "replying with #{reply}"
+ {:ok, req2} = Erlang.cowboy_http_req.reply(200, [], reply, req)
+
{:ok, req2, state}
end
@@ -52,6 +52,37 @@ defmodule CerebrateDnssd do
end
end
+defmodule CerebrateRpc do
+ use GenServer.Behavior
+
+ def start_link(config) do
+ Erlang.gen_server.start_link({:local, :cerebrate_rpc}, __MODULE__, [config], [])
+ end
+
+ def init(_config) do
+ IO.puts "Initialize CerebrateRpc gen_server"
+ {:ok, []}
+ end
+
+ def handle_call(:check_data, _from, state) do
+ {:reply, state, state}
+ end
+
+ def handle_cast(:run_checks, state) do
+ {:noreply, CerebrateChecks.all()}
+ end
+
+ # API
+ def run_checks() do
+ Erlang.gen_server.cast(:cerebrate_rpc, :run_checks)
+ end
+
+ def check_data() do
+ Erlang.gen_server.call(:cerebrate_rpc, :check_data)
+ end
+
+end
+
defmodule CerebrateCollector do
def start_link(config) do
pid = spawn_link CerebrateCollector, :start, [config]
@@ -60,19 +91,13 @@ defmodule CerebrateCollector do
end
def start(_config) do
- run Erlang.dict.store(:start_time, Erlang.now(), Erlang.dict.new())
+ run
end
- def run(state) do
- check_data = CerebrateChecks.all()
- #IO.inspect check_data
- receive do
- match: {:query, caller}
- caller <- check_data
- after: 1000
- #IO.puts "No calls after 1000ms"
- end
- run state
+ def run() do
+ CerebrateRpc.run_checks()
+ ok = Erlang.timer.sleep 2000
+ run
end
end

0 comments on commit f45d829

Please sign in to comment.