| 1 |
|
defmodule Elita.Boot.Await do |
| 2 |
|
import Elita.Boot.Announce, only: [notify: 2] |
| 3 |
|
import Utils.Normalize, only: [name: 1] |
| 4 |
|
|
| 5 |
:-( |
def handle({:ok, pid}, n), do: pin(pid, n) |
| 6 |
:-( |
def handle({:error, {:already_started, pid}}, n), do: pin(pid, n) |
| 7 |
:-( |
def handle({:error, {:shutdown, wrapped}}, n), do: unwrap(wrapped, n) |
| 8 |
|
|
| 9 |
|
def handle({:error, :duplicate}, n) do |
| 10 |
:-( |
:global.whereis_name({name(n), :puppet}) |> bond(n) |
| 11 |
|
end |
| 12 |
|
|
| 13 |
:-( |
def handle(other, _n), do: other |
| 14 |
|
|
| 15 |
:-( |
defp pin(pid, n) do |
| 16 |
:-( |
notify(n, pid) |
| 17 |
|
{:ok, pid} |
| 18 |
|
end |
| 19 |
|
|
| 20 |
:-( |
defp bond(:undefined, _n), do: {:error, :attach_failed} |
| 21 |
|
|
| 22 |
:-( |
defp bond(p, n) do |
| 23 |
:-( |
notify(n, p) |
| 24 |
|
{:ok, p} |
| 25 |
|
end |
| 26 |
|
|
| 27 |
|
defp unwrap({:failed_to_start_child, :session, :duplicate}, n), |
| 28 |
:-( |
do: handle({:error, :duplicate}, n) |
| 29 |
|
|
| 30 |
|
defp unwrap({:failed_to_start_child, :session, {:already_started, pid}}, n), |
| 31 |
:-( |
do: handle({:error, {:already_started, pid}}, n) |
| 32 |
|
|
| 33 |
:-( |
defp unwrap(other, _n), do: {:error, {:shutdown, other}} |
| 34 |
|
end |