Skip to content

Commit e64ad84

Browse files
committed
Clean up parent code_sync artifacts on pool shutdown
1 parent 439752e commit e64ad84

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

lib/flame/pool.ex

+8
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ defmodule FLAME.Pool do
201201
:name,
202202
:runner_sup,
203203
:task_sup,
204+
:cleaner,
204205
:terminator_sup,
205206
:child_placement_sup,
206207
:idle_shutdown_after,
@@ -222,6 +223,9 @@ defmodule FLAME.Pool do
222223
])
223224

224225
Keyword.validate!(opts[:code_sync] || [], [
226+
:get_path,
227+
:extract_dir,
228+
:tmp_dir,
225229
:copy_apps,
226230
:copy_paths,
227231
:sync_beams,
@@ -395,6 +399,7 @@ defmodule FLAME.Pool do
395399
)
396400

397401
terminator_sup = Keyword.fetch!(opts, :terminator_sup)
402+
cleaner = Keyword.fetch!(opts, :cleaner)
398403
child_placement_sup = Keyword.fetch!(opts, :child_placement_sup)
399404
runner_opts = runner_opts(opts, terminator_sup)
400405
min = Keyword.fetch!(opts, :min)
@@ -415,6 +420,9 @@ defmodule FLAME.Pool do
415420
|> CodeSync.compute_changed_paths()
416421

417422
%CodeSync.PackagedStream{} = parent_stream = CodeSync.package_to_stream(code_sync)
423+
424+
:ok = FLAME.Pool.Cleaner.watch_path(cleaner, parent_stream.stream.path)
425+
418426
parent_stream
419427
end
420428

lib/flame/pool/cleaner.ex

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
defmodule FLAME.Pool.Cleaner do
2+
@moduledoc false
3+
use GenServer
4+
5+
def start_link(opts) do
6+
GenServer.start_link(__MODULE__, opts, name: Keyword.fetch!(opts, :name))
7+
end
8+
9+
def watch_path(server, path) do
10+
GenServer.call(server, {:watch, path})
11+
end
12+
13+
def list_paths(server) do
14+
GenServer.call(server, :list)
15+
end
16+
17+
def init(_opts) do
18+
Process.flag(:trap_exit, true)
19+
{:ok, %{paths: []}}
20+
end
21+
22+
def handle_call({:watch, path}, _from, state) do
23+
{:reply, :ok, %{state | paths: [path | state.paths]}}
24+
end
25+
26+
def handle_call(:list, _from, state) do
27+
{:reply, state.paths, state}
28+
end
29+
30+
def terminate(_reason, state) do
31+
for path <- state.paths, do: File.rm!(path)
32+
33+
:ok
34+
end
35+
end

lib/flame/pool/supervisor.ex

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ defmodule FLAME.Pool.Supervisor do
1313
def init(opts) do
1414
name = Keyword.fetch!(opts, :name)
1515
runner_sup = Module.concat(name, "RunnerSup")
16+
cleaner = Module.concat(name, "Cleaner")
1617
terminator_sup = Module.concat(name, "TerminatorSup")
1718
task_sup = Module.concat(name, "TaskSup")
1819

@@ -22,13 +23,15 @@ defmodule FLAME.Pool.Supervisor do
2223
pool_opts =
2324
Keyword.merge(opts,
2425
task_sup: task_sup,
26+
cleaner: cleaner,
2527
runner_sup: runner_sup,
2628
terminator_sup: terminator_sup,
2729
child_placement_sup: child_placement_sup
2830
)
2931

3032
children =
3133
[
34+
{FLAME.Pool.Cleaner, name: cleaner},
3235
{Task.Supervisor, name: task_sup, strategy: :one_for_one},
3336
{DynamicSupervisor, name: runner_sup, strategy: :one_for_one},
3437
{DynamicSupervisor, name: terminator_sup, strategy: :one_for_one},

test/flame_test.exs

+26-4
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ defmodule FLAME.FLAMETest do
2121
end
2222

2323
setup config do
24-
runner_opts = Map.fetch!(config, :runner)
25-
runner_sup = Module.concat(config.test, "RunnerSup")
26-
pool_pid = start_supervised!({Pool, Keyword.merge(runner_opts, name: config.test)})
24+
case config do
25+
%{runner: runner_opts} ->
26+
runner_sup = Module.concat(config.test, "RunnerSup")
27+
pool_pid = start_supervised!({Pool, Keyword.merge(runner_opts, name: config.test)})
2728

28-
{:ok, runner_sup: runner_sup, pool_pid: pool_pid}
29+
{:ok, runner_sup: runner_sup, pool_pid: pool_pid}
30+
31+
%{} ->
32+
:ok
33+
end
2934
end
3035

3136
@tag runner: [min: 1, max: 2, max_concurrency: 2]
@@ -619,4 +624,21 @@ defmodule FLAME.FLAMETest do
619624
assert_receive {:DOWN, _, _, ^runner, {:shutdown, :idle}}, 1000
620625
end
621626
end
627+
628+
test "code_sync artifact cleaner", config do
629+
mock = FLAME.Test.CodeSyncMock.new()
630+
631+
cleaner = Module.concat(config.test, "Cleaner")
632+
633+
pool_pid =
634+
start_supervised!(
635+
{Pool, min: 1, max: 1, max_concurrency: 1, name: config.test, code_sync: mock.opts}
636+
)
637+
638+
assert [artifact] = FLAME.Pool.Cleaner.list_paths(cleaner)
639+
assert File.exists?(artifact)
640+
assert FLAME.call(config.test, fn -> :works end) == :works
641+
Supervisor.stop(pool_pid)
642+
refute File.exists?(artifact)
643+
end
622644
end

0 commit comments

Comments
 (0)