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

itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews