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!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
RudManusachi
To find a
pidby process name we could checkProcess.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
A way to get all ‘monitor-able’ processes is to register them in a process group under the
:pgmodule at startup. Then you’ll have a list ofpids to callProcess.info/1on.:pg.join(:message_length_procs, self())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
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_lenin the default info is to NOT copy all the messages unless you explicitly request it.vrod
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:pgmodule and:queuemodule to help make a better solutionvrod
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 andmessage_queue_lenis always zero.RudManusachi
Could you please show the code you try?
Here is the simple example in iex shell:
vrod
Using
self()works – I tried something a little more complicated where I named aGenServerlike this:Then I tried this:
Sorry I must be doing something wrong. I expected to be able to inspect the named genserver process.
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
vrod
I see. I guess I am not fully understanding how
handle_infoprocesses a message.I added
Process.sleep(5000)to handle_info and I think I understand how this works now.