defmodule Aprsme.Cluster.Topology do @moduledoc """ Wrapper for Cluster.Supervisor that only starts when clustering is enabled. """ require Logger def child_spec(opts) do cluster_enabled = Application.get_env(:aprsme, :cluster_enabled, false) topologies = Application.get_env(:libcluster, :topologies, []) build_spec(cluster_enabled, topologies, opts) end # Clustering disabled — no-op spec. defp build_spec(false, _topologies, opts), do: noop_spec(opts) # Clustering enabled but no topologies configured — no-op + warning. defp build_spec(true, topologies, opts) when topologies in [nil, []] do Logger.warning("No libcluster topologies configured, clustering will not work") noop_spec(opts) end # Clustering enabled with topologies — delegate to libcluster's supervisor. defp build_spec(true, topologies, opts) do Logger.info("Cluster.Topology starting with topologies: #{inspect(topologies)}") supervisor_opts = Keyword.merge([name: Aprsme.ClusterSupervisor], opts) {Cluster.Supervisor, [topologies, supervisor_opts]} end defp noop_spec(opts) do %{ id: __MODULE__, start: {__MODULE__, :start_link, [opts]}, type: :worker } end def start_link(_opts), do: :ignore end