代码之家  ›  专栏  ›  技术社区  ›  user3139545

理解药剂BADARG错误信息

  •  2
  • user3139545  · 技术社区  · 6 年前

    当我试图从一个 DynamicSupervisor 我得到以下错误:

    {:error,
     {:EXIT,
      {:badarg,
       [
         {:erlang, :apply,
          [
            BfgEngine.MarketService,
            :start_link,
            {{BfgEngine.MarketService, :start_link, ["1111"]}, :permanent, 5000,
             :worker, [BfgEngine.MarketService]}
          ], []},
         {:supervisor, :do_start_child_i, 3, [file: 'supervisor.erl', line: 379]},
         {:supervisor, :handle_call, 3, [file: 'supervisor.erl', line: 404]},
         {:gen_server, :try_handle_call, 4, [file: 'gen_server.erl', line: 661]},
         {:gen_server, :handle_msg, 6, [file: 'gen_server.erl', line: 690]},
         {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 249]}
       ]}}}
    

    IM使用的代码是:

      def start_market(market_id) do
        spec = {MarketService, market_id}
        DynamicSupervisor.start_child(__MODULE__, spec)
      end
    

    但是我不清楚出了什么问题。哪个函数的参数不正确?如何分解并读取给定的错误消息?

    更新:

    这是我主管的初始化方法:

      @impl true
      def init(initial_arg) do
        DynamicSupervisor.init(
          strategy: :one_for_one,
          extra_arguments: [initial_arg]
        )
      end
    

    更新2: 这是市场服务的起始链接:

      def start_link(market_id) when is_bitstring(market_id) do
        GenServer.start_link(__MODULE__, market_id, name: via_tuple(market_id))
      end
    

    IM使用默认值 child_spec 我来自 GenServer

    更新3: 更改为:

      def start_market(market_id) do
        spec = {MarketService, market_id: market_id}
        DynamicSupervisor.start_child(__MODULE__, spec)
      end
    

    给予:

    {:error,
     {:undef,
      [
        {BfgEngine.MarketService, :start_link, [[], [market_id: "222"]], []},
        {DynamicSupervisor, :start_child, 3,
         [file: 'lib/dynamic_supervisor.ex', line: 654]},
        {DynamicSupervisor, :handle_start_child, 2,
         [file: 'lib/dynamic_supervisor.ex', line: 640]},
        {:gen_server, :try_handle_call, 4, [file: 'gen_server.erl', line: 661]},
        {:gen_server, :handle_msg, 6, [file: 'gen_server.erl', line: 690]},
        {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 249]}
      ]}}
    

    更改为:

      def start_market(market_id) do
        spec = {MarketService, :market_id, market_id}
        DynamicSupervisor.start_child(__MODULE__, spec)
      end
    

    给予:

    ** (ArgumentError) supervisors expect each child to be one of:
    
      * a module
      * a {module, arg} tuple
      * a child specification as a map with at least the :id and :start fields
      * or a tuple with 6 elements generated by Supervisor.Spec (deprecated)
    
    Got: {BfgEngine.MarketService, :market_id, "222"}
    
        (elixir) lib/supervisor.ex:657: Supervisor.init_child/1
        (elixir) lib/supervisor.ex:744: Supervisor.child_spec/2
        (elixir) lib/dynamic_supervisor.ex:304: DynamicSupervisor.start_child/2
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   Milan Jaric    6 年前

    从发布的代码很难分辨,但你能试着改变吗 start_market 到:

      def start_market(market_id) do
        spec = {MarketService, :market_id, market_id}
        DynamicSupervisor.start_child(__MODULE__, spec)
      end
    

    更新(以下是两个选项):

      def start_market(market_id) do
        spec = &{
          id: MarketService,
          start: {MarketService, start_link, [market_id]},
          type: :worker
        }
        DynamicSupervisor.start_child(__MODULE__, spec)
      end
    

    或者

      def start_market(market_id) do
        spec = {MarketService, [market_id]}
        DynamicSupervisor.start_child(__MODULE__, spec)
      end
    
        2
  •  1
  •   Hynek -Pichi- Vychodil Paulo Suassuna    6 年前

    你有 badarg 功能异常 erlang:apply/3 当有三个论点时 BfgEngine.MarketService 我是说, :start_link {{BfgEngine.MarketService, :start_link, ["1111"]}, :permanent, 5000, :worker, [BfgEngine.MarketService]} 它发生在功能上 supervisor:do_start_child_i/3 是的。

    函数的参数 erlang:应用/3 应该是mfa a.k.a模块、函数、参数。 {{bfgengine.marketservice,:开始链接,[“1111”]},:永久的,5000,:工作者,[bfgengine.marketservice]} 不是争论,因为它显然不是 参数列表 是的。从你的代码中,我可以猜出错误是变量的内容 spec 是的。你应该提供一些道具或地图。我不知道,你应该看看 DynamicSupervisor 小心点。