What’s the best way to add 3rd party modules in your supervisor tree?
For example, I’m using the following code which successfully starts a supervised mariaex process that I can send queries to, but I can’t name it because the start link implementation in mariaex doesn’t take a name argument.
defmodule ElixirMysql.Application do
use Application
def start(_type, _args) do
import Supervisor.Spec, warn: false
config = Application.get_env(:elixir_mysql, :db)
children = [
worker(Mariaex, [config])
]
opts = [strategy: :one_for_one, name: ElixirMysql.Supervisor]
Supervisor.start_link(children, opts)
end
end
Is there a way to name processes in situations like this? I thought about wrapping it around a custom genserver, but I don’t know if that’s even possible, I couldn’t find any examples.
I just skimmed over the sources, and it seems as if MariaEx uses :dbconnect to manage itselfs lifecycle. So it is probably already supervised. If not just add it. You can use tools like erlangs :observer to see if there is a supervision tree spun up. Of course you can supervise the supervisor. It won’t need a name though, and I’m not sure how you could grab that supervisir into yours, since it will be probably started in the application start up of :mariaex (or even earlier by one of its dependencies).
Also, you are not meant to send messages directly to any of :mariaexs processes, so you don’t need to know their names. Just use the exposed functions of the application. This is true regardless if a process is supervised or not. If you need to send directly to a process of a dependency, then there is probably an issue with the design of the dependency.
Last but not least, a minor nitpick about your wording: You don’t add “modules” into a supervision tree, but “processes”.
iex(2)> Mariaex.query(p, “CREATE TABLE test1 (id serial, title text)”)
{:ok, %Mariaex.Result{columns: [], command: :create, num_rows: 0, rows: []}}
I’m just trying to figure out how I can start a mariaex process and keep track of it’s pid so I can use it throughout my Application.
Maybe trying to supervise it was the wrong way to go about it, but it seems I still need to keep track of it’s pid somehow, and I’m not able to name it,because the Mariaex.start_link/1 doesn’t take a process name option.