Skip to content

Instantly share code, notes, and snippets.

@sescobb27
Last active April 3, 2019 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sescobb27/0d5a6da1be2c8a2df2591c30a0b0df43 to your computer and use it in GitHub Desktop.
Save sescobb27/0d5a6da1be2c8a2df2591c30a0b0df43 to your computer and use it in GitHub Desktop.
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