我缩小了问题的大小,因为它太大了。这是代码:

defmodule MayRaiseGenServer do
  use GenServer

  def start_link do
    IO.puts "started MyServer, name is #{__MODULE__}"

    GenServer.start_link(__MODULE__, [], name: __MODULE__)
  end

  def maybe_will_raise do
    GenServer.call(__MODULE__, :maybe_will_raise)
  end

  def handle_call(:maybe_will_raise,_from, state) do
    IO.puts "maybe_will_raise called!"
    :random.seed(:erlang.now)
    number = Enum.to_list(1..100) |> Enum.shuffle |> List.first
    IO.puts "number is #{number}"
    if rem(number,2) != 0 do
      raise "#{number}"
    end
    {:reply, {"You got lucky"}, state}
  end
end

defmodule MayRaiseSupervisor do
  use Supervisor

  def start_link([]) do
    IO.puts "starting supervisor, name is #{__MODULE__}"
    Supervisor.start_link(__MODULE__, [])
  end

  def init(arg) do
    IO.puts "initted with arg: #{arg}"
    children = [
      worker(MayRaiseGenServer, [])
    ]

    supervise(children, strategy: :one_for_one, restart: :transient, name: __MODULE__)
  end
end

MayRaiseSupervisor.start_link([])
IO.inspect MayRaiseGenServer.maybe_will_raise
:timer.sleep(2000)
IO.puts "after sleep"

最初,我只看到一次启动GenServer的消息,但是现在我又看到了。这是输出:
starting supervisor, name is Elixir.MayRaiseSupervisor
initted with arg:
started MyServer, name is Elixir.MayRaiseGenServer
maybe_will_raise called!
number is 14
started MyServer, name is Elixir.MayRaiseGenServer

11:32:28.807 [error] GenServer MayRaiseGenServer terminating
** (RuntimeError) 14
    lib/mini.ex:20: MayRaiseGenServer.handle_call/3
    (stdlib) gen_server.erl:615: :gen_server.try_handle_call/4
    (stdlib) gen_server.erl:647: :gen_server.handle_msg/5
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Last message: :maybe_will_raise
State: []
** (exit) exited in: GenServer.call(MayRaiseGenServer, :maybe_will_raise, 5000)
    ** (EXIT) an exception was raised:
        ** (RuntimeError) 14
            lib/mini.ex:20: MayRaiseGenServer.handle_call/3
            (stdlib) gen_server.erl:615: :gen_server.try_handle_call/4
            (stdlib) gen_server.erl:647: :gen_server.handle_msg/5
            (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
    (elixir) lib/gen_server.ex:604: GenServer.call/3
    lib/mini.ex:45: (file)
    (elixir) lib/code.ex:363: Code.require_file/2

从上面的输出中,我不太清楚会发生什么。根据IO上显示的消息,看起来GenServer已重新启动,但是为什么再次抛出异常?另外,在此代码中:
MayRaiseSupervisor.start_link([])
IO.inspect MayRaiseGenServer.maybe_will_raise
:timer.sleep(2000)
IO.puts "after sleep"

如果调用MayRaiseGenServer.maybe_will_raise的方法确实会引发错误,则看起来像后面的几行,带有timer.sleepIO.puts的行将不再运行。即使我更改代码来尝试处理异常,也是如此:
MayRaiseSupervisor.start_link([])
try do
  IO.inspect MayRaiseGenServer.maybe_will_raise
rescue
  RuntimeError -> IO.puts "there was an error"
end
:timer.sleep(2000)
IO.puts "after sleep"

我似乎仍然无法到达最后的IO.puts(如果出现错误)。有没有一种方法可以处理对maybe_will_raise的调用,使我可以处理它引发错误并继续执行?我猜想主管在重新启动后不会自动重试一段代码。

最佳答案

以我的观点。

上面的输出告诉您堆栈跟踪,何时使用GenServer.call(MayRaiseGenServer, :maybe_will_raise, 5000)中的退出信号和错误日志引发异常,这是因为terminate/2被调用的原因是{%RuntimeError{message: ...}, [...]

您可以定义terminate/2回调以查看:

def terminate(reason, _state) do
  IO.inspect reason
end

Terminate/2



但是,当在GenServer回调中引发异常(init/1除外)时,它将调用terminate/2告知服务器即将退出(已发送退出信号)。

因此,此行之后的代码将不会执行:
try do
IO.inspect MayRaiseGenServer.maybe_will_raise
...



当您退出GenServer时,主管也会启动一个新的链接,该链接将主进程与您的GenServer进程链接起来(您的MayRaiseGenServer.start_link再次被调用)

最后一件事是如果要使代码继续执行,您可以像这样捕获退出信号:
MayRaiseSupervisor.start_link([])
try do
  IO.inspect MayRaiseGenServer.maybe_will_raise
catch
  :exit, _ -> IO.puts "there was an error"
end
:timer.sleep(2000)
IO.puts "after sleep"

但我认为您应该考虑在GenServer回调中使用raise。希望有帮助!

关于elixir - 受监督的GenServer是否无法重新启动?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39278862/

10-10 17:20