defmodule Sandbox do
use GenServer
def start_link() do
GenServer.start_link(__MODULE__, [], name: __MODULE__)
end
@impl true
def init(_) do
:observer.start
main()
{:ok, :running}
end
def main do
IO.puts("Hello World!")
end
This is part of the application Foo
. 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.
However, if main
is changed as follows-
def main do
IO.puts("Hello World!")
main()
end
: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 main
running perpetually even after its restarted by the Supervisor. What’s the best way to accomplish this?
Edit: use GenServer
added