thomas.fortes

thomas.fortes

Best way to get a pid for a supervisors child

I’m deploying an app at fly.io that can shut down when idle, this post from Chris McCord shows how to do it, but in the app I’m using bandit and thousand island, not cowboy and ranch, so I asked @mtrudel if thousand island supported introspection and he implemented a way to get the connection pids from a server, which was awesomely nice of him.

Anyway, my solution uses the exact same structure as the Chris McCord one but replacing the use of ranch with Bandit and Thousand Island, the problem is that I don’t see anything wrong and it works perfectly, but maybe some of you know a more elegant way to fetch the pid needed, here’s the code:

 defp shutdown_when_inactive(every_ms) do
    Process.sleep(every_ms)

    pid =
      Supervisor.which_children(AppWeb.Endpoint)
      |> Enum.find(fn c ->
        case c do
          {{AppWeb.Endpoint, :http}, _pid, :worker, [Bandit]} -> true
          _ -> false
        end
      end)
      |> elem(1)

    {:ok, connections} = ThousandIsland.connection_pids(pid)

    if connections == [] do
      System.stop(0)
    else
      shutdown_when_inactive(every_ms)
    end
  end

Thanks

Most Liked

mtrudel

mtrudel

Creator of Bandit

Great to hear you got this working!

As you’ve figured out, once you get a handle on the root Bandit process (which is actually just a Thousand Island server process), you can get the list of current connection processes for it via the ThousandIsland.connection_pids/1 function added in Thousand Island 0.5.17. That part is pretty straightforward.

This leaves the problem of how to get the Bandit/Thousand Island pid from an Endpoint process (accessible via id as the Endpoint’s module name). Pulling out specific children of a supervisor (by id or otherwise) is always as awkward as you’ve done here; there’s not really an easier way. There’s a million ways to golf this sort of thing down to something more elegant but I’ve never really seen an approach I like; curious if anyone else has a better approach. FWIW I use a pattern more or less like you do here within Thousand Island.

fceruti

fceruti

Cool, thanks!

For future reference, here’s the snippet using everything I learned today :stuck_out_tongue:

 defp shutdown_when_inactive(every_ms) do
    Process.sleep(every_ms)

    {_, pid, _, _} =
      AppWeb.Endpoint
      |> Supervisor.which_children()
      |> Enum.find(&Kernel.match?({{AppWeb.Endpoint, :http}, _pid, _type, [Bandit]}, &1))

    {:ok, connections} = ThousandIsland.connection_pids(pid)

    if connections == [] do
      System.stop(0)
    else
      shutdown_when_inactive(every_ms)
    end
  end
thomas.fortes

thomas.fortes

Yeah, your approach is more elegant than mine, similar idea, cleaner execution, still wondering if someone has a different approach.

Thanks again

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement