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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With