Skip to content

Commit

Permalink
use libcluster_postgres for node discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
joshk committed Jan 3, 2024
1 parent e3c3491 commit a6ffb64
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
23 changes: 22 additions & 1 deletion config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ if config_env() == :prod do
end

##
# Database connection settings
# Database and Libcluster connection settings
#
if config_env() == :prod do
database_ssl_opts =
Expand Down Expand Up @@ -190,6 +190,27 @@ if config_env() == :prod do

config :nerves_hub,
database_auto_migrator: System.get_env("DATABASE_AUTO_MIGRATOR", "true") == "true"

# Libcluster is using Postgres for Node discovery
# The library only accepts keyword configs, so the DATABASE_URL has to be
# parsed and put together with the ssl pieces from above.
postgres_config = Ecto.Repo.Supervisor.parse_url(System.fetch_env!("DATABASE_URL"))

libcluster_db_config =
[port: 5432]
|> Keyword.merge(postgres_config)
|> Keyword.take([:hostname, :username, :password, :database, :port])
|> Keyword.merge(ssl: System.get_env("DATABASE_SSL", "true") == "true")
|> Keyword.merge(ssl_opts: database_ssl_opts)
|> Keyword.merge(parameters: [])

config :libcluster,
topologies: [
postgres: [
strategy: LibclusterPostgres.Strategy,
config: libcluster_db_config
]
]
end

##
Expand Down
4 changes: 3 additions & 1 deletion lib/nerves_hub/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ defmodule NervesHub.Application do
raise "fwup could not be found in the $PATH. This is a requirement of NervesHubWeb and cannot start otherwise"
end

topologies = Application.get_env(:libcluster, :topologies, [])

children =
[
{Registry, keys: :unique, name: NervesHub.Devices},
Expand All @@ -26,7 +28,7 @@ defmodule NervesHub.Application do
NervesHub.ObanRepo,
NervesHub.Release.DatabaseAutoMigrator,
{Phoenix.PubSub, name: NervesHub.PubSub},
{DNSCluster, query: Application.get_env(:nerves_hub, :dns_cluster_query) || :ignore},
{Cluster.Supervisor, [topologies]},
{Task.Supervisor, name: NervesHub.TaskSupervisor},
{Oban, Application.fetch_env!(:nerves_hub, Oban)},
NervesHub.Tracker
Expand Down
4 changes: 1 addition & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ defmodule NervesHub.MixProject do
{:bandit, "~> 1.0"},
{:base62, "~> 1.2"},
{:bcrypt_elixir, "~> 3.0"},
# {:dns_cluster, "~> 0.1.1"},
{:dns_cluster,
git: "https://github.com/davydog187/dns_cluster.git", branch: "support-multiple-queries"},
{:castore, "~> 1.0"},
{:circular_buffer, "~> 0.4.1"},
{:comeonin, "~> 5.3"},
Expand All @@ -74,6 +71,7 @@ defmodule NervesHub.MixProject do
{:hackney, "~> 1.16"},
{:hlclock, "~> 1.0"},
{:jason, "~> 1.2", override: true},
{:libcluster_postgres, "~> 0.1.2"},
{:logfmt, "~> 3.3"},
{:mox, "~> 1.0", only: [:test, :dev]},
{:nimble_csv, "~> 1.1"},
Expand Down
3 changes: 2 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"db_connection": {:hex, :db_connection, "2.6.0", "77d835c472b5b67fc4f29556dee74bf511bbafecdcaf98c27d27fa5918152086", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c2f992d15725e721ec7fbc1189d4ecdb8afef76648c746a8e1cad35e3b8a35f3"},
"decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"},
"decorator": {:hex, :decorator, "1.4.0", "a57ac32c823ea7e4e67f5af56412d12b33274661bb7640ec7fc882f8d23ac419", [:mix], [], "hexpm", "0a07cedd9083da875c7418dea95b78361197cf2bf3211d743f6f7ce39656597f"},
"dns_cluster": {:git, "https://github.com/davydog187/dns_cluster.git", "2535ab2a30d7d37767f40a7b6424b270e3fc9df7", [branch: "support-multiple-queries"]},
"earmark": {:hex, :earmark, "1.4.46", "8c7287bd3137e99d26ae4643e5b7ef2129a260e3dcf41f251750cb4563c8fb81", [:mix], [], "hexpm", "798d86db3d79964e759ddc0c077d5eb254968ed426399fbf5a62de2b5ff8910a"},
"ecto": {:hex, :ecto, "3.11.1", "4b4972b717e7ca83d30121b12998f5fcdc62ba0ed4f20fd390f16f3270d85c3e", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ebd3d3772cd0dfcd8d772659e41ed527c28b2a8bde4b00fe03e0463da0f1983b"},
"ecto_sql": {:hex, :ecto_sql, "3.11.1", "e9abf28ae27ef3916b43545f9578b4750956ccea444853606472089e7d169470", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.11.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16.0 or ~> 0.17.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ce14063ab3514424276e7e360108ad6c2308f6d88164a076aac8a387e1fea634"},
Expand All @@ -31,6 +30,8 @@
"html_entities": {:hex, :html_entities, "0.5.2", "9e47e70598da7de2a9ff6af8758399251db6dbb7eebe2b013f2bbd2515895c3c", [:mix], [], "hexpm", "c53ba390403485615623b9531e97696f076ed415e8d8058b1dbaa28181f4fdcc"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"libcluster": {:hex, :libcluster, "3.3.3", "a4f17721a19004cfc4467268e17cff8b1f951befe428975dd4f6f7b84d927fe0", [:mix], [{:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "7c0a2275a0bb83c07acd17dab3c3bfb4897b145106750eeccc62d302e3bdfee5"},
"libcluster_postgres": {:hex, :libcluster_postgres, "0.1.2", "51380bb6b3a6e11b2614202c63169b464cf4867814da52cc3aa82dfc5ae22248", [:mix], [{:libcluster, "~> 3.3", [hex: :libcluster, repo: "hexpm", optional: false]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, repo: "hexpm", optional: false]}], "hexpm", "b957ca609ec5dcddc37342a756bfc027029225bfb089bf0b32974f0ec0cac237"},
"logfmt": {:hex, :logfmt, "3.3.3", "6521ee4a5c532088e15d487fab9f736c07bdd161d643560c73cd4b10685deb65", [:mix], [], "hexpm", "dbd51cd3fe37c3429b9bd687bad1f531a533505f4a641592129e7a47e24104d1"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"},
Expand Down

0 comments on commit a6ffb64

Please sign in to comment.