Elixirだ 第4回
-
Upload
joenoh -
Category
Technology
-
view
109 -
download
4
Transcript of Elixirだ 第4回
訃報{ :DOWN, #Reference<0.0.0.204>, :process, #PID<0.97.0>, { %RuntimeError{message: “…”}, [{:erlang, :apply, 2, []}] } }
> プロセス<0.97.0>が落ちた
リンクさせる$ iex
iex(1)> pid = spawn fn -> ...(1)> :timer.sleep 20000 ...(1)> raise RuntimeError ...(1)> end
iex(2)> Process.link pid
iexが共倒れして再起動する
spawn_link
> spawn直後に死んだら > ちゃんとリンクできない > spawnと同時にlinkする
pid = spawn fn -> … end Process.link pid
pid = spawn_link fn -> … end
supervisor> OTPのひとつ > 子プロセスを死活監視する > 再起動戦略 > one_for_one > one_for_all > rest_for_one > simple_one_for_one
再起動戦略> rest_for_one > 1個死ぬ > 死んだ奴より後に起動したプロセスを全て再起動
> simple_one_for_one > 基本はone_for_one > 1種類の子プロセスしか持てない
lib/echo.exdef start(_type, _args) do import Supervisor.Spec, warn: false
children = [worker(Echo.Server, [])]
opts = [ strategy: :one_for_one, name: Echo.Supervisor ] Supervisor.start_link(children, opts) end
lib/echo/server.exdefmodule Echo.Server do alias GenServer, as: GS alias EchoServer, as: ES use GS
def start_link do GS.start_link(ES, [], name: ES) end
lib/echo/server.ex def crash! do GS.cast(ES, :crash) end
def handle_cast(:crash, _) do raise RuntimeError, message: “Oops!” end
lib/echo/server.ex def echo(term) do GS.call(ES, {:echo, term}) end
def handle_call({:echo, str}, _, s) do {:reply, str, s} end end
lib/echo.exdef start(_type, _args) do # 中略 opts = [ strategy: :one_for_one, name: Echo.Supervisor, max_restarts: 2, max_seconds: 10 ] Supervisor.start_link(children, opts) end
lib/echo.exdef start(_type, _args) do # 中略 opts = [ strategy: :one_for_one, name: Echo.Supervisor, max_restarts: 2, max_seconds: 10 ] Supervisor.start_link(children, opts) end
10秒間に2回までしか 再起動しない
息の根を止めろ> 急いで3回殺してみる
iex(1)> Echo.Server.crash! iex(2)> Echo.Server.crash! iex(3)> Echo.Server.crash!
[info] Application echo exited: shutdown
プロセスだ 総復習> Fibonacci.Server > one_for_oneで監視 > Fibonacci.next/0 > 0, 1, 1, 2, 3, 5, 8 > 13は返さず死ぬ > RuntimeError