Skip to content

Commit

Permalink
genserver health ping
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Oct 12, 2023
1 parent cc9b87c commit 0305d73
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
13 changes: 13 additions & 0 deletions lib/reaper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ defmodule TestcontainersElixir.Reaper do
GenServer.call(__MODULE__, {:register, filter})
end

def ping do
try do
GenServer.call(__MODULE__, :ping)
catch
:exit, _reason -> :error
end
end

@impl true
def init(connection) do
{:ok, _} =
Expand Down Expand Up @@ -50,6 +58,11 @@ defmodule TestcontainersElixir.Reaper do
{:reply, register_filter(socket, filter), socket}
end

@impl true
def handle_call(:ping, _from, socket) do
{:reply, :ok, socket}
end

defp register_filter(socket, {filter_key, filter_value}) do
:gen_tcp.send(
socket,
Expand Down
28 changes: 23 additions & 5 deletions lib/testcontainers_elixir.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,30 @@ defmodule TestcontainersElixir do
Documentation for `TestcontainersElixir`.
"""

alias TestcontainersElixir.Reaper
alias DockerEngineAPI.Connection
alias DockerEngineAPI.Api

def hello do
connection =
DockerEngineAPI.Connection.new(base_url: "http+unix://%2Fvar%2Frun%2Fdocker.sock/v1.43")
docker_url = "http+unix://%2Fvar%2Frun%2Fdocker.sock/v1.43"

conn = Connection.new(base_url: docker_url)

# TODO create a container, gets its container id and pass it to the next line

:ok = conn |> register_container("some_container_id")

conn |> Api.Image.image_list()
end

defp register_container(conn, container_id) when is_binary(container_id) do
case Reaper.ping() do
:ok ->
Reaper.register({"id", container_id})

{:ok, _pid} = connection |> TestcontainersElixir.Reaper.start_link()
:ok = TestcontainersElixir.Reaper.register({"id", "some_container_id?"})
connection |> DockerEngineAPI.Api.Image.image_list()
:error ->
{:ok, _} = conn |> Reaper.start_link()
Reaper.register({"id", container_id})
end
end
end

0 comments on commit 0305d73

Please sign in to comment.