Skip to content

Commit

Permalink
convert ryuk genserver/actor to .. plain methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Oct 14, 2023
1 parent ae7fbcc commit 4fd70e4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 43 deletions.
5 changes: 0 additions & 5 deletions lib/docker/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ defmodule TestcontainersElixir.Docker.Api do
end

defp reap_container(container_id) when is_binary(container_id) do
case Reaper.start_link() do
{:error, {:already_started, _}} -> :ok
{:ok, _} -> :ok
end

Reaper.register({"id", container_id})
end

Expand Down
55 changes: 17 additions & 38 deletions lib/reaper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,34 @@
# 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 start_link() do
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
end

def register(filter) do
GenServer.call(__MODULE__, {:register, filter})
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, 5_000) do
{:ok, "ACK\n"} ->
:ok

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

@impl true
def init(_) do
defp get_ryuk_socket() 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
{:reply, register(socket, filter), socket}
end

defp register(socket, {filter_key, filter_value}, retries \\ 3) 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} when retries > 0 ->
IO.puts("Connection was closed, retrying...")
register(socket, {filter_key, filter_value}, retries - 1)

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

Expand Down

0 comments on commit 4fd70e4

Please sign in to comment.