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.

Last Post!

vrod

vrod

Interesting, I will remember this for the future. In my case, I do want to kill all children processes, so I think it’s ok to stop.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement