I’m working through a very simple tutorial of sorts and wanted to ask for a little guidance on my next step (related to process registration).
Right now I have a working app with 2 GenServers, 1 Supervisor and the Registry. Here is my Application module
defmodule EX.Application do
use Application
def start(_type, _args) do
EX.Supervisor.start_link(name: EX.Supervisor)
end
end
Here is the Supervisor
defmodule EX.Supervisor do
use Supervisor
def start_link(opts) do
Supervisor.start_link(__MODULE__, :ok, opts)
end
def init(:ok) do
children = [
{Registry, keys: :unique, name: EX.Registry},
EX.Cache,
EX.Worker
]
Supervisor.init(children, strategy: :one_for_one)
end
end
The exercise for learning sake today is this … As my worker init runs I’d like to fetch the cache state. The tricky part that landed me here asking a question is “how do I ask for the cache (by name?) and allow the Registry lookup to do it’s thing?” note: I’m trying not to do an explicit start_link here in my worker because I want to understand better how I would interop w/ a process by name
Here is my init (worker)
defmodule EX.Worker do
use GenServer
def start_link(name) do
GenServer.start_link(__MODULE__, :ok, name: via(name))
end
defp via(name), do: EX.Registry.worker(name)
@impl GenServer
def init(:ok) do
{:ok, %{}, {:continue, :init}}
end
@impl GenServer
def handle_continue(:init, _state) do
# cached_state = ?
# also, how should I reply from this to continue properly ?
{:noreply, new_state, 2000}
end
end
Here is my Cache (note: I realize this example is of no actual value architecturally speaking)
defmodule EX.Cache do
use GenServer
def start_link(name) do
GenServer.start_link(__MODULE__, :ok, name: via(name))
end
defp via(name), do: EX.Registry.cache(name)
@impl GenServer
def init(:ok) do
{:ok, %{}}
end
end
Here is my simple Registry for reference
defmodule EX.Registry do
@type t :: {:via, Registry, tuple}
def worker(name) do
via({name, :worker})
end
def cache(name) do
via({name, :cache})
end
defp via(data) do
{:via, Registry, {__MODULE__, data}}
end
end
I’ve had (some) luck in the past hard coding a pid and doing the Registry lookup by that value but I’d like to leverage the power of “lookups by name” if that makes sense. Allowing the Registry (and runtime) to handle the implementation deals of that specific process PID. My next iteration that follows (after I grok more deeply how this should work) will be to supervise the EX.Cache module and allow that Supervisor to restart Cache workers (so the PID will change very much change /etc)
Note: I’m using Elixir 1.7.3 and OTP 21 on macOS High Sierra (with no plans to deploy this right now).
Thanks to everyone who drops by to read this - I’m very much in the soak it up/ learning phase and I’ll do my best to blog whatever I learn here to pay it forward so far I’ve got 26 posts up since Sept 24th with plans to keep rolling as I refine my understanding of the ecosystem, language, etc.