defmodule Sandbox do
def start_link() do
GenServer.start_link(__MODULE__, , name: __MODULE__)
def init(_) do
def main do
This is part of the application
Sandbox is a supervised GenServer which executes the
main function when
Foo is started and whenever
Sandbox crashes and is restarted by the Supervisor.
:observer displays it as
Elixir.Sandbox under its Applications tab.
main is changed as follows-
def main do
:observer no longer shows the app nor the
Elixir.Sandbox GenServer process which seems to indicate that either the Supervisor or the GenServer didn’t start. (Please correct me if this is not actually the case.)
The goal: To keep
mainrunning perpetually even after its restarted by the Supervisor. What’s the best way to accomplish this?
use GenServer added
Learning about processes is the heart of the BEAM. It is also why the BEAM is attractive.
But it is hard and I advise You to watch those free lessons here before writing your own gen_server.
In particular, classes 2 and 3. Because class 1 is more about FP.
Your code needs corrections…
- You did not put use GenServer
- You should not start the observer from a server, but from the console
- It is not the way You make a recurrent call to a function, You have no control over the loop
- Instead, You can send a message to yourself with Process.send_after
You should learn about processes, and You will understand their power.
If I mention those classes, it’s because they had great impact on my understanding.
Thanks for pointing out that oversight. OP has been corrected
Thank you for sharing that
Thanks for the resource, I really appreciate it
A GenServer isn’t considered “started” until the
init function returns - see
GenServer.init/1 for more detail. Making
main call itself means that
init never returns.