mmmmillar
GenStage Consumer appears to be dropping events
Problem:
As a first go at using GenStage, I’m building a simple task queue. Initially it used consumers that I would add explicitly in my Application supervisor. I’ve since updated it to use a ConsumerSupervisor to add/remove Consumers as required.
The issue I’m having is that every task added to the producer gets picked up by the ProducerConsumer and passed on, but about half of them seem to disappear into thin air (ie I only see the output of IO.inspect("#{job_id} is starting") for about half of them)!
Does anyone know why this is happening or how I can debug this further?
Thanks
Code
producer.ex
defmodule Q.Producer do
use GenStage
require Logger
def start_link(_init_args) do
GenStage.start_link(__MODULE__, {:queue.new(), 0}, name: __MODULE__)
end
@impl true
def init(initial) do
{:producer, initial}
end
@impl true
def handle_demand(demand, {backlog, existing_demand}) do
# IO.inspect("Received demand: #{demand}, existing_demand: #{existing_demand}")
case :queue.len(backlog) do
0 ->
{:noreply, [], {backlog, existing_demand + demand}}
n ->
n = min(demand, n)
{items, backlog} = :queue.split(n, backlog)
:queue.len(backlog) |> Q.Stats.set_waiting()
{:noreply, :queue.to_list(items), {backlog, existing_demand + demand - n}}
end
end
@impl true
def handle_cast({:enqueue, item}, {backlog, 0}) do
{:noreply, [], {:queue.in(item, backlog), 0}}
end
@impl true
def handle_cast({:enqueue, item}, {backlog, existing_demand}) do
IO.inspect("Received item: existing_demand: #{existing_demand}")
backlog = :queue.in(item, backlog)
{{:value, item}, backlog} = :queue.out(backlog)
{:noreply, [item], {backlog, existing_demand - 1}}
end
def enqueue(item), do: GenStage.cast(__MODULE__, {:enqueue, item})
end
producer_consumer.ex
defmodule Q.ProducerConsumer do
use GenStage
def start_link(_init_args) do
GenStage.start_link(__MODULE__, :ok, name: __MODULE__)
end
def init(initial) do
{:producer_consumer, initial, subscribe_to: [{Q.Producer, max_demand: 1}]}
end
def handle_events(events, _from, state) do
# producer "middleware" - do things like filter before passing on to consumer
IO.inspect("passing events to consumer: #{inspect(events)}")
{:noreply, events, state}
end
end
consumer_supervisor.ex
defmodule Q.ConsumerSupervisor do
use ConsumerSupervisor
def start_link(_args) do
{:ok, pid} = ConsumerSupervisor.start_link(__MODULE__, :ok, name: __MODULE__)
{:ok, pid}
end
def init(:ok) do
children = [
%{
id: Q.Consumer,
start: {Q.Consumer, :start_link, []},
restart: :transient
}
]
ConsumerSupervisor.init(children,
strategy: :one_for_one,
subscribe_to: [
{Q.ProducerConsumer, max_demand: 5}
]
)
end
end
consumer.ex
defmodule Q.Consumer do
alias Q.JobRecord
use GenStage
import Q.Constants
@max_job_duration max_job_duration()
def start_link(_init_args) do
GenStage.start_link(__MODULE__, :ok)
end
def init(initial) do
Process.flag(:trap_exit, true)
Q.Stats.increment_consumer_count()
{:consumer, initial, subscribe_to: [{Q.ProducerConsumer, max_demand: 1}]}
end
def handle_events(events, _from, state) do
Enum.each(events, fn job_id ->
task =
Task.async(fn ->
IO.inspect("#{job_id} is starting")
JobRecord.set_started(job_id)
run_job()
end)
case Task.yield(task, @max_job_duration) || Task.shutdown(task) do
{:ok, _result} ->
JobRecord.set_completed(job_id)
nil ->
JobRecord.retry_job(job_id)
end
end)
# As a consumer we never emit events
{:noreply, [], state}
end
def handle_info({:EXIT, _pid, reason}, state) do
Q.Stats.decrement_consumer_count()
{:stop, reason, state}
end
defp run_job do
Process.sleep(100)
end
end
Marked As Solved
mmmmillar
My problem was that I was still using GenStage for the “consumer” when in fact I just needed to start a task (as the consumer supervisor was now picking up the events)
defmodule Q.Consumer do
def start_link(job_id) do
Task.start_link(fn ->
# do stuff
end)
end
end
1
Popular in Questions
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
Hello,
I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these
buyer = %{
id: ...
New
I would like to know what is the best IDE for elixir development?
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
Sometimes I want to check if the input into a function is not a blank string.
My first approach:
defmodule Example do
def do_stuff(s...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Other popular topics
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
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
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
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
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







