Skip to content

Commit

Permalink
Register global name in composite
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmi committed Jun 9, 2024
1 parent b5eec9b commit 146e31a
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/composite.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ defmodule Strom.Composite do
end

def start_link(%__MODULE__{name: name} = composite) do
GenServer.start_link(__MODULE__, composite, name: name)
GenServer.start_link(__MODULE__, composite, name: {:global, name})
end

@impl true
Expand All @@ -83,13 +83,15 @@ defmodule Strom.Composite do
end

@spec call(Strom.flow(), __MODULE__.t() | atom()) :: Strom.flow()
def call(flow, %__MODULE__{name: name}), do: GenServer.call(name, {:call, flow}, :infinity)
def call(flow, %__MODULE__{name: name}),
do: GenServer.call({:global, name}, {:call, flow}, :infinity)

def call(flow, name) when is_atom(name), do: GenServer.call(name, {:call, flow}, :infinity)
def call(flow, name) when is_atom(name),
do: GenServer.call({:global, name}, {:call, flow}, :infinity)

@spec stop(__MODULE__.t()) :: :ok
def stop(%__MODULE__{name: name, components: components}) do
pid = Process.whereis(name)
pid = :global.whereis_name(name)
Process.unlink(pid)
stop_components(components)
DynamicSupervisor.terminate_child(Strom.DynamicSupervisor, pid)
Expand Down Expand Up @@ -131,7 +133,7 @@ defmodule Strom.Composite do
|> Enum.map(&String.at(&1, 13))
|> Enum.join("")
|> String.slice(0..15)
|> then(&(&1 <> "_" <> pid_postfix()))
|> then(&(&1 <> "_" <> timestamp_postfix()))
|> String.to_atom()
end

Expand All @@ -149,7 +151,9 @@ defmodule Strom.Composite do
end)
end

defp pid_postfix do
to_string(:erlang.pid_to_list(self()))
defp timestamp_postfix do
:erlang.system_time()
|> rem(round(1.0e9))
|> to_string()
end
end

0 comments on commit 146e31a

Please sign in to comment.