Skip to content

Commit

Permalink
eager
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismccord committed Sep 6, 2024
1 parent e43a339 commit 6ac9765
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
23 changes: 18 additions & 5 deletions lib/flame/code_sync.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ defmodule FLAME.CodeSync do
purge_modules: [],
verbose: false,
compress: false,
chunk_size: 64_000
chunk_size: 64_000,
eager: false

def new(opts \\ []) do
Keyword.validate!(opts, [
Expand All @@ -45,7 +46,8 @@ defmodule FLAME.CodeSync do
:start_apps,
:verbose,
:compress,
:chunk_size
:chunk_size,
:eager
])

compute_start_apps(%CodeSync{
Expand All @@ -57,7 +59,8 @@ defmodule FLAME.CodeSync do
start_apps: Keyword.get(opts, :start_apps, true),
verbose: Keyword.get(opts, :verbose, false),
compress: Keyword.get(opts, :compress, true),
chunk_size: Keyword.get(opts, :chunk_size, 64_000)
chunk_size: Keyword.get(opts, :chunk_size, 64_000),
eager: Keyword.get(opts, :eager, false)
})
end

Expand Down Expand Up @@ -188,7 +191,13 @@ defmodule FLAME.CodeSync do
log_verbose("packaged size: #{File.stat!(out_path).size / (1024 * 1024)}mb")
end

[File.stream!(out_path, [], code.chunk_size) |> Enum.to_list() |> IO.iodata_to_binary()]
stream = File.stream!(out_path, [], code.chunk_size)

if code.eager do
[stream |> Enum.to_list() |> IO.iodata_to_binary()]
else
stream
end
end

%PackagedStream{
Expand Down Expand Up @@ -260,7 +269,11 @@ defmodule FLAME.CodeSync do
end

def rm_packaged_stream(%PackagedStream{} = pkg) do
# if pkg.stream, do: File.rm(pkg.stream.path)
case pkg.stream do
val when is_binary(val) or is_nil(val) -> :noop
%File.Stream{path: path} -> File.rm(path)
end

:ok
end

Expand Down
6 changes: 5 additions & 1 deletion lib/flame/pool.ex
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ defmodule FLAME.Pool do
when the runner boots. When `true`, all applications currently running on the parent node
are sent to the runner node to be started. Defaults to `false`.
* `:eager` – Whether to load the code sync artifacts eagerly into memory or stream
from disk. Eager can enable faster code sync but will consume more memory. Defaults to `false`.
* `:verbose` – If `true`, the pool will log verbose information about the code sync process.
Defaults to `false`.
Expand Down Expand Up @@ -224,7 +227,8 @@ defmodule FLAME.Pool do
:start_apps,
:verbose,
:compress,
:chunk_size
:chunk_size,
:eager,
])

GenServer.start_link(__MODULE__, opts, name: Keyword.fetch!(opts, :name))
Expand Down
2 changes: 1 addition & 1 deletion test/code_sync_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ defmodule FLAME.CodeSyncTest do
|> CodeSync.compute_copy_paths()

assert %FLAME.CodeSync.PackagedStream{} = pkg = CodeSync.package_to_stream(code)
# assert File.exists?(pkg.stream.path)
assert File.exists?(pkg.stream.path)

# cheap way to ensure apps are started on extract. Note async: false is required
Application.stop(:logger)
Expand Down

0 comments on commit 6ac9765

Please sign in to comment.