Skip to content

Commit

Permalink
Stats Supervisor
Browse files Browse the repository at this point in the history
  • Loading branch information
akira committed Oct 29, 2015
1 parent ce84e3d commit 54928a9
Showing 3 changed files with 29 additions and 6 deletions.
6 changes: 4 additions & 2 deletions lib/exq/manager/server.ex
Original file line number Diff line number Diff line change
@@ -39,7 +39,10 @@ defmodule Exq.Manager.Server do

{:ok, localhost} = :inet.gethostname()

{:ok, stats} = GenServer.start_link(Stats, {redis}, [])
stats = String.to_atom("#{name}_stats")
{:ok, _} = Exq.Stats.Supervisor.start_link(
redis: redis,
name: stats)

enqueuer = String.to_atom("#{name}_enqueuer")
{:ok, _} = Exq.Enqueuer.Supervisor.start_link(
@@ -149,7 +152,6 @@ defmodule Exq.Manager.Server do
end

def terminate(_reason, state) do
GenServer.call(state.stats, {:stop})
:eredis.stop(state.redis)
:ok
end
13 changes: 9 additions & 4 deletions lib/exq/stats/server.ex
Original file line number Diff line number Diff line change
@@ -8,6 +8,8 @@ defmodule Exq.Stats.Server do
alias Exq.Stats.Process
require Logger

@default_name :exq_stats

defmodule State do
defstruct redis: nil
end
@@ -20,15 +22,18 @@ defmodule Exq.Stats.Server do
## gen server callbacks
##===========================================================

def start_link(redis) do
GenServer.start_link(__MODULE__, {redis}, [])
def start_link(opts \\ []) do
name = Keyword.get(opts, :name, @default_name)
GenServer.start_link(__MODULE__, [opts], [{:name, name}])
end

# These are the callbacks that GenServer.Behaviour will use
def init({redis}) do
{:ok, %State{redis: redis}}
def init([opts]) do
{:ok, %State{redis: Keyword.get(opts, :redis)}}
end

def default_name, do: @default_name

def handle_cast({:add_process, namespace, process}, state) do
add_process(state.redis, namespace, process)
{:noreply, state}
16 changes: 16 additions & 0 deletions lib/exq/stats/supervisor.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Exq.Stats.Supervisor do
use Supervisor

def start_link(opts \\ []) do
Supervisor.start_link(__MODULE__, [opts], name: String.to_atom("#{manager_name(opts)}_sup"))
end

def init([opts]) do
children = [worker(Exq.Stats.Server, [opts])]
supervise(children, strategy: :one_for_one, max_restarts: 20)
end

defp manager_name(opts) do
Keyword.get(opts, :name, Exq.Stats.Server.default_name)
end
end

0 comments on commit 54928a9

Please sign in to comment.