Skip to content

Commit

Permalink
going back to sync genserver calls for reaper
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Oct 15, 2023
1 parent c7b3fba commit edae9ab
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 20 deletions.
6 changes: 3 additions & 3 deletions lib/container/ceph_container.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ defmodule TestcontainersElixir.Container.CephContainer do
exposed_ports: [3300, 8080],
environment: %{
CEPH_DEMO_UID: "demo",
CEPH_DEMO_BUCKET: "#{bucket}",
CEPH_DEMO_ACCESS_KEY: "#{access_key}",
CEPH_DEMO_SECRET_KEY: "#{secret_key}",
CEPH_DEMO_BUCKET: bucket,
CEPH_DEMO_ACCESS_KEY: access_key,
CEPH_DEMO_SECRET_KEY: secret_key,
CEPH_PUBLIC_NETWORK: "0.0.0.0/0",
MON_IP: "127.0.0.1",
RGW_NAME: "localhost"
Expand Down
76 changes: 59 additions & 17 deletions lib/reaper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,62 @@
# Original by: Marco Dallagiacoma @ 2023 in https://github.com/dallagi/excontainers
# Modified by: Jarl André Hübenthal @ 2023
defmodule TestcontainersElixir.Reaper do
use GenServer

alias TestcontainersElixir.Docker
alias TestcontainersElixir.Container

@ryuk_image "testcontainers/ryuk:0.5.1"
@ryuk_port 8080

def register({filter_key, filter_value}) do
with {:ok, socket} <- get_ryuk_socket() do
:gen_tcp.send(
socket,
"#{:uri_string.quote(filter_key)}=#{:uri_string.quote(filter_value)}" <> "\n"
)

case :gen_tcp.recv(socket, 0, 8_000) do
{:ok, "ACK\n"} ->
:ok

{:error, reason} ->
IO.puts("Reaper: Ignoring error acking data: #{inspect(reason)}")
:ok
end
end
def start_link() do
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
end

defp get_ryuk_socket() do
def register(filter) do
:ok = ensure_reaper_is_alive()
GenServer.call(__MODULE__, {:register, filter}, 10000)
end

@impl true
def init(_) do
with {:ok, container} <- create_ryuk_container(),
{:ok, socket} <- create_ryuk_socket(container) do
{:ok, socket}
else
error ->
{:stop, "Failed to start reaper: #{inspect(error)}"}
end
end

@impl true
def handle_call({:register, filter}, _from, socket) do
case register(socket, filter) do
:ok ->
{:reply, :ok, socket}
other ->
{:reply, {:error, "Failed to handle register: #{inspect(other)}"}, socket}
end

end

defp register(socket, {filter_key, filter_value}) do
:gen_tcp.send(
socket,
"#{:uri_string.quote(filter_key)}=#{:uri_string.quote(filter_value)}" <> "\n"
)

case :gen_tcp.recv(socket, 0, 1_000) do
{:ok, "ACK\n"} ->
:ok

{:error, :closed} ->
IO.puts("Reaper connection was closed, ignoring")
:ok

{:error, reason} ->
IO.puts("Error receiving data: #{inspect(reason)}")
{:error, reason}
end
end

Expand All @@ -51,4 +79,18 @@ defmodule TestcontainersElixir.Reaper do
packet: :line
])
end

defp ensure_reaper_is_alive() do
case __MODULE__.start_link() do
{:error, {:already_started, _}} ->
:ok

{:ok, _} ->
:ok

other ->
IO.puts("Ignoring unexpected result from starting reaper: #{inspect(other)}")
:ok
end
end
end

0 comments on commit edae9ab

Please sign in to comment.