diff --git a/lib/flame/code_sync.ex b/lib/flame/code_sync.ex index e5fb315..41a5d33 100644 --- a/lib/flame/code_sync.ex +++ b/lib/flame/code_sync.ex @@ -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, [ @@ -45,7 +46,8 @@ defmodule FLAME.CodeSync do :start_apps, :verbose, :compress, - :chunk_size + :chunk_size, + :eager ]) compute_start_apps(%CodeSync{ @@ -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 @@ -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{ @@ -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 diff --git a/lib/flame/pool.ex b/lib/flame/pool.ex index 6a13397..962720d 100644 --- a/lib/flame/pool.ex +++ b/lib/flame/pool.ex @@ -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`. @@ -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)) diff --git a/test/code_sync_test.exs b/test/code_sync_test.exs index 22b6694..afd48b7 100644 --- a/test/code_sync_test.exs +++ b/test/code_sync_test.exs @@ -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)