Skip to content

Commit

Permalink
Postpone joining cluster until Supervisor registers itself
Browse files Browse the repository at this point in the history
  • Loading branch information
martosaur committed Jan 23, 2023
1 parent 53f073d commit f337ac6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
23 changes: 21 additions & 2 deletions lib/nebulex_local_multilevel_adapter/sidecar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,29 @@ defmodule NebulexLocalMultilevelAdapter.Sidecar do
# Trap exit signals to run cleanup job
_ = Process.flag(:trap_exit, true)

{:ok, adapter_meta, {:continue, :join_cluster}}
end

@impl true
def handle_continue(:join_cluster, adapter_meta) do
join_cluster(adapter_meta)
end

@impl true
def handle_info(:join_cluster, adapter_meta) do
join_cluster(adapter_meta)
end

def join_cluster(adapter_meta) do
_ = Nebulex.Cache.Registry.lookup(adapter_meta.name)

# Ensure joining the cluster only when the cache supervision tree is started
:ok = Cluster.join(adapter_meta.name)

{:ok, adapter_meta}
{:noreply, adapter_meta}
rescue
ArgumentError ->
Process.send_after(self(), :join_cluster, 50)
{:noreply, adapter_meta}
end

@impl true
Expand Down
6 changes: 6 additions & 0 deletions test/clustered_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ defmodule NebulexLocalMultilevelAdapter.ClusteredTest do
]}
])

wait_until(fn ->
Enum.all?([Cache.Isolated, Cache.Connected], fn cache ->
[_, _, _] = Nebulex.Cache.Cluster.get_nodes(cache)
end)
end)

on_exit(fn ->
:ok = Process.sleep(100)
stop_caches(node_pid_list)
Expand Down

0 comments on commit f337ac6

Please sign in to comment.