【发布时间】:2019-07-09 21:26:43
【问题描述】:
两周以来,我试图对我们公司的 Elixir 应用程序进行一次完整的重构,因为我们有太多的流程问题。
所以我从头开始,一步一步做。现在,自从我在主管中开始工作时,几乎 3 天以来我都面临着同样的错误:bad_return。我的进程树是这样的:
Application
|- MainSupervisor
|- Some workers (Extreme for EventStore, Repo for PostgreSQL, and a stream subscriber for eventstore)
|- AccountStreamSupervisor
|- AccountStreamDispatcher (Supervisor)
|- StreamSubscriber (Worker)
dispatcher 和subscriber 都有start_child 函数(所以稍后会在运行时使用)
我用Supervisor.start_link/2 为每个主管初始化我的树。应用程序、MainSupervisor、AccountStreamSupervisor 启动没有问题,但是在初始化 AccountStreamDispatcher 时,我有这个:bad_return 错误。
跟踪表明 AccountStreamDispatcher 的 init/1 是问题所在,因为它返回 {:ok, #PID<0.392.0>(根据文档,这是一个很好的响应)。
我尝试了很多东西,比如更改start_link 和init 方法签名,更改子声明,总是一样。我知道没有我的调度员,一切都会正确启动...
这是一些代码:
defmodule MainSupervisor do
use Supervisor
require Logger
def start_link(_args) do
Logger.info("MainSupervisor => Starting...")
result = Supervisor.start_link(__MODULE__, name: :main_supervisor)
case result do
{:ok, _} ->
nil
:ignore ->
Logger.error("Unable start main supervisor because is ignored")
{:error, {:already_started, _}} ->
Logger.error("Unable start main supervisor because is already started")
{:error, {:shutdown, reason}} ->
Logger.error("Unable start main supervisor because #{IO.inspect(reason)}")
{:error, reason} ->
Logger.error("Unable start main supervisor because #{IO.inspect(reason)}")
end
result
end
def init(_) do
Logger.info("MainSupervisor => Initializing...")
event_store_settings = Application.get_env(:extreme, :event_store)
children = [
[...]
%{
id: ViewBuilder.V2.AccountStreamSupervisor,
start: {ViewBuilder.V2.AccountStreamSupervisor, :start_link, []},
type: :supervisor
}
]
Supervisor.start_link(children, strategy: :one_for_one)
end
end
defmodule AccountStreamSupervisor do
use Supervisor
require Logger
def start_link do
Logger.info("AccountStreamSupervisor => Starting...")
result = Supervisor.start_link(__MODULE__, name: :account_supervisor)
case result do
{:ok, _} ->
nil
:ignore ->
Logger.error("Unable start account stream supervisor because is ignored")
{:error, {:already_started, _}} ->
Logger.error("Unable start account stream supervisor because is already started")
{:error, {:shutdown, reason}} ->
Logger.error("Unable start account stream supervisor because #{IO.inspect(reason)}")
{:error, reason} ->
Logger.error("Unable start account stream supervisor because #{IO.inspect(reason)}")
end
result
end
def init(_) do
Logger.info("AccountStreamSupervisor => Initializing...")
children = [
%{
id: AccountStreamDispatcher,
start: {AccountStreamDispatcher, :start_link, []},
type: :supervisor
}
]
Supervisor.start_link(children, strategy: :one_for_one)
end
def start_child(account_stream_name) do
Logger.debug(
"AccountStreamSupervisor => Start a new child - AccountStreamDispatcher with the name: #{
account_stream_name
}"
)
Supervisor.start_child(:account_supervisor, [])
end
end
defmodule AccountStreamDispatcher do
use Supervisor
require Logger
def start_link do
Logger.debug("AccountStreamDispatcher => Starting...")
result = Supervisor.start_link(__MODULE__, name: :account_dispatcher)
IO.inspect(result)
case result do
{:ok, _} ->
nil
:ignore ->
Logger.error("Unable start dispatcher because is ignored")
{:error, {:already_started, pid}} ->
Logger.debug("Dispatcher is already started with pid #{pid}")
{:error, reason} ->
Logger.error("Unable start dispatcher because #{IO.inspect(reason)}")
end
result
end
def init(_) do
Logger.info("AccountStreamDispatcher => Initializing...")
children = [
%{
id: StreamSubscriber,
start: {StreamSubscriber, :start_link, []},
type: :supervisor
}
]
Supervisor.start_link(children, [strategy: :one_for_one])
end
def start_child(account_stream_name, type, account_id, sub_keys) do
Logger.debug(
"AccountStreamDispatcher => Start a new child - StreamSubscriber with the name: #{
account_stream_name
}"
)
Supervisor.start_child(
:account_dispatcher,
[
%{
stream_name: account_stream_name,
stream_type: type,
account_id: account_id,
sub_keys: sub_keys
}
]
)
end
end
defmodule StreamSubscriber do
use GenServer
require Logger
alias EventHandler.EventHandlerProvider, as: EventHandlerProvider
def start_link(
args = %{
stream_name: name,
stream_type: _type,
account_id: _account_id,
sub_keys: _sub_keys
}
) do
Logger.debug("StreamSubscriber => Starting... (#{name})")
result = GenServer.start_link(__MODULE__, args, name: name)
case result do
{:ok, _} ->
nil
:ignore ->
Logger.error("Unable start process #{name} because is ignored")
{:error, {:already_started, _}} ->
Logger.error("Unable start process #{name} because is already started")
{:error, reason} ->
Logger.error("Unable start process #{name} because #{IO.inspect(reason)}")
end
result
end
def init(%{stream_name: name, stream_type: type, account_id: account_id, sub_keys: sub_keys}) do
Logger.debug("StreamSubscriber => Initializing... (#{name})")
state = %{stream_name: name, stream_type: type, account_id: account_id, sub_keys: sub_keys}
{:ok, _} = EventHandlerProvider.create_handler(type, name, account_id, sub_keys)
{:ok, state}
end
end
我做错了什么?
【问题讨论】: