fcabouat

fcabouat

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 ConsumerSupervisor — gen_stage v1.3.2 : 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,

Showing Posts 1 to 3

NobbZ

NobbZ

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

fcabouat

fcabouat OP

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]}]}   

fcabouat

fcabouat OP

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 gen_stage/examples/consumer_supervisor.exs at main · elixir-lang/gen_stage · GitHub example helped a lot) with cold head and I could find it.

Thanx.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews