Skip to content

Commit

Permalink
Try tcp conn
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismccord committed Sep 6, 2024
1 parent 373e263 commit afb0474
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
28 changes: 25 additions & 3 deletions lib/flame/code_sync.ex
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,37 @@ 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
if pkg.stream do
def extract_packaged_stream(%PackagedStream{} = pkg, ip_port \\ nil) do
stream =
case ip_port do
nil ->
pkg.stream

{remote_ip, port} ->
{:ok, tcp_ip} = :inet.parse_address(~c"#{remote_ip}")

{:ok, socket} =
:gen_tcp.connect(tcp_ip, port, [
:binary,
:inet6,
packet: :raw,
active: false
])

# Receive the file from the server
{:ok, data} = :gen_tcp.recv(socket, 0)
:gen_tcp.close(socket)
File.stream!(data)
end

if stream do
verbose = if pkg.verbose, do: [:verbose], else: []
compressed = if pkg.compress, do: [:compressed], else: []
extract_dir = mfa(pkg.extract_dir)
target_tmp_path = Path.join([mfa(pkg.tmp_dir), "flame_child_code_sync_#{pkg.id}.tar.gz"])
flame_stream = File.stream!(target_tmp_path)
# transfer the file
Enum.into(pkg.stream, flame_stream)
Enum.into(stream, flame_stream)

# extract tar
:ok = :erl_tar.extract(target_tmp_path, [{:cwd, extract_dir}] ++ compressed ++ verbose)
Expand Down
26 changes: 25 additions & 1 deletion lib/flame/runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,36 @@ defmodule FLAME.Runner do
terminator: term
} = new_runner

ip_port =
if base_sync_stream && is_binary(base_sync_stream.stream) do
{:ok, listen_socket} =
:gen_tcp.listen(0, [
:binary,
:inet6,
packet: :raw,
active: false,
reuseaddr: true,
ip: {0, 0, 0, 0, 0, 0, 0, 0}
])

{:ok, port} = :inet.port(listen_socket)

Task.start_link(fn ->
{:ok, socket} = :gen_tcp.accept(listen_socket)
:ok = :gen_tcp.send(socket, base_sync_stream.stream)
{:ok, _ack} = :gen_tcp.recv(socket, 0)
:gen_tcp.close(socket)
end)

{System.fetch_env!("FLY_PRIVATE_IP"), port}
end

{:ok, _} =
remote_call!(runner, new_backend_state, runner.boot_timeout, false, fn ->
# 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 base_sync_stream, do: CodeSync.extract_packaged_stream(base_sync_stream, ip_port)
if beams_stream, do: CodeSync.extract_packaged_stream(beams_stream)

:ok =
Expand Down

0 comments on commit afb0474

Please sign in to comment.