Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to access the state of a GenServer?

Is there a built in way to see the current state of a GenServer? You can always implement a simple call to return the state, but is there a more generic way?

When I run :observer.start I can look at the running applications and see their state, so it seems possible. But it may be doing some double-secret Erlang voodoo to get that.

like image 977
Matt Avatar asked Dec 12 '25 08:12

Matt


1 Answers

It depends on how you're going to use it.

If obtaining the state is part of your business logic, then yes - this should be properly modelled in your application with GenServer.call to return the state.

If you need this only in terms of debugging/insights of your application, you could use :sys.get_state like this:

iex> :sys.get_state(pid)
# some state printed

Please be aware of that :sys.get_state will accept either pid or the name of the server (if it is registered under one).

Also, please take a look at this section about debugging with :sys module in Elixir's GenServer documentation as it is very useful.

Hope that helps!

like image 200
Paweł Dawczak Avatar answered Dec 14 '25 09:12

Paweł Dawczak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!