Last active
April 3, 2019 13:28
-
-
Save sescobb27/0d5a6da1be2c8a2df2591c30a0b0df43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule RestartServer do | |
use GenServer | |
def start_link(args) do | |
GenServer.start_link(__MODULE__, args, [name: __MODULE__]) | |
end | |
def init(_) do | |
IO.puts("RESTARTING") | |
num = Enum.random(1..1_000_000) | |
{:ok, num} | |
end | |
end | |
defmodule RestartSupervisor do | |
use Supervisor | |
def start_link(args) do | |
Supervisor.start_link(__MODULE__, args) | |
end | |
@impl true | |
def init(_) do | |
opts = [strategy: :one_for_one] | |
Supervisor.init([{RestartServer, []}], opts) | |
end | |
end | |
Process.flag(:trap_exit, true) | |
RestartSupervisor.start_link([]) | |
pid = Process.whereis(RestartServer) | |
:sys.get_state(pid) | |
Process.exit(pid, :kill) | |
pid = Process.whereis(RestartServer) | |
:sys.get_state(pid) | |
# iex(1)> Process.flag(:trap_exit, true) | |
# false | |
# iex(2)> RestartSupervisor.start_link([]) | |
# RESTARTING | |
# {:ok, #PID<0.143.0>} | |
# iex(3)> pid = Process.whereis(RestartServer) | |
# #PID<0.144.0> | |
# iex(4)> :sys.get_state(pid) | |
# 471361 | |
# iex(5)> Process.exit(pid, :kill) | |
# true | |
# RESTARTING | |
# iex(6)> pid = Process.whereis(RestartServer) | |
# #PID<0.148.0> | |
# iex(7)> :sys.get_state(pid) | |
# 898523 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment