I am wondering if it is possible to terminate a supervisor after some time. For regular GenServer job, I can do something like this if I want process to run for 30 seconds:
use GenServer
# ...
@impl true
def init(state) do
Process.send_after(self(), :stop, 30_000)
{:ok, state}
end
@impl true
def handle_info(:stop, state) do
IO.inspect(state, label: "Stopping")
{:stop, :normal, state}
end
Is it possible to make supervisor terminate like this after some time? Supervisor does not have handle_info/2. Or is it better to have a GenServer start a supervisor? Sorry I am very new to supervision trees logic. Thank you!
defmodule RunForAWhileThenStop do
use GenServer
def start_link(state) do
GenServer.start_link(__MODULE__, state, name: __MODULE__)
end
@impl true
def init(opts) do
time = Keyword.get(opts, :time, 60_000)
children = [ ... ]
Process.send_after(self(), :stop, time)
Supervisor.start_link(children, strategy: :one_for_one)
end
@impl true
def handle_info(:stop, state) do
{:stop, :normal, state}
end
This way I can something like RunForAWhileThenStop.start_link(time: 30_000) and let my GenServer run for 30 seconds and then stop.