From 8483dd4e16c54af5c949306342845dd154f3aa9e Mon Sep 17 00:00:00 2001 From: Abat Shakenov Date: Sun, 13 Jun 2021 11:47:22 +0600 Subject: [PATCH 1/2] Add port monitoring --- lib/statix.ex | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/statix.ex b/lib/statix.ex index 03a7bc2..4db76f0 100644 --- a/lib/statix.ex +++ b/lib/statix.ex @@ -306,6 +306,10 @@ defmodule Statix do end quote location: :keep do + use GenServer + + require Logger + @behaviour Statix unquote(current_statix) @@ -342,6 +346,21 @@ defmodule Statix do Statix.transmit(current_statix(), :set, key, val, options) end + def init(_init_arg) do + Process.flag(:trap_exit, true) + connect() + {:ok, []} + end + + def start_link(opts) do + GenServer.start_link(__MODULE__, :ok, opts) + end + + def handle_info({:EXIT, port, reason}, %Statix.Conn{sock: __MODULE__} = state) do + Logger.error("Port #{inspect(port)} exited with reason #{reason}") + {:stop, :normal, state} + end + defoverridable( increment: 3, decrement: 3, From a23327c76c9853e621f7872be6456388d932139a Mon Sep 17 00:00:00 2001 From: Abat Shakenov Date: Sun, 13 Jun 2021 14:00:05 +0600 Subject: [PATCH 2/2] Update docs to account for code changes --- lib/statix.ex | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/statix.ex b/lib/statix.ex index 4db76f0..1d67395 100644 --- a/lib/statix.ex +++ b/lib/statix.ex @@ -10,16 +10,19 @@ defmodule Statix do end This will make `MyApp.Statix` a Statix connection that implements the `Statix` - behaviour. This connection can be started with the `MyApp.Statix.connect/1` - function (see the `c:connect/1` callback) and a few functions can be called on - it to report metrics to the StatsD-compatible server read from the - configuration. Usually, `connect/1` is called in your application's - `c:Application.start/2` callback: + behaviour. A few functions can be called on it to report metrics to the + StatsD-compatible server read from the configuration. The connection can be + started by including `MyApp.Statix` in your application's `c:Application.start/2` + callback's list of supervised children: def start(_type, _args) do - :ok = MyApp.Statix.connect() + children = [ + # ... + MyApp.Statix + ] # ... + Supervisor.start_link(children, strategy: :one_for_one) end ## Configuration