-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API change: LocalCluster.start_nodes/3 no longer returns node names. See LocalCluster.nodes/1.
- Loading branch information
1 parent
9fc14f5
commit 3e4218b
Showing
4 changed files
with
116 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
defmodule LocalCluster.Peer do | ||
@moduledoc """ | ||
Contains metadata about a peer that has been started with `LocalCluster.start_nodes/2` | ||
or `LocalCluster.start_nodes/3`. | ||
""" | ||
import Kernel, except: [node: 1] | ||
|
||
@type t :: %__MODULE__{} | ||
|
||
defstruct [:node, :pid] | ||
|
||
@doc """ | ||
Given a list of `LocalCluster.Peer` structs, returns the | ||
node names. | ||
The node names can be used for `:rpc` calls. | ||
""" | ||
def nodes(peers) when is_list(peers) do | ||
Enum.map(peers, &node/1) | ||
end | ||
|
||
@doc """ | ||
Given a `LocalCluster.Peer`, returns the node name. | ||
The node name can be used for `:rpc` calls. | ||
""" | ||
def node(%__MODULE__{node: node}) do | ||
node | ||
end | ||
|
||
def start_link(prefix, idx) do | ||
args = | ||
~w[-loader inet -hosts 127.0.0.1 -setcookie #{:erlang.get_cookie()}] | ||
|> Enum.map(&String.to_charlist/1) | ||
|
||
{:ok, pid, node} = | ||
start_link_int(%{ | ||
host: ~c"127.0.0.1", | ||
name: :"#{prefix}#{idx}", | ||
args: args | ||
}) | ||
|
||
{:ok, %__MODULE__{node: node, pid: pid}} | ||
end | ||
|
||
def stop(%__MODULE__{pid: pid, node: node}) do | ||
stop_int(pid, node) | ||
end | ||
|
||
if Code.ensure_loaded?(:peer) and function_exported?(:peer, :start_link, 1) do | ||
def start_link_int(opts), do: :peer.start_link(opts) | ||
def stop_int(pid, _node), do: :peer.stop(pid) | ||
else | ||
# Support for OTP < 25 | ||
def start_link_int(%{host: host, name: name, args: args}) do | ||
case :slave.start_link(host, name, :string.join(args, ~c" ")) do | ||
{:ok, node} -> | ||
{:ok, nil, node} | ||
|
||
error -> | ||
error | ||
end | ||
end | ||
|
||
def stop_int(_pid, node), do: :slave.stop(node) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters