Skip to content

Commit afd81e7

Browse files
authored
Clean up code_sync artifacts in extract_dir (#77)
1 parent 146b934 commit afd81e7

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

lib/flame/code_sync.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ defmodule FLAME.CodeSync do
216216
defp trim_leading_slash([?/ | path]), do: path
217217
defp trim_leading_slash([_ | _] = path), do: path
218218

219-
def extract_packaged_stream(%PackagedStream{} = pkg) do
219+
def extract_packaged_stream(%PackagedStream{} = pkg, terminator) do
220220
if pkg.stream do
221221
verbose = if pkg.verbose, do: [:verbose], else: []
222222
compressed = if pkg.compress, do: [:compressed], else: []
@@ -232,6 +232,9 @@ defmodule FLAME.CodeSync do
232232
# add code paths
233233
:ok = add_code_paths_from_tar(pkg, extract_dir)
234234

235+
# add path to clean up
236+
FLAME.Terminator.watch_path(terminator, extract_dir)
237+
235238
File.rm(target_tmp_path)
236239

237240
# purge any deleted modules

lib/flame/runner.ex

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,10 @@ defmodule FLAME.Runner do
253253
new_state
254254

255255
{new_state, %CodeSync.PackagedStream{} = parent_pkg} ->
256+
terminator = state.runner.terminator
257+
256258
remote_call!(state.runner, state.backend_state, state.runner.boot_timeout, false, fn ->
257-
:ok = CodeSync.extract_packaged_stream(parent_pkg)
259+
:ok = CodeSync.extract_packaged_stream(parent_pkg, terminator)
258260
end)
259261

260262
CodeSync.rm_packaged_stream(parent_pkg)
@@ -306,8 +308,13 @@ defmodule FLAME.Runner do
306308
# ensure app is fully started if parent connects before up
307309
if otp_app, do: {:ok, _} = Application.ensure_all_started(otp_app)
308310

309-
if base_sync_stream, do: CodeSync.extract_packaged_stream(base_sync_stream)
310-
if beams_stream, do: CodeSync.extract_packaged_stream(beams_stream)
311+
if base_sync_stream do
312+
CodeSync.extract_packaged_stream(base_sync_stream, term)
313+
end
314+
315+
if beams_stream do
316+
CodeSync.extract_packaged_stream(beams_stream, term)
317+
end
311318

312319
:ok =
313320
Terminator.schedule_idle_shutdown(term, idle_after, idle_check, single_use)

lib/flame/terminator.ex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ defmodule FLAME.Terminator do
3232
single_use: false,
3333
calls: %{},
3434
watchers: %{},
35+
paths: [],
3536
log: false,
3637
status: nil,
3738
failsafe_timer: nil,
@@ -73,6 +74,10 @@ defmodule FLAME.Terminator do
7374
GenServer.call(terminator, {:watch, pids})
7475
end
7576

77+
def watch_path(terminator, path) do
78+
GenServer.call(terminator, {:watch_path, path})
79+
end
80+
7681
def deadline_me(terminator, timeout) do
7782
GenServer.call(terminator, {:deadline, timeout})
7883
end
@@ -263,6 +268,10 @@ defmodule FLAME.Terminator do
263268
{:reply, :ok, cancel_idle_shutdown(state)}
264269
end
265270

271+
def handle_call({:watch_path, path}, _from, %Terminator{watchers: paths} = state) do
272+
{:reply, :ok, %{state | paths: [path | paths]}}
273+
end
274+
266275
def handle_call(:system_shutdown, _from, %Terminator{} = state) do
267276
{:reply, :ok,
268277
system_stop(state, "system shutdown instructed from parent #{inspect(state.parent.pid)}")}
@@ -288,13 +297,22 @@ defmodule FLAME.Terminator do
288297
{:reply, :ok, schedule_idle_shutdown(new_state)}
289298
end
290299

300+
defp clean_up_paths(paths) do
301+
for path <- paths do
302+
File.rm_rf(path)
303+
end
304+
end
305+
291306
@impl true
292307
def terminate(_reason, %Terminator{} = state) do
293308
state =
294309
state
295310
|> cancel_idle_shutdown()
296311
|> system_stop("terminating")
297312

313+
# clean up any paths that were watched before waiting to not be killed
314+
clean_up_paths(state.paths)
315+
298316
# supervisor will force kill us if we take longer than configured shutdown_timeout
299317
Enum.each(state.calls, fn
300318
# skip callers that placed a child since they are on the remote node

0 commit comments

Comments
 (0)