fly49

fly49

How to properly stop Producer -> ConsumerSupervisor?

I am quite new to Elixir and trying to understand GenStage flow.

I created a producer that emits a finite list of numbers

defmodule Producer do
  use GenStage

  def start_link(_) do
    GenStage.start_link(Producer, Enum.to_list(1..10), name: Producer)
  end

  def init(list) do
    {:producer, list}
  end

  def handle_demand(demand, state) when demand > 0 do
    case Enum.split(state, demand) do
      {[],[]} ->
        GenStage.async_info(self(), :stop)
        {:noreply, [], []}

      {pulled, remaining} ->
        {:noreply, pulled, remaining}
    end
  end

  def handle_info(:stop, state) do
    {:stop, :normal, state}
  end
end

and ConsumerSupervisor that starts new children for each number

defmodule ConSupervisor do
  use ConsumerSupervisor

  @opts [
    strategy: :one_for_one,
    subscribe_to: [{Producer, max_demand: 2, min_demand: 0}],
    max_restarts: 20
  ]

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

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

    ConsumerSupervisor.init(children, @opts)
  end
end

Children are just tasks with 30% rate of success

defmodule Consumer do
  def start_link(event) do
    Task.start_link(fn ->
      Process.sleep(1000)

      if :rand.uniform(3) == 3 do
        IO.puts("task failed with #{event}")
        raise("ooops")
      end

      num = Kernel.inspect(event)
      IO.puts("Inserted #{num}")
    end)
  end
end

I want to terminate Producer the way that it can be assured that all Tasks are completed. For now when it terminates it emits cancel for ConsumerSupervisor that discard all Tasks retrying to finish successfully.

[info] GenStage consumer #PID<0.433.0> is stopping after receiving cancel from producer #PID<0.431.0> with reason: :normal

I feel like it should be tricky and require some consecutive cast/call messaging, but maybe there is more straightforward way to do this?

Marked As Solved

NeutronStein

NeutronStein

As @Ljzn mentioned you can check active workers with count_children and stop if there are none active.

  def handle_info(:stop, state) do
    %{active: active} = ConsumerSupervisor.count_children(consumer_pid_or_registered_name)
    if active > 0 do
      Process.send_after(self(), :stop, 5000)
      {:noreply, [], state}
    else
      {:stop, :normal, state}
    end
  end

Also Liked

Ljzn

Ljzn

I’m not very familiar with genState. Maybe you can use ConsumerSupervisor.count_children/1 repeatedly to check is all tasks completed.

Where Next?

Popular in Questions Top

SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement