ThousandIsland's equivalent of `:ranch.procs/2`?

I’m trying to shut down my phoenix app when Idle as explained in this blog post, but I want to use ThousandIsland / Bandit.

How can I adapt this snippet using ThousandIsland?

 if :ranch.procs(AppWeb.Endpoint.HTTP, :connections) == [] do
      System.stop(0)
1 Like

Looks like it’s ThousandIsland.connection_pids/1

I tried that, but got into these errors:

ThousandIsland.connection_pids(self())

[error] Task #PID<0.908.0> started from Timetask.Supervisor terminating
** (stop) exited in: GenServer.call(#PID<0.908.0>, :which_children, :infinity)
    ** (EXIT) process attempted to call itself
    (elixir 1.14.2) lib/gen_server.ex:1031: GenServer.call/3
    (thousand_island 0.6.4) lib/thousand_island/server.ex:22: ThousandIsland.Server.acceptor_pool_supervisor_pid/1
    (thousand_island 0.6.4) lib/thousand_island.ex:183: ThousandIsland.connection_pids/1
    (elixir 1.14.2) lib/task/supervised.ex:89: Task.Supervised.invoke_mfa/2
    (stdlib 4.0) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
Function: #Function<0.17788813/0 in Timetask.Application.start/2>
    Args: []

ThousandIsland.listener_info(self())

[error] Task #PID<0.918.0> started from Timetask.Supervisor terminating
** (stop) exited in: GenServer.call(#PID<0.918.0>, :which_children, :infinity)
    ** (EXIT) process attempted to call itself
    (elixir 1.14.2) lib/gen_server.ex:1031: GenServer.call/3
    (thousand_island 0.6.4) lib/thousand_island/server.ex:13: ThousandIsland.Server.listener_pid/1
    (thousand_island 0.6.4) lib/thousand_island.ex:169: ThousandIsland.listener_info/1
    (timetask 0.7.0) lib/config/application.ex:45: Timetask.Application.shutdown_when_inactive/1
    (elixir 1.14.2) lib/task/supervised.ex:89: Task.Supervised.invoke_mfa/2
    (stdlib 4.0) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
Function: #Function<0.26013251/0 in Timetask.Application.start/2>
    Args: []

I’m completely out of my depth here.

I think you have to use the following

endpoint_pid = Process.whereis(AppWeb.Endpoint)
ThousandIsland.connection_pids(endpoint_pid)

That didn’t help either. I found another post with a solution thou :smile:

1 Like

Bandit servers are just Thousand Island servers under the hood, and the latter provides a first class way to shut an instance down via the ThousandIsland.stop/2 function. See the docs for details.

As to the ‘process attempted to call itself’ error, the connection_pids/1 and listener_info/1 functions both expect to be handed the PID of a running Thousand Island server (ie: you would use them like so:

{:ok, server_pid} = ThousandIsland.start_link(opts)

# You can store `server_pid` away as you'd like, and call this at any time that the server is still running....
ThousandIsland.listener_info(server_pid) |> IO.inspect()
1 Like