ConsumerSupervisor failed to start child from :undef error

Hi,

I’m not able to create and test basic ConsumerSupervisor behavior.

I tried to follow the docs, but had a few issues. First one is that my child spec lacked “restart: :transient” (not present in the docs Printer example https://hexdocs.pm/gen_stage/ConsumerSupervisor.html : should I create an issue ?).

Here’s my code

defmodule BasicConsumerSupervisor do
  defmodule MyConsumerSupervisor do
    use ConsumerSupervisor

    def start_link(arg) do
      ConsumerSupervisor.start_link(__MODULE__, arg)
    end

    def init(_arg) do
      children = [%{id: MyConsumer, start: {MyConsumer, :start_link, []}, restart: :transient}]
      opts = [strategy: :one_for_one]
      ConsumerSupervisor.init(children, opts)
    end
  end

  defmodule MyConsumer do
    def start_link(event, truc) do
      Task.start_link(fn ->
        IO.inspect({self(), event, truc})
      end)
    end
  end
end

And Iex session with errors

iex(2)> {:ok, c} = BasicConsumerSupervisor.MyConsumerSupervisor.start_link([])
{:ok, #PID<0.323.0>}
iex(3)> {:ok, p} = 0..100 |> Enum.to_list |> GenStage.from_enumerable()               
{:ok, #PID<0.325.0>}
iex(4)> GenStage.sync_subscribe(c, to: p)                                             
{:ok, #Reference<0.1632678573.1983643649.222301>}
iex(5)> 
18:44:26.775 [error] ConsumerSupervisor failed to start child from: {#PID<0.325.0>, #Reference<0.1632678573.1983643649.222301>} with reason: {:undef, [{MyConsumer, :start_link, [0], []}, {ConsumerSupervisor, :start_child, 3, [file: 'lib/consumer_supervisor.ex', line: 564]}, {ConsumerSupervisor, :start_events, 6, [file: 'lib/consumer_supervisor.ex', line: 424]}, {ConsumerSupervisor, :handle_events, 3, [file: 'lib/consumer_supervisor.ex', line: 413]}, {GenStage, :consumer_dispatch, 6, [file: 'lib/gen_stage.ex', line: 2329]}, {:gen_server, :try_dispatch, 4, [file: 'gen_server.erl', line: 637]}, {:gen_server, :handle_msg, 6, [file: 'gen_server.erl', line: 711]}, {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 249]}]}


18:44:26.783 [error] ConsumerSupervisor failed to start child from: {#PID<0.325.0>, #Reference<0.1632678573.1983643649.222301>} with reason: {:undef, [{MyConsumer, :start_link, [1], []}, {ConsumerSupervisor, :start_child, 3, [file: 'lib/consumer_supervisor.ex', line: 564]}, {ConsumerSupervisor, :start_events, 6, [file: 'lib/consumer_supervisor.ex', line: 424]}, {ConsumerSupervisor, :handle_events, 3, [file: 'lib/consumer_supervisor.ex', line: 413]}, {GenStage, :consumer_dispatch, 6, [file: 'lib/gen_stage.ex', line: 2329]}, {:gen_server, :try_dispatch, 4, [file: 'gen_server.erl', line: 637]}, {:gen_server, :handle_msg, 6, [file: 'gen_server.erl', line: 711]}, {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 249]}]}


18:44:26.800 [error] ConsumerSupervisor failed to start child from: {#PID<0.325.0>, #Reference<0.1632678573.1983643649.222301>} with reason: {:undef, [{MyConsumer, :start_link, [2], []}, {ConsumerSupervisor, :start_child, 3, [file: 'lib/consumer_supervisor.ex', line: 564]}, {ConsumerSupervisor, :start_events, 6, [file: 'lib/consumer_supervisor.ex', line: 424]}, {ConsumerSupervisor, :handle_events, 3, [file: 'lib/consumer_supervisor.ex', line: 413]}, {GenStage, :consumer_dispatch, 6, [file: 'lib/gen_stage.ex', line: 2329]}, {:gen_server, :try_dispatch, 4, [file: 'gen_server.erl', line: 637]}, {:gen_server, :handle_msg, 6, [file: 'gen_server.erl', line: 711]}, {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 249]}]}

Any ideas what could be wrong ?

cheers,

There is no module named MyConsumer, you probably mean BasicConsumerSupervisor.MyConsumer.

You’re right, I fixed it. But I still get the same error though.

defmodule BasicConsumerSupervisor.MyConsumerSupervisor do
  use ConsumerSupervisor

  def start_link(arg) do
    ConsumerSupervisor.start_link(__MODULE__, arg)
  end

  def init(_arg) do
    children = [
      %{
        id: BasicConsumerSupervisor.MyConsumer,
        start: {BasicConsumerSupervisor.MyConsumer, :start_link, []},
        restart: :transient
      }
    ]

    opts = [strategy: :one_for_one]
    ConsumerSupervisor.init(children, opts)
  end
end

defmodule BasicConsumerSupervisor.MyConsumer do
  def start_link(event, truc) do
    Task.start_link(fn ->
      IO.inspect({self(), event, truc})
    end)
  end
end

{:ok, c} = BasicConsumerSupervisor.MyConsumerSupervisor.start_link([])
{:ok, p} = 0..10 |> Enum.to_list() |> GenStage.from_enumerable()
GenStage.sync_subscribe(c, to: p)
Process.sleep(:infinity)

=>

20:49:47.967 [error] ConsumerSupervisor failed to start child from: {#PID<0.145.0>, #Reference<0.3581977563.3345743873.173221>} with reason: {:undef, [{BasicConsumerSupervisor.MyConsumer, :start_link, [0], []}, {ConsumerSupervisor, :start_child, 3, [file: 'lib/consumer_supervisor.ex', line: 564]}, {ConsumerSupervisor, :start_events, 6, [file: 'lib/consumer_supervisor.ex', line: 424]}, {ConsumerSupervisor, :handle_events, 3, [file: 'lib/consumer_supervisor.ex', line: 413]}, {GenStage, :consumer_dispatch, 6, [file: 'lib/gen_stage.ex', line: 2329]}, {:gen_server, :try_dispatch, 4, [file: 'gen_server.erl', line: 637]}, {:gen_server, :handle_msg, 6, [file: 'gen_server.erl', line: 711]}, {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 249]}]}                                                                                                                                                                                         


20:49:47.967 [error] ConsumerSupervisor failed to start child from: {#PID<0.145.0>, #Reference<0.3581977563.3345743873.173221>} with reason: {:undef, [{BasicConsumerSupervisor.MyConsumer, :start_link, [1], []}, {ConsumerSupervisor, :start_child, 3, [file: 'lib/consumer_supervisor.ex', line: 564]}, {ConsumerSupervisor, :start_events, 6, [file: 'lib/consumer_supervisor.ex', line: 424]}, {ConsumerSupervisor, :handle_events, 3, [file: 'lib/consumer_supervisor.ex', line: 413]}, {GenStage, :consumer_dispatch, 6, [file: 'lib/gen_stage.ex', line: 2329]}, {:gen_server, :try_dispatch, 4, [file: 'gen_server.erl', line: 637]}, {:gen_server, :handle_msg, 6, [file: 'gen_server.erl', line: 711]}, {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 249]}]}                                                                                                                                                                                         


20:49:47.968 [error] ConsumerSupervisor failed to start child from: {#PID<0.145.0>, #Reference<0.3581977563.3345743873.173221>} with reason: {:undef, [{BasicConsumerSupervisor.MyConsumer, :start_link, [2], []}, {ConsumerSupervisor, :start_child, 3, [file: 'lib/consumer_supervisor.ex', line: 564]}, {ConsumerSupervisor, :start_events, 6, [file: 'lib/consumer_supervisor.ex', line: 424]}, {ConsumerSupervisor, :handle_events, 3, [file: 'lib/consumer_supervisor.ex', line: 413]}, {GenStage, :consumer_dispatch, 6, [file: 'lib/gen_stage.ex', line: 2329]}, {:gen_server, :try_dispatch, 4, [file: 'gen_server.erl', line: 637]}, {:gen_server, :handle_msg, 6, [file: 'gen_server.erl', line: 711]}, {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 249]}]}   

Ok, I’m quite stupid : I propagated old errors into new code : took me a while before understanding the first error (ConsumerSupervisor needing restart: transient or restart: temporary), and then I had remaining errors regarding functions arity. Here this is the start_link of BasicConsumerSupervisor.MyConsumer, which was wrong : I had added a second parameter which was superfluous.

Isolating things properly (the working https://github.com/elixir-lang/gen_stage/blob/master/examples/consumer_supervisor.exs example helped a lot) with cold head and I could find it.

Thanx.