conductor.ex 798 Bytes
Newer Older
Jens-Christian Fischer's avatar
Jens-Christian Fischer committed
1 2 3 4 5 6 7 8 9 10 11 12 13
defmodule Grengine.Conductor do

  use GenServer
  require Logger

  def start_link, do: GenServer.start_link(__MODULE__, 1000, name: __MODULE__)

  def init(state) do
    tick()

    {:ok, state}
  end

Jens-Christian Fischer's avatar
Jens-Christian Fischer committed
14
  defp tick, do: Process.send_after(self(), :tick, 1000)
Jens-Christian Fischer's avatar
Jens-Christian Fischer committed
15 16 17

  def handle_info(:tick, state) do
    Logger.info("handle tick")
Jens-Christian Fischer's avatar
Jens-Christian Fischer committed
18 19
    get_performances()
    |> Enum.each( fn perf ->
Jens-Christian Fischer's avatar
Jens-Christian Fischer committed
20
      do_performance(perf)
Jens-Christian Fischer's avatar
Jens-Christian Fischer committed
21
    end)
Jens-Christian Fischer's avatar
Jens-Christian Fischer committed
22
    tick()
Jens-Christian Fischer's avatar
Jens-Christian Fischer committed
23
    {:noreply, state}
Jens-Christian Fischer's avatar
Jens-Christian Fischer committed
24 25 26 27 28 29


  end

  def get_performances do
    # see https://github.com/paulanthonywilson/gproc_select_examples/blob/master/test/gproc_select_test.exs
Jens-Christian Fischer's avatar
Jens-Christian Fischer committed
30 31
    # matcher = :ets.fun2ms(fn x -> x end)
    matcher = [{:"_", [], [:"$$"]}]
Jens-Christian Fischer's avatar
Jens-Christian Fischer committed
32 33 34
    :gproc.select(matcher)
  end

Jens-Christian Fischer's avatar
Jens-Christian Fischer committed
35 36
  def do_performance(perf) do
    {_, _, name, pid} = perf
37
    Logger.info(name)
Jens-Christian Fischer's avatar
Jens-Christian Fischer committed
38 39 40 41
  end


end