Skip to content

Instantly share code, notes, and snippets.

José Valim josevalim

Block or report user

Report or block josevalim

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View rotor.exs
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
View supervisor.ex
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
@josevalim
josevalim / server.ex
Created Jun 8, 2014
Table inheritance with supervisors
View server.ex
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)
View gist:f0d9025bb3f5a580aee6
=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}]}]}
View exercise.exs
@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
View gist:3e1817e2e9b5b282fdbf
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
View shutdown.exs
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)
View parsec.exs
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)
View epmd
+ 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
View exq_web_router.ex
defmodule Exq.RouterPlug do
import Plug.Conn
use Plug.Router
plug :match
plug :dispatch
get "/queues" do
IO.puts "YOLO"
conn |> halt()
You can’t perform that action at this time.