vrod

vrod

Terminate supervisor after some time

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!

Most Liked

lud

lud

You need too call Supervisor.stop from another process to stop the supervisor in a clean way. Now, what are you trying to do exactly?

vrod

vrod

Here’s what I came up with:

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.

kokolegorille

kokolegorille

You should have a look at GenServer timeout.

It allows You to stop the server after a certain inactivity period.

Also doing this on a supervisor will kill all children, unless they trap exit.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

We're in Beta

About us Mission Statement