@@ -10,7 +10,7 @@ defmodule Mix.Tasks.Deps.Partition do
1010 @ deps_partition_install_mix_exs ~c" deps.partition.mix.exs"
1111
1212 def server ( deps , count , force? ) do
13- { :ok , socket } = :gen_tcp . listen ( 0 , [ :binary , packet: :line , active: true , reuseaddr: true ] )
13+ { :ok , socket } = :gen_tcp . listen ( 0 , [ :binary , packet: :line , active: false ] )
1414
1515 try do
1616 server ( socket , deps , count , force? )
@@ -71,28 +71,37 @@ defmodule Mix.Tasks.Deps.Partition do
7171 :use_stdio ,
7272 :stderr_to_stdout ,
7373 line: 1_000_000 ,
74- args: args ,
7574 env: [ { ~c" MIX_OS_CONCURRENCY_LOCK" , ~c" false" } | env_vars ]
7675 ]
7776
78- clients =
79- Enum . map ( 1 .. count // 1 , fn index ->
77+ ports =
78+ Map . new ( 1 .. count // 1 , fn index ->
8079 if Mix . debug? ( ) do
8180 IO . puts ( "-> Starting mix deps.partition ##{ index } " )
8281 end
8382
84- port = Port . open ( { :spawn_executable , String . to_charlist ( elixir ) } , options )
83+ args = args ++ [ ~c" --index" , Integer . to_charlist ( index ) ]
84+ port = Port . open ( { :spawn_executable , String . to_charlist ( elixir ) } , [ args: args ] ++ options )
8585
86- case :gen_tcp . accept ( socket , 15000 ) do
87- { :ok , client } ->
88- % { port: port , index: index , socket: client }
86+ { index , port }
87+ end )
8988
89+ clients =
90+ Enum . map ( 1 .. count // 1 , fn _ ->
91+ with { :ok , client } <- :gen_tcp . accept ( socket , 15000 ) ,
92+ { :ok , message } <- :gen_tcp . recv ( socket , 0 , 15000 ) do
93+ :inet . setopts ( client , active: true )
94+ index = message |> String . trim ( ) |> String . to_integer ( )
95+ % { port: Map . fetch! ( ports , index ) , index: index , socket: client }
96+ else
9097 error ->
98+ logs =
99+ Enum . map_join ( ports , "\n " , fn { index , port } -> close_port ( port , "#{ index } >" ) end )
100+
91101 Mix . raise ( """
92102 could not start partition dependency compiler, no connection made to TCP port: #{ inspect ( error ) }
93103
94- The spawned operating system process wrote the following output:
95- #{ close_port ( port , "" ) }
104+ #{ logs }
96105 """ )
97106 end
98107 end )
@@ -203,7 +212,7 @@ defmodule Mix.Tasks.Deps.Partition do
203212
204213 ## Client
205214
206- @ switches [ port: :integer , host: :string , force: :boolean ]
215+ @ switches [ port: :integer , host: :string , force: :boolean , index: :string ]
207216
208217 @ impl true
209218 def run ( args ) do
@@ -216,11 +225,14 @@ defmodule Mix.Tasks.Deps.Partition do
216225 { opts , [ ] } = OptionParser . parse! ( args , strict: @ switches )
217226 host = Keyword . fetch! ( opts , :host )
218227 port = Keyword . fetch! ( opts , :port )
228+ index = Keyword . fetch! ( opts , :index )
219229 force? = Keyword . get ( opts , :force , false )
220230
221231 { :ok , socket } =
222232 :gen_tcp . connect ( String . to_charlist ( host ) , port , [ :binary , packet: :line , active: false ] )
223233
234+ :gen_tcp . send ( socket , "#{ index } \n " )
235+
224236 try do
225237 deps = Mix.Dep . load_and_cache ( )
226238 client_loop ( socket , deps , force? , Mix.Project . deps_config ( ) )
0 commit comments