Reliable way to get the PID of a Ecto repo

Is there a reliable way to get the PID of a repo when you don’t know the process name or there isn’t one?

Please say more about the use-case, but my intuition is that in the presence of a connection pool you never want to care about any one process in particular, preferring instead to use the Repo’s existing function API to do what’s necessary.

2 Likes

For an analogy that might sound familiar to some, this goal is (potentially) like constantly going to one individual coworker with your questions when an entire team or department happens to be qualified to answer them equally well. You’ll spend more time waiting for that individual to have the availability to assist than you would if you asked the appropriate group collectively. If that individual goes on vacation or falls ill, you might not be aware but feel upset about the lengthy wait you now have for the answer to your question, and could fall behind on your own work when a better outcome was possible.

A repo (e.g. MyApp.Repo) is a process that you can start/add to your supervision tree. So it should have a PID. If you Process.whereis(MyApp.Repo) you will get a PID if the process is started with MyApp.Repo as the process name. I’d like to know if there’s a way to grab that PID in the event you don’t know the process name or it was started as an anonymous process.

If it’s already running under a supervisor known to you, you can use Supervisor.which_children/1 by finding the child with the module which starts the process.

For an example look for MyApp.Application.find_server/0 in:

1 Like

Do we have a case of XY Problem here?

What’s your desired outcome? You shouldn’t poke in the guts of your libraries’ runtime state. They have public function API for a reason.

2 Likes