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.
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
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.
I have come across similar need.
I am developing a library/tool that generates elixir code for calling stored procedures from postgres. Its a database-first approach. The code I am generating right now (and this is how I tested it - successfuly) uses single PID to use API of the
Postgrex module (
Postgrex.query(the_pid, "...", )). This PID should have a pool of connections to the database (so actual load-balancing is hidden in
Postgrex module). Now I am trying to integrate this library into existing project that has traditional
MyApp.Repo started under supervisor. And I dont know how to obtain the single PID that manages the connection pool.
From what I know so far, is that
Posgrex driver internally to communicate with postgres db.
So far I managed to discover
after_connect: MFA callback that is fired (from my observations) for each worker (10 times exactly) in the specified
pool_size: 10 repo’s config instead of the single PID that manages the connection pool.
Is there something I am missing or can you please point me to the right resource/docs?
Thanks in advance
So for the reference. I managed to get my code working… I was using
Postgrex.query that required as a 1st argument the PID of
Postgrex process. But I realized that there is also
query/2 function exposed in application’s repo that does not require any PID… so the code now uses repo started by host application and does not need to have redundant db connections opened with
Postgrex as I was afraid of…