Packages
baby
0.25.2
0.35.1
0.35.0
0.34.0
0.33.0
0.32.0
0.31.0
0.30.1
0.30.0
0.26.0
0.25.2
0.25.1
0.25.0
0.23.0
0.22.0
0.21.1
0.21.0
0.20.0
0.19.0
0.18.0
0.17.0
0.16.4
0.16.3
0.16.1
0.16.0
0.15.1
0.15.0
0.14.1
0.14.0
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.4
0.11.3
0.11.2
0.11.1
0.11.0
0.10.0
0.9.7
0.9.6
0.9.5
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.0
0.7.0
0.6.0
Bushbaby Automated Bamboo Yields
Current section
Files
Jump to
Current section
Files
lib/baby/monitor.ex
defmodule Baby.Monitor do
use GenServer
alias Baby.Util
require Logger
@moduledoc """
Cutely and unhelpfully named.
A GenServer managing periodic `cryout` activity
"""
def start_link(opts) when is_map(opts) do
children = [
{DynamicSupervisor, strategy: :one_for_one, name: Baby.Monitor.DynamicSupervisor}
]
Supervisor.start_link(children, strategy: :one_for_one)
GenServer.start_link(__MODULE__, opts)
end
@impl true
def init(%{cryouts: cryouts} = state) do
for peer <- cryouts do
Process.send_after(self(), {:cryout, peer}, :rand.uniform(3000), [])
end
{:ok, state}
end
@impl true
def handle_info({:cryout, opts}, %{identity: id, clump_id: clump} = state) do
host = Keyword.get(opts, :host)
Logger.info(["Crying out to ", host])
DynamicSupervisor.start_child(
Baby.Monitor.DynamicSupervisor,
{Baby.Connection,
[
host: Util.host_to_ip(host),
port: Keyword.get(opts, :port),
identity: id,
clump_id: clump
]}
)
# We get inherent jitter via the connection spin up
next_start = Util.period_to_ms(Keyword.get(opts, :period, {17, :minute}))
Process.send_after(self(), {:cryout, opts}, next_start, [])
{:noreply, state}
end
end