vrod

vrod

I am wondering for advice on how to monitor an application with many genservers doing long-running work. Often we want to know “how many messages are in the queue?” The process has a mailbox not a queue but this is the same idea : how can we see what our app is doing? I have seen GenStage.estimate_buffered_count/2 and I am wondering if that is the best way. There is a mix of genserver and genstage, so I thought maybe Process.info/1 could help with genservers, but I noticed that this function seems to require a pid and it will not work with a process name. Also I do not see the messages in the process info? Only message_queue_len?
Another idea was because we are using pubsub, maybe we could add subscribers that would count messages but that feels maybe smelly.

Thank you for suggestions!

Showing Posts 1 to 10

RudManusachi

RudManusachi

To find a pid by process name we could check Process.whereis(:name)

And now we could see the messages via Process.info(pid, :messages), but only be careful, as this operation copies all messages to the process that called. So if mailbox is huge - the operation might be expensive.

mpope

mpope

A way to get all ‘monitor-able’ processes is to register them in a process group under the :pg module at startup. Then you’ll have a list of pids to call Process.info/1 on.

:pg.join(:message_length_procs, self())

lud

lud

If you need to do that kind of stuff a general patter is to do the actual work in a side process of the gen server (a Task process managed by the gen server), while the gen server just accept messages and put them in a queue (with the :queue module). So you can do whaterver you want with the queue : inspect it, filter it, or use another queue with priorization, etc.

rvirding

rvirding

Creator of Erlang

A simple way of of finding the length of the message queue is Process.info(pid, :message_queue_len) which avoids copying the whole message queue. Only having :message)_queue_len in the default info is to NOT copy all the messages unless you explicitly request it.

12
Post #4
vrod

vrod OP

Thank you! These are helpful suggestions. I think I can make this work with Process.info(pid, :message_queue_len) – I will look at the :pg module and :queue module to help make a better solution

vrod

vrod OP

Now that I am trying to make this work a bit more, I am not finding success. I have tried to simplify this by starting a genserver and sending messages to it using send/2, but I never see messages and message_queue_len is always zero.

RudManusachi

RudManusachi

Could you please show the code you try?

Here is the simple example in iex shell:

iex(1)> Process.info(self, :message_queue_len)
{:message_queue_len, 0}
iex(2)> send(self, :hello)
:hello
iex(3)> Process.info(self, :message_queue_len)
{:message_queue_len, 1}
iex(4)> Process.info(self, :messages)
{:messages, [:hello]}
vrod

vrod OP

Using self() works – I tried something a little more complicated where I named a GenServer like this:

defmodule Genfoo.Application do
  @moduledoc false

  use Application

  @impl true
  def start(_type, _args) do
    children = [
      Supervisor.child_spec(
        {Genfoo.Thing, %{name: :thing1}},
        id: :thing1
      )
    ]

    opts = [strategy: :one_for_one, name: Genfoo.Supervisor]
    Supervisor.start_link(children, opts)
  end
end


defmodule Genfoo.Thing do
  @moduledoc false
  use GenServer

  def start_link(%{name: name} = state) do
    GenServer.start_link(__MODULE__, state, name: name)
  end

  @impl true
  def init(state) do
    {:ok, state}
  end

  @impl true
  def handle_info(_msg, state) do
    IO.puts("handle_info")
    {:noreply, state}
  end

  @impl true
  def handle_call(_msg, _from, state) do
    IO.puts("handle_call")
    {:reply, "Called", state}
  end

  @impl true
  def handle_cast(_msg, state) do
    IO.puts("handle_cast")
    {:noreply, state}
  end
end

Then I tried this:

pid = Process.whereis(:thing1)
send(pid, "xx")
:thing1 |> Process.whereis() |> Process.info(:messages)

Sorry I must be doing something wrong. I expected to be able to inspect the named genserver process.

RudManusachi

RudManusachi

As soon as your process successfully fetches the message from mailbox (which it does in your handle_info) - it’s not there anymore. As a result your mailbox gets emptied out pretty fast =)


to continue showing in the simple example in iex

iex(1)> Process.info(self, :message_queue_len)
{:message_queue_len, 0}
iex(2)> send(self, :hello)
:hello
iex(3)> Process.info(self, :message_queue_len)
{:message_queue_len, 1}
iex(4)> Process.info(self, :messages)
{:messages, [:hello]}

# fetch the first message from the message_queue
iex(5)> receive do
...(5)>   any_message -> any_message
...(5)> end
:hello

# now message_queue is empty
iex(6)> Process.info(self, :message_queue_len)
{:message_queue_len, 0}
iex(7)> Process.info(self, :messages)
{:messages, []}
vrod

vrod OP

I see. I guess I am not fully understanding how handle_info processes a message.
I added Process.sleep(5000) to handle_info and I think I understand how this works now.

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
widianto
I think I’ve found a small improvement I could contribute to <%= web_namespace %>.CoreComponents (installer/templates/phx_web/compo...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews