Skip to content

Instantly share code, notes, and snippets.

View josevalim's full-sized avatar

José Valim josevalim

View GitHub Profile
defmodule Rotor do
@doc """
Starts a new rotor as part of a supervision tree.
## Options
* `:name` - the registered name of the rotor
"""
def start_link(paths, opts) do
@josevalim
josevalim / server.ex
Created June 8, 2014 14:05
Table inheritance with supervisors
defmodule Todo.Manager do
use GenServer
## Client API
@doc """
Starts the `Todo.Manager`.
"""
def start_link(table, sup, event, opts \\ []) do
{:ok, pid} = GenServer.start_link(__MODULE__, {table, sup, event}, opts)
@josevalim
josevalim / supervisor.ex
Created June 8, 2014 14:21
Public tables
defmodule Todo.Supervisor do
use Supervisor
def start_link do
ets = :ets.new(@manager,
[:set, :public, :named_table, {:read_concurrency, true}])
Supervisor.start_link(__MODULE__, ets, name: Todo.Supervisor)
end
@manager Todo.Manager
=ERROR REPORT==== 12-Jun-2014::15:23:03 ===
** Task <0.38.0> terminating
** Reason for termination ==
** {badarg,[{erlang,hd,[[]],[]},
{'Elixir.Agent.Server',handle_call,3,
[{file,"lib/agent/server.ex"},{line,11}]},
{gen_server,handle_msg,5,[{file,"gen_server.erl"},{line,580}]},
{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,239}]}]}
@proto_version "1.0"
def process_options(opts) do
ret = []
if opts[:in], do: ret = ["-in"|ret]
case opts[:err] do
:out -> ret = ["-err", "out"|ret]
:err -> ret = ["-err", "err"|ret]
_ -> nil
iex(1)> defmodule Foo do
...(1)> use GenEvent
...(1)> end
{:module, Foo,
<<70, 79, 82, 49, 0, 0, 8, 144, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 2, 7, 131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95, 118, 49, 108, 0, 0, 0, 2, 104, 2, 100, 0, 4, ...>>,
[true, true, true, true, true, true]}
iex(2)> {:ok, pid} = GenEvent.start_link
{:ok, #PID<0.50.0>}
iex(3)> GenEvent.add_handler(pid, Foo, [])
:ok
iex(2)> import Supervisor.Spec
nil
iex(3)> Supervisor.start_link([worker(Task, [fn -> Process.register(self, :hello); :timer.sleep(:infinity) end])], strategy: :one_for_one)
{:ok, #PID<0.61.0>}
iex(4)> Process.whereis(:hello)
#PID<0.62.0>
iex(5)> Process.exit(self(), :shutdown)
** (EXIT from #PID<0.57.0>) shutdown
Interactive Elixir (0.15.2-dev) - press Ctrl+C to exit (type h() ENTER for help)
defmodule Parsec do
# Handles parsec for ..., do: ...
defmacro parsec({:for, _, args}) do
{exprs, [[do: return]]} = Enum.split(args, -1)
do_parsec(exprs, return)
end
# Handles parsec for ... do ... end
defmacro parsec({:for, _, args}, do: return) do
do_parsec(args, return)
+ EPMD
- Distributed systems are cool
- Distributed systems are also complex
- First step: How to find other machines (and nodes) in the same network
- Erlang Port Mapper Daemon
+ EPMD in C
- Ships with Erlang
- is a TCP server
- usually 4369
defmodule Exq.RouterPlug do
import Plug.Conn
use Plug.Router
plug :match
plug :dispatch
get "/queues" do
IO.puts "YOLO"
conn |> halt()