Skip to content

Commit 1e6861e

Browse files
committed
run tasks with unlinked testcontainers genserver,
this will die when the beam process exits fixes #197
1 parent b4875e3 commit 1e6861e

4 files changed

Lines changed: 30 additions & 17 deletions

File tree

lib/mix/tasks/testcontainers/run.ex

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ defmodule Mix.Tasks.Testcontainers.Run do
2121
{:ok, _} = Application.ensure_all_started(app)
2222
end)
2323

24-
{:ok, _} = Testcontainers.start_link()
24+
{:ok, _} = Testcontainers.start()
2525

2626
{opts, rest_args, _} =
2727
OptionParser.parse(args,
@@ -41,25 +41,24 @@ defmodule Mix.Tasks.Testcontainers.Run do
4141
[] -> {"test", []}
4242
end
4343

44-
run_sub_task_and_exit(database, sub_task, sub_task_args, db_volume)
45-
end
44+
IO.puts("Starting database container: #{database}")
45+
IO.puts("Using database volume: #{db_volume || "none"}")
4646

47-
@spec run_sub_task_and_exit(String.t(), String.t(), list(String.t()), String.t() | nil) :: no_return()
48-
defp run_sub_task_and_exit(database, sub_task, sub_task_args, db_volume) do
49-
{container, env} = setup_container(database, db_volume)
47+
{_container, env} = setup_container(database, db_volume)
5048

51-
IO.puts("Starting in-process mix task: #{sub_task} #{Enum.join(sub_task_args, " ")}")
49+
run_sub_task_and_exit(sub_task, sub_task_args, env)
50+
end
5251

53-
System.at_exit(fn _ ->
54-
try do
55-
Testcontainers.stop_container(container.container_id)
56-
catch
57-
_, _ -> :ok
58-
end
59-
end)
52+
@spec run_sub_task_and_exit(String.t(), list(String.t()), list({String.t(), String.t()})) :: no_return()
53+
defp run_sub_task_and_exit(sub_task, sub_task_args, env) do
54+
55+
IO.puts("Starting mix task: #{sub_task} #{Enum.join(sub_task_args, " ")}")
6056

6157
Enum.each(env, fn {k, v} -> System.put_env(k, v) end)
62-
Mix.Task.run(sub_task, sub_task_args)
58+
59+
:ok = Mix.Task.run(sub_task, sub_task_args)
60+
61+
IO.puts("Task finished: #{sub_task} #{Enum.join(sub_task_args, " ")}")
6362
end
6463

6564
defp setup_container(database, db_volume) do

lib/testcontainers.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,24 @@ defmodule Testcontainers do
2222

2323
@timeout 300_000
2424

25+
@doc """
26+
Starts the Testcontainers application.
27+
28+
This will terminate when the calling process exits, for ex a task.
29+
"""
2530
def start_link(options \\ []) do
2631
GenServer.start_link(__MODULE__, options, name: Keyword.get(options, :name, __MODULE__))
2732
end
2833

34+
@doc """
35+
Starts the Testcontainers application.
36+
37+
This will NOT terminate when the calling process exits, for ex a task.
38+
"""
39+
def start(options \\ []) do
40+
GenServer.start(__MODULE__, options, name: Keyword.get(options, :name, __MODULE__))
41+
end
42+
2943
@impl true
3044
def init(options \\ []) do
3145
Process.flag(:trap_exit, true)

lib/util/constants.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule Testcontainers.Constants do
22
@moduledoc false
33

44
def library_name, do: :testcontainers
5-
def library_version, do: "1.13.2"
5+
def library_version, do: "1.13.3"
66
def ryuk_version, do: "0.11.0"
77
def container_label, do: "org.testcontainers"
88
def container_lang_label, do: "org.testcontainers.lang"

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule TestcontainersElixir.MixProject do
22
use Mix.Project
33

44
@app :testcontainers
5-
@version "1.13.2"
5+
@version "1.13.3"
66
@source_url "https://github.com/testcontainers/testcontainers-elixir"
77

88
def project do

0 commit comments

Comments
 (0)