Skip to content

Commit 77087cc

Browse files
committed
[#30] Add bootstrap server to run init and cleanup jobs
1 parent 81e5277 commit 77087cc

File tree

2 files changed

+61
-23
lines changed

2 files changed

+61
-23
lines changed

lib/nebulex_redis_adapter.ex

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,6 @@ defmodule NebulexRedisAdapter do
304304

305305
alias Nebulex.Adapter
306306
alias Nebulex.Adapter.Stats
307-
alias Nebulex.Telemetry
308-
alias Nebulex.Telemetry.StatsHandler
309307
alias NebulexRedisAdapter.{ClientCluster, Command, Connection, RedisCluster}
310308

311309
## Nebulex.Adapter
@@ -369,14 +367,7 @@ defmodule NebulexRedisAdapter do
369367
# init stats
370368
stats_counter = Stats.init(opts)
371369

372-
child_spec =
373-
Nebulex.Adapters.Supervisor.child_spec(
374-
name: normalize_module_name([name, Supervisor]),
375-
strategy: :rest_for_one,
376-
children: children
377-
)
378-
379-
meta = %{
370+
adapter_meta = %{
380371
name: name,
381372
mode: mode,
382373
keyslot: keyslot,
@@ -389,20 +380,14 @@ defmodule NebulexRedisAdapter do
389380
telemetry_prefix: Keyword.fetch!(opts, :telemetry_prefix)
390381
}
391382

392-
_ = maybe_attach_stats_handler(meta)
393-
394-
{:ok, child_spec, meta}
395-
end
396-
397-
defp maybe_attach_stats_handler(%{stats_counter: nil}), do: :ok
383+
child_spec =
384+
Nebulex.Adapters.Supervisor.child_spec(
385+
name: normalize_module_name([name, Supervisor]),
386+
strategy: :rest_for_one,
387+
children: [{NebulexRedisAdapter.BootstrapServer, adapter_meta} | children]
388+
)
398389

399-
defp maybe_attach_stats_handler(%{stats_counter: stats_counter} = meta) do
400-
Telemetry.attach_many(
401-
stats_counter,
402-
[meta.telemetry_prefix ++ [:command, :stop]],
403-
&StatsHandler.handle_event/4,
404-
stats_counter
405-
)
390+
{:ok, child_spec, adapter_meta}
406391
end
407392

408393
defp do_init(:standalone, name, pool_size, opts) do
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
defmodule NebulexRedisAdapter.BootstrapServer do
2+
@moduledoc """
3+
This server takes care of initialization and cleanup jobs. For example,
4+
attaching the stats handler when the cache starts and detaching it when
5+
it terminates.
6+
"""
7+
use GenServer
8+
9+
import Nebulex.Helpers
10+
11+
alias Nebulex.Telemetry
12+
alias Nebulex.Telemetry.StatsHandler
13+
14+
## API
15+
16+
@spec start_link(Nebulex.Adapter.adapter_meta()) :: GenServer.on_start()
17+
def start_link(adapter_meta) do
18+
name = normalize_module_name([Map.fetch!(adapter_meta, :name), BootstrapServer])
19+
GenServer.start_link(__MODULE__, adapter_meta, name: name)
20+
end
21+
22+
## GenServer Callbacks
23+
24+
@impl true
25+
def init(adapter_meta) do
26+
_ = Process.flag(:trap_exit, true)
27+
{:ok, adapter_meta, {:continue, :attach_stats_handler}}
28+
end
29+
30+
@impl true
31+
def handle_continue(:attach_stats_handler, adapter_meta) do
32+
_ = maybe_attach_stats_handler(adapter_meta)
33+
{:noreply, adapter_meta}
34+
end
35+
36+
@impl true
37+
def terminate(_reason, adapter_meta) do
38+
if ref = adapter_meta.stats_counter, do: Telemetry.detach(ref)
39+
end
40+
41+
## Private Functions
42+
43+
defp maybe_attach_stats_handler(adapter_meta) do
44+
if ref = adapter_meta.stats_counter do
45+
Telemetry.attach_many(
46+
ref,
47+
[adapter_meta.telemetry_prefix ++ [:command, :stop]],
48+
&StatsHandler.handle_event/4,
49+
ref
50+
)
51+
end
52+
end
53+
end

0 commit comments

Comments
 (0)