Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up most code_sync artifacts in extract_dir #77

Merged
merged 7 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading