Skip to content

Commit 5040976

Browse files
committed
Implement call cluster distribution
1 parent 614c3ae commit 5040976

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

lib/call/application.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ defmodule Call.Application do
1717
),
1818
worker(Call.RabbitMQ, [], restart: :permanent),
1919
worker(Call.Database, [], restart: :permanent),
20+
worker(Call.Dispatcher, [], restart: :permanent),
2021
worker(DapnetService.CouchDB, [], restart: :permanent),
2122
]
2223

lib/call/dispatch.ex

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1-
defmodule Call.Dispatch do
1+
defmodule Call.Dispatcher do
2+
use GenServer
3+
24
def dispatch(call) do
5+
GenServer.call(__MODULE__, {:dispatch, call})
6+
end
7+
8+
def start_link do
9+
GenServer.start_link(__MODULE__, {}, [name: __MODULE__])
10+
end
11+
12+
def init(_opts) do
13+
{:ok, nil}
14+
end
15+
16+
def handle_call({:dispatch, call}, _from, state) do
317
recipients = Map.get(call, "recipients", %{})
418

519
# subscribers = Map.get(recipients, "subscribers", [])
@@ -32,6 +46,8 @@ defmodule Call.Dispatch do
3246
end)
3347
end)
3448
end)
49+
50+
{:reply, :ok, state}
3551
end
3652

3753
def send_call(call, transmitter, pager) do

lib/call/rabbitmq.ex

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ defmodule Call.RabbitMQ do
2121
end
2222

2323
def init(_opts) do
24-
connect
24+
connect()
2525
end
2626

2727
def handle_call({:publish_call, call}, _from, chan) do
@@ -52,16 +52,20 @@ defmodule Call.RabbitMQ do
5252
def handle_info({:basic_deliver, payload, %{delivery_tag: tag, routing_key: key}}, chan) do
5353
:ok = Basic.ack chan, tag
5454

55-
data = Poison.decode!(payload)
56-
55+
case Poison.decode(payload) do
56+
{:ok, call} ->
57+
Call.Dispatcher.dispatch(call)
58+
Call.Database.store call
59+
_ -> Logger.warn("Failed to decode remote call")
60+
end
5761

5862
{:noreply, chan}
5963
end
6064

6165
# Automatic Reconnect
6266
def handle_info({:DOWN, _, :process, _pid, _reason}, _) do
6367
Logger.warn("RabbitMQ connection closed.")
64-
{:ok, chan} = connect
68+
{:ok, chan} = connect()
6569
{:noreply, chan}
6670
end
6771

@@ -91,7 +95,7 @@ defmodule Call.RabbitMQ do
9195
Logger.error("RabbitMQ connection failed.")
9296
# Reconnection loop
9397
:timer.sleep(10000)
94-
connect
98+
connect()
9599
end
96100
end
97101

lib/call/router.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ defmodule Call.Router do
2121
|> Map.put("created_on", Timex.now())
2222
|> Map.put("created_by", user)
2323

24-
Call.Dispatch.dispatch call
24+
Call.Dispatcher.dispatch call
2525
Call.Database.store call
2626

2727
json_call = Poison.encode!(call)
@@ -35,8 +35,6 @@ defmodule Call.Router do
3535
end
3636

3737
get "/calls" do
38-
IO.inspect conn
39-
4038
case Call.Database.list() do
4139
nil ->
4240
send_resp(conn, 404, '{"error": "Not found"}')

0 commit comments

Comments
 (0)