Skip to content

Commit

Permalink
The child_spec() function should not contain any logic (#2191)
Browse files Browse the repository at this point in the history
By massaging options inside child_spec's definition we make the module
behave differently when its start_link() function is called directly
(during manual or automated testing) compared to when it's started by a
supervisor.
  • Loading branch information
alco authored Dec 24, 2024
1 parent b64c900 commit 1665bca
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions packages/sync-service/lib/electric/process_registry.ex
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
defmodule Electric.ProcessRegistry do
@spec child_spec([Registry.start_option()]) :: Supervisor.child_spec()
def child_spec(options) do
options =
options
|> Keyword.put_new(:name, registry_name(Keyword.fetch!(options, :stack_id)))
|> Keyword.put(:keys, :unique)

%{
id: Keyword.get(options, :name, Registry),
id: __MODULE__,
start: {__MODULE__, :start_link, [options]},
type: :supervisor
}
end

def start_link(opts), do: Registry.start_link(opts)
def start_link(opts) do
opts =
opts
|> Keyword.put_new(:name, registry_name(Keyword.fetch!(opts, :stack_id)))
|> Keyword.put(:keys, :unique)

Registry.start_link(opts)
end

def registry_name(stack_id) when is_atom(stack_id) or is_binary(stack_id),
do: :"#{__MODULE__}:#{stack_id}"
Expand Down

0 comments on commit 1665bca

Please sign in to comment.