Skip to content

Commit

Permalink
Stages refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalimaha committed Apr 3, 2018
1 parent a6b8ddd commit 86481eb
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 75 deletions.
6 changes: 3 additions & 3 deletions lib/weather_station_manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ defmodule WeatherStationManager do
children = [
supervisor(WeatherStationManager.Repo, []),
supervisor(WeatherStationManager.Endpoint, []),
worker(Tizio.Source, []),
worker(Tizio.DBManager, []),
worker(Tizio.TwitterManager, [])
worker(UpdatesManager, []),
worker(UpdatesDBWorker, []),
worker(UpdatesTwitterWorker, [])
]

# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
Expand Down
5 changes: 2 additions & 3 deletions test/controllers/updates_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ defmodule WeatherStationManager.UpdatesControllerTest do
end

test "the update is broadcasted to the workers", %{conn: conn} do
with_mock Tizio.Source, [broadcast_update: fn(_spam) -> :eggs end] do
with_mock UpdatesManager, [broadcast_update: fn(_spam) -> :eggs end] do
post conn, "/api/updates", @update

assert called Tizio.Source.broadcast_update(@update)
assert called UpdatesManager.broadcast_update(@update)
end
end
end

3 changes: 1 addition & 2 deletions web/controllers/updates_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ defmodule WeatherStationManager.UpdatesController do
use WeatherStationManager.Web, :controller

def create(conn, params) do
Tizio.Source.broadcast_update(params)
UpdatesManager.broadcast_update(params)

conn
|> put_status(201)
|> json(%{ status: "OK" })
end
end

67 changes: 0 additions & 67 deletions web/playground/tizio.ex

This file was deleted.

21 changes: 21 additions & 0 deletions web/stages/updates_db_worker.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule UpdatesDBWorker do
use GenStage

require Logger

def start_link() do
GenStage.start_link(__MODULE__, :ok)
end

def init(:ok) do
{ :consumer, :ok, subscribe_to: [ UpdatesManager ] }
end

def handle_events(events, _from, state) do
Logger.debug "[STAGE] - Write to DB: START"
Enum.each events, fn(_spam) -> Logger.debug "processing SQL..." end
Logger.debug "[STAGE] - Write to DB: DONE"

{ :noreply, [], state }
end
end
25 changes: 25 additions & 0 deletions web/stages/updates_manager.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule UpdatesManager do
use GenStage

require Logger

def start_link() do
GenStage.start_link(__MODULE__, :ok, name: __MODULE__)
end

def init(:ok) do
{ :producer, :ok, dispatcher: GenStage.BroadcastDispatcher }
end

def handle_demand(_demand, state) do
{ :noreply, [], state }
end

def broadcast_update(update) do
GenStage.call(__MODULE__, { :broadcast, update })
end

def handle_call({ :broadcast, update }, _from, state) do
{ :reply, %{ :ok => "Weather update received." }, [ update ], state }
end
end
21 changes: 21 additions & 0 deletions web/stages/updates_twitter_worker.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule UpdatesTwitterWorker do
use GenStage

require Logger

def start_link() do
GenStage.start_link(__MODULE__, :ok)
end

def init(:ok) do
{ :consumer, :ok, subscribe_to: [ UpdatesManager ] }
end

def handle_events(events, _from, state) do
Logger.debug "[STAGE] - Tweet about it: START"
Enum.each events, fn(_spam) -> Logger.debug "processing tweet..." end
Logger.debug "[STAGE] - Tweet about it: DONE"

{ :noreply, [], state }
end
end

0 comments on commit 86481eb

Please sign in to comment.