Skip to content

Commit

Permalink
make reapeer stop properly if failing
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Oct 13, 2023
1 parent d97df0b commit 682b39e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule TestcontainersElixir.Connection do
@api_version "v1.41"

def get_connection do
Connection.new(base_url: docker_base_url() |> IO.inspect())
Connection.new(base_url: docker_base_url())
end

defp docker_base_url do
Expand Down
6 changes: 3 additions & 3 deletions lib/containers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule TestcontainersElixir.Containers do
container_id = container."Id",
{:ok, _} <- Api.Container.container_start(conn, container_id),
:ok <- on_exit.(:stop_container, fn -> stop_container(conn, container_id) end),
:ok <- reap_container(conn, container_id),
:ok <- reap_container(container_id),
{:ok, container} <- get_container(conn, container_id),
{:ok, _} <- waiting_strategy.(conn, container) do
{:ok, container}
Expand All @@ -44,8 +44,8 @@ defmodule TestcontainersElixir.Containers do
end
end

defp reap_container(conn, container_id) when is_binary(container_id) do
case conn |> Reaper.start_link() do
defp reap_container(container_id) when is_binary(container_id) do
case Reaper.start_link() do
{:error, {:already_started, _}} -> :ok
{:ok, _} -> :ok
end
Expand Down
11 changes: 7 additions & 4 deletions lib/reaper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
defmodule TestcontainersElixir.Reaper do
use GenServer

alias TestcontainersElixir.Connection
alias DockerEngineAPI.Api
alias DockerEngineAPI.Model
alias TestcontainersElixir.Container

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

def start_link(connection) do
GenServer.start_link(__MODULE__, connection, name: __MODULE__)
def start_link() do
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
end

def register(filter) do
GenServer.call(__MODULE__, {:register, filter})
end

@impl true
def init(connection) do
Process.flag(:trap_exit, true)
def init(_) do
connection = Connection.get_connection()

with {:ok, _image_create_response} <-
Api.Image.image_create(connection, fromImage: @ryuk_image),
Expand All @@ -32,6 +33,8 @@ defmodule TestcontainersElixir.Reaper do
container = Container.of(container_info),
{:ok, socket} <- create_ryuk_socket(container) do
{:ok, socket}
else error ->
{:stop, "Failed to start reaper: #{inspect(error)}"}
end
end

Expand Down

0 comments on commit 682b39e

Please sign in to comment.