Skip to content

Commit

Permalink
Clean up code_sync artifacts in extract_dir (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcnnn authored Feb 7, 2025
1 parent 146b934 commit afd81e7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
5 changes: 4 additions & 1 deletion lib/flame/code_sync.ex
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ defmodule FLAME.CodeSync do
defp trim_leading_slash([?/ | path]), do: path
defp trim_leading_slash([_ | _] = path), do: path

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

# add path to clean up
FLAME.Terminator.watch_path(terminator, extract_dir)

File.rm(target_tmp_path)

# purge any deleted modules
Expand Down
13 changes: 10 additions & 3 deletions lib/flame/runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,10 @@ defmodule FLAME.Runner do
new_state

{new_state, %CodeSync.PackagedStream{} = parent_pkg} ->
terminator = state.runner.terminator

remote_call!(state.runner, state.backend_state, state.runner.boot_timeout, false, fn ->
:ok = CodeSync.extract_packaged_stream(parent_pkg)
:ok = CodeSync.extract_packaged_stream(parent_pkg, terminator)
end)

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

if base_sync_stream, do: CodeSync.extract_packaged_stream(base_sync_stream)
if beams_stream, do: CodeSync.extract_packaged_stream(beams_stream)
if base_sync_stream do
CodeSync.extract_packaged_stream(base_sync_stream, term)
end

if beams_stream do
CodeSync.extract_packaged_stream(beams_stream, term)
end

:ok =
Terminator.schedule_idle_shutdown(term, idle_after, idle_check, single_use)
Expand Down
18 changes: 18 additions & 0 deletions lib/flame/terminator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ defmodule FLAME.Terminator do
single_use: false,
calls: %{},
watchers: %{},
paths: [],
log: false,
status: nil,
failsafe_timer: nil,
Expand Down Expand Up @@ -73,6 +74,10 @@ defmodule FLAME.Terminator do
GenServer.call(terminator, {:watch, pids})
end

def watch_path(terminator, path) do
GenServer.call(terminator, {:watch_path, path})
end

def deadline_me(terminator, timeout) do
GenServer.call(terminator, {:deadline, timeout})
end
Expand Down Expand Up @@ -263,6 +268,10 @@ defmodule FLAME.Terminator do
{:reply, :ok, cancel_idle_shutdown(state)}
end

def handle_call({:watch_path, path}, _from, %Terminator{watchers: paths} = state) do
{:reply, :ok, %{state | paths: [path | paths]}}
end

def handle_call(:system_shutdown, _from, %Terminator{} = state) do
{:reply, :ok,
system_stop(state, "system shutdown instructed from parent #{inspect(state.parent.pid)}")}
Expand All @@ -288,13 +297,22 @@ defmodule FLAME.Terminator do
{:reply, :ok, schedule_idle_shutdown(new_state)}
end

defp clean_up_paths(paths) do
for path <- paths do
File.rm_rf(path)
end
end

@impl true
def terminate(_reason, %Terminator{} = state) do
state =
state
|> cancel_idle_shutdown()
|> system_stop("terminating")

# clean up any paths that were watched before waiting to not be killed
clean_up_paths(state.paths)

# supervisor will force kill us if we take longer than configured shutdown_timeout
Enum.each(state.calls, fn
# skip callers that placed a child since they are on the remote node
Expand Down

0 comments on commit afd81e7

Please sign in to comment.