@@ -164,13 +164,12 @@ impl Launch {
164164 host_outcome ( spec)
165165 }
166166
167- /// The `run_custom_host_json` request for one host's plan and optional cancellation marker.
168- fn spec ( & self , plan : & HostPlan , cancel_marker : Option < & Path > ) -> String {
167+ /// The host request for one plan and cancel marker; only split hosts detect module state .
168+ fn spec ( & self , plan : & HostPlan , cancel_marker : & Path , detect_state : bool ) -> String {
169169 let mut payload: Map < String , Value > = self . payload . clone ( ) ;
170170 payload. insert ( "plan" . to_owned ( ) , json ! ( plan) ) ;
171- if let Some ( marker) = cancel_marker {
172- payload. insert ( "cancel_marker" . to_owned ( ) , json ! ( text( marker) ) ) ;
173- }
171+ payload. insert ( "cancel_marker" . to_owned ( ) , json ! ( text( cancel_marker) ) ) ;
172+ payload. insert ( "detect_state" . to_owned ( ) , json ! ( detect_state) ) ;
174173 json ! ( {
175174 "program" : self . program,
176175 "arguments" : self . arguments,
@@ -189,36 +188,23 @@ fn run_partitions<'py>(
189188 ( input_dir, cancel) : ( & Path , & HostCancel ) ,
190189 ( write_inputs, abandoned) : ( impl FnOnce ( ) -> PyResult < ( ) > , & Path ) ,
191190) -> PyResult < Vec < HostSlice > > {
192- if plans. len ( ) == 1 {
193- let spec : String = launch . spec ( plan , None ) ;
194- let outcomes : Vec < HostOutcome > = run_hosts (
195- py ,
196- ( launch , vec ! [ spec ] ) ,
197- ( write_inputs, abandoned) ,
198- ( None , cancel ) ,
199- ) ? ;
191+ let split : bool = plans. len ( ) > 1 ;
192+ let outcomes : Vec < HostOutcome > = run_cancellable (
193+ py ,
194+ ( launch , plans , split ) ,
195+ ( input_dir , cancel ) ,
196+ ( write_inputs, abandoned) ,
197+ ) ? ;
198+ if !split {
200199 return outcomes
201200 . into_iter ( )
202201 . map ( |outcome| outcome. map_err ( |message| rules_error ( py, message) ) )
203202 . collect ( ) ;
204203 }
205- let cancel_dir: PathBuf = unique_directory ( input_dir, "hosts-" ) ?;
206- let marker: PathBuf = cancel_dir. join ( CUSTOM_HOST_CANCELLED_MARKER ) ;
207- let specs: Vec < String > = plans
208- . iter ( )
209- . map ( |partition| launch. spec ( partition, Some ( & marker) ) )
210- . collect ( ) ;
211- let outcomes: PyResult < Vec < HostOutcome > > = run_hosts (
212- py,
213- ( launch, specs) ,
214- ( write_inputs, abandoned) ,
215- ( Some ( & marker) , cancel) ,
216- ) ;
217- let _removed: std:: io:: Result < ( ) > = std:: fs:: remove_dir_all ( & cancel_dir) ;
218- let outcomes: Vec < HostOutcome > = outcomes?;
219204 interrupted ( py, cancel) ?;
220205 if outcomes. iter ( ) . any ( Result :: is_err) {
221- return single_host ( py, launch, plan) . map ( |slice| vec ! [ slice] ) ;
206+ return single_host ( py, ( launch, plan) , ( input_dir, cancel) , abandoned)
207+ . map ( |slice| vec ! [ slice] ) ;
222208 }
223209 let slices: Vec < HostSlice > = outcomes. into_iter ( ) . filter_map ( Result :: ok) . collect ( ) ;
224210 let stateful: BTreeSet < String > = slices
@@ -239,7 +225,7 @@ fn run_partitions<'py>(
239225 ( code. clone ( ) , subjects)
240226 } )
241227 . collect ( ) ;
242- let rerun: HostSlice = single_host ( py, launch, & rerun_plan) ?;
228+ let rerun: HostSlice = single_host ( py, ( launch, & rerun_plan) , ( input_dir , cancel ) , abandoned ) ?;
243229 let mut merged: Vec < HostSlice > = slices
244230 . into_iter ( )
245231 . map ( |slice| without_codes ( slice, & stateful) )
@@ -248,19 +234,55 @@ fn run_partitions<'py>(
248234 Ok ( merged)
249235}
250236
251- /// Run one host for `plan` once the payload already exists.
252- fn single_host ( py : Python < ' _ > , launch : & Launch , plan : & HostPlan ) -> PyResult < HostSlice > {
253- let spec: String = launch. spec ( plan, None ) ;
254- py. detach ( || launch. run ( & spec) )
237+ /// Run one host for `plan` from its written payload; an interrupt stops its next invocation.
238+ fn single_host (
239+ py : Python < ' _ > ,
240+ ( launch, plan) : ( & Launch , & HostPlan ) ,
241+ ( input_dir, cancel) : ( & Path , & HostCancel ) ,
242+ abandoned : & Path ,
243+ ) -> PyResult < HostSlice > {
244+ let outcomes: Vec < HostOutcome > = run_cancellable (
245+ py,
246+ ( launch, std:: slice:: from_ref ( plan) , false ) ,
247+ ( input_dir, cancel) ,
248+ ( || Ok ( ( ) ) , abandoned) ,
249+ ) ?;
250+ outcomes
251+ . into_iter ( )
252+ . next ( )
253+ . unwrap_or_else ( || Err ( "custom rule host returned no outcome" . to_owned ( ) ) )
255254 . map_err ( |message| rules_error ( py, message) )
256255}
257256
257+ /// Run one host per plan with a fresh cancel marker that an interrupt or failed host sets.
258+ fn run_cancellable (
259+ py : Python < ' _ > ,
260+ ( launch, plans, detect_state) : ( & Launch , & [ HostPlan ] , bool ) ,
261+ ( input_dir, cancel) : ( & Path , & HostCancel ) ,
262+ ( write_inputs, abandoned) : ( impl FnOnce ( ) -> PyResult < ( ) > , & Path ) ,
263+ ) -> PyResult < Vec < HostOutcome > > {
264+ let cancel_dir: PathBuf = unique_directory ( input_dir, "hosts-" ) ?;
265+ let marker: PathBuf = cancel_dir. join ( CUSTOM_HOST_CANCELLED_MARKER ) ;
266+ let specs: Vec < String > = plans
267+ . iter ( )
268+ . map ( |partition| launch. spec ( partition, & marker, detect_state) )
269+ . collect ( ) ;
270+ let outcomes: PyResult < Vec < HostOutcome > > = run_hosts (
271+ py,
272+ ( launch, specs) ,
273+ ( write_inputs, abandoned) ,
274+ ( & marker, cancel) ,
275+ ) ;
276+ let _removed: std:: io:: Result < ( ) > = std:: fs:: remove_dir_all ( & cancel_dir) ;
277+ outcomes
278+ }
279+
258280/// Run every host on its own thread while this thread publishes the payload they wait for.
259281fn run_hosts (
260282 py : Python < ' _ > ,
261283 ( launch, specs) : ( & Launch , Vec < String > ) ,
262284 ( write_inputs, abandoned) : ( impl FnOnce ( ) -> PyResult < ( ) > , & Path ) ,
263- ( cancel_marker, cancel) : ( Option < & Path > , & HostCancel ) ,
285+ ( cancel_marker, cancel) : ( & Path , & HostCancel ) ,
264286) -> PyResult < Vec < HostOutcome > > {
265287 std:: thread:: scope ( |scope| {
266288 let ( sender, finished) : ( Sender < HostStatus > , Receiver < HostStatus > ) = channel ( ) ;
@@ -305,7 +327,7 @@ fn run_hosts(
305327fn await_hosts (
306328 py : Python < ' _ > ,
307329 ( finished, count) : ( Receiver < HostStatus > , usize ) ,
308- cancel_marker : Option < & Path > ,
330+ cancel_marker : & Path ,
309331 cancel : & HostCancel ,
310332) -> PyResult < ( ) > {
311333 let finished: Mutex < Receiver < HostStatus > > = Mutex :: new ( finished) ;
@@ -341,11 +363,9 @@ fn interrupted(py: Python<'_>, cancel: &HostCancel) -> PyResult<()> {
341363 Ok ( ( ) )
342364}
343365
344- /// Stop split hosts at their next invocation; a running invocation still ends or times out.
345- fn cancel_hosts ( cancel_marker : Option < & Path > ) {
346- if let Some ( marker) = cancel_marker {
347- let _touched: std:: io:: Result < ( ) > = touch ( marker) ;
348- }
366+ /// Stop hosts at their next invocation; a running invocation still ends or times out.
367+ fn cancel_hosts ( cancel_marker : & Path ) {
368+ let _touched: std:: io:: Result < ( ) > = touch ( cancel_marker) ;
349369}
350370
351371/// One host run decoded, or the message `RulesError` reports for it.
0 commit comments