我缩小了问题的大小,因为它太大了。这是代码:
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.sleep
和IO.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
如果原因不是:正常,则:shutdown和{:shutdown,term}将记录错误。
但是当在GenServer回调中引发异常(init/1
除外)时,它将调用terminate/2
告诉服务器即将退出(退出信号已发送)。
因此,此行之后的代码将不会执行:
try do
IO.inspect MayRaiseGenServer.maybe_will_raise
...
但不应该再次出现IO.puts“启动MyServer”输出?
当您的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
。希望有所帮助!
错误地,我使用GenServer.start
而不是GenServer.start_link
并搜索了两天的答案。
注1::observer.start
中的主管树帮助调试了这个问题
注2:但仅限使用start_link
的问题。我正在为像我一样犯同样错误的人写作:-P。
服务器正在重新启动。为了证明这一点,我添加了这个片段
spawn fn ->
:timer.sleep(1000)
IO.inspect GenServer.call(__MODULE__, :maybe_will_raise)
end
在两个地方。一个在handle_call回调中,我也在init函数中添加了它。之后是完整的模块
defmodule MayRaiseGenServer do
@moduledoc false
use GenServer
def start_link do
IO.puts "started MyServer, name is #{__MODULE__}"
GenServer.start_link(__MODULE__, [], name: __MODULE__)
end
def init(_) do
spawn fn ->
:timer.sleep(1000)
IO.inspect GenServer.call(__MODULE__, :maybe_will_raise)
end
{:ok, nil}
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
spawn fn ->
:timer.sleep(1000)
IO.inspect GenServer.call(__MODULE__, :maybe_will_raise)
end
{:reply, {"You got lucky"}, state}
end
end
那你就不用打电话了
IO.inspect MayRaiseGenServer.maybe_will_raise
后
MayRaiseSupervisor.start_link([])
试试看吧