@@ -26,7 +26,7 @@ use crate::{
26
26
CoordinatorMessage , JobId , Multiplexed , OneToOneResponse , ReadFileRequest ,
27
27
ReadFileResponse , SerializedError , WorkerMessage , WriteFileRequest ,
28
28
} ,
29
- sandbox:: { CompileRequest , CompileResponse , CompileResponseWithOutput , Channel } ,
29
+ sandbox:: { Channel , CompileRequest , CompileResponse , CompileResponseWithOutput } ,
30
30
DropErrorDetailsExt ,
31
31
} ;
32
32
@@ -37,14 +37,34 @@ enum DemultiplexCommand {
37
37
}
38
38
39
39
#[ derive( Debug ) ]
40
- pub struct Container {
41
- stable : ContainerCore ,
42
- beta : ContainerCore ,
43
- nightly : ContainerCore ,
40
+ pub struct Coordinator < B > {
41
+ backend : B ,
42
+ // Consider making these lazily-created and/or idly time out
43
+ stable : Container ,
44
+ beta : Container ,
45
+ nightly : Container ,
44
46
token : CancellationToken ,
45
47
}
46
48
47
- impl Container {
49
+ impl < B > Coordinator < B >
50
+ where
51
+ B : Backend ,
52
+ {
53
+ pub fn new ( backend : B ) -> Result < Self , Error > {
54
+ let token = CancellationToken :: new ( ) ;
55
+
56
+ let [ stable, beta, nightly] =
57
+ Channel :: ALL . map ( |channel| Container :: new ( channel, token. clone ( ) , & backend) ) ;
58
+
59
+ Ok ( Self {
60
+ backend,
61
+ stable : stable?,
62
+ beta : beta?,
63
+ nightly : nightly?,
64
+ token,
65
+ } )
66
+ }
67
+
48
68
pub async fn compile (
49
69
& self ,
50
70
request : CompileRequest ,
@@ -56,11 +76,14 @@ impl Container {
56
76
& self ,
57
77
request : CompileRequest ,
58
78
) -> Result < ActiveCompilation , CompileError > {
59
- self . select_channel ( request. channel ) . begin_compile ( request) . await
79
+ self . select_channel ( request. channel )
80
+ . begin_compile ( request)
81
+ . await
60
82
}
61
83
62
- pub async fn shutdown ( self ) -> Result < ( ) > {
84
+ pub async fn shutdown ( self ) -> Result < B > {
63
85
let Self {
86
+ backend,
64
87
stable,
65
88
beta,
66
89
nightly,
@@ -70,16 +93,14 @@ impl Container {
70
93
71
94
let ( stable, beta, nightly) = join ! ( stable. shutdown( ) , beta. shutdown( ) , nightly. shutdown( ) ) ;
72
95
73
- stable. unwrap ( ) ;
96
+ stable. unwrap ( ) ; // ContainerTaskPanickedSnafu
74
97
beta. unwrap ( ) ;
75
98
nightly. unwrap ( ) ;
76
99
77
- Ok ( ( ) )
78
- // task.await.context(ContainerTaskPanickedSnafu)?
100
+ Ok ( backend)
79
101
}
80
102
81
-
82
- fn select_channel ( & self , channel : Channel ) -> & ContainerCore {
103
+ fn select_channel ( & self , channel : Channel ) -> & Container {
83
104
match channel {
84
105
Channel :: Stable => & self . stable ,
85
106
Channel :: Beta => & self . beta ,
@@ -88,13 +109,19 @@ impl Container {
88
109
}
89
110
}
90
111
112
+ impl Coordinator < DockerBackend > {
113
+ pub fn new_docker ( ) -> Result < Self , Error > {
114
+ Self :: new ( DockerBackend ( ( ) ) )
115
+ }
116
+ }
117
+
91
118
#[ derive( Debug ) ]
92
- pub struct ContainerCore {
119
+ pub struct Container {
93
120
task : JoinHandle < Result < ( ) > > ,
94
121
commander : Commander ,
95
122
}
96
123
97
- impl ContainerCore {
124
+ impl Container {
98
125
fn new ( channel : Channel , token : CancellationToken , backend : & impl Backend ) -> Result < Self > {
99
126
let ( mut child, stdin, stdout) = backend. run_worker_in_background ( channel) ?;
100
127
let IoQueue {
@@ -124,11 +151,7 @@ impl ContainerCore {
124
151
id : Default :: default ( ) ,
125
152
} ;
126
153
127
- Ok ( ContainerCore {
128
- task,
129
- commander,
130
- } )
131
-
154
+ Ok ( Container { task, commander } )
132
155
}
133
156
134
157
pub async fn compile (
@@ -234,10 +257,7 @@ impl ContainerCore {
234
257
}
235
258
236
259
pub async fn shutdown ( self ) -> Result < ( ) > {
237
- let Self {
238
- task,
239
- commander,
240
- } = self ;
260
+ let Self { task, commander } = self ;
241
261
drop ( commander) ;
242
262
task. await . context ( ContainerTaskPanickedSnafu ) ?
243
263
}
@@ -495,40 +515,11 @@ pub enum CommanderError {
495
515
WorkerOperationFailed { text : String } ,
496
516
}
497
517
498
- #[ derive( Debug ) ]
499
- pub struct Coordinator < B > {
500
- backend : B ,
501
- }
502
-
503
- impl < B : Backend > Coordinator < B > {
504
- pub fn new ( backend : B ) -> Self {
505
- Self { backend }
506
- }
507
-
508
- pub fn allocate ( & mut self ) -> Result < Container , Error > {
509
- let token = CancellationToken :: new ( ) ;
510
-
511
- let [ stable, beta, nightly] = Channel :: ALL . map ( |channel| {
512
- ContainerCore :: new ( channel, token. clone ( ) , & self . backend )
513
- } ) ;
514
-
515
- Ok ( Container {
516
- stable : stable?,
517
- beta : beta?,
518
- nightly : nightly?,
519
- token,
520
- } )
521
- }
522
- }
523
-
524
- impl Coordinator < DockerBackend > {
525
- pub fn new_docker ( ) -> Self {
526
- Self :: new ( DockerBackend ( ( ) ) )
527
- }
528
- }
529
-
530
518
pub trait Backend {
531
- fn run_worker_in_background ( & self , channel : Channel ) -> Result < ( Child , ChildStdin , ChildStdout ) > {
519
+ fn run_worker_in_background (
520
+ & self ,
521
+ channel : Channel ,
522
+ ) -> Result < ( Child , ChildStdin , ChildStdout ) > {
532
523
let mut child = self
533
524
. prepare_worker_command ( channel)
534
525
. stdin ( Stdio :: piped ( ) )
@@ -544,6 +535,15 @@ pub trait Backend {
544
535
fn prepare_worker_command ( & self , channel : Channel ) -> Command ;
545
536
}
546
537
538
+ impl < B > Backend for & B
539
+ where
540
+ B : Backend ,
541
+ {
542
+ fn prepare_worker_command ( & self , channel : Channel ) -> Command {
543
+ B :: prepare_worker_command ( self , channel)
544
+ }
545
+ }
546
+
547
547
macro_rules! docker_command {
548
548
( $( $arg: expr) ,* $( , ) ?) => ( {
549
549
let mut cmd = Command :: new( "docker" ) ;
@@ -721,6 +721,7 @@ mod tests {
721
721
722
722
use super :: * ;
723
723
724
+ #[ derive( Debug ) ]
724
725
struct TestBackend {
725
726
project_dir : TempDir ,
726
727
}
@@ -754,10 +755,7 @@ mod tests {
754
755
755
756
impl Backend for TestBackend {
756
757
fn prepare_worker_command ( & self , channel : Channel ) -> Command {
757
- let toolchain_file = format ! ( r#"
758
- [toolchain]
759
- channel = "{}"
760
- "# , channel. to_str( ) ) ;
758
+ let toolchain_file = format ! ( r#"[toolchain]\nchannel = "{}""# , channel. to_str( ) ) ;
761
759
let path = self . project_dir . path ( ) . join ( "rust-toolchain.toml" ) ;
762
760
std:: fs:: write ( path, toolchain_file) . expect ( "Couldn't write toolchain file" ) ;
763
761
@@ -767,7 +765,7 @@ mod tests {
767
765
}
768
766
}
769
767
770
- fn new_coordinator ( ) -> Coordinator < impl Backend > {
768
+ fn new_coordinator ( ) -> Result < Coordinator < impl Backend > > {
771
769
Coordinator :: new ( TestBackend :: new ( ) )
772
770
// Coordinator::new_docker()
773
771
}
@@ -788,35 +786,33 @@ mod tests {
788
786
#[ tokio:: test]
789
787
#[ snafu:: report]
790
788
async fn test_compile_response ( ) -> Result < ( ) > {
791
- let mut coordinator = new_coordinator ( ) ;
789
+ let coordinator = new_coordinator ( ) ? ;
792
790
793
- let container = coordinator. allocate ( ) ?;
794
791
let response = tokio:: time:: timeout (
795
792
Duration :: from_millis ( 5000 ) ,
796
- container . compile ( new_compile_request ( ) ) ,
793
+ coordinator . compile ( new_compile_request ( ) ) ,
797
794
)
798
795
. await
799
796
. expect ( "Failed to receive streaming from container in time" )
800
797
. unwrap ( ) ;
801
798
802
799
assert ! ( response. success) ;
803
800
804
- container . shutdown ( ) . await ?;
801
+ coordinator . shutdown ( ) . await ?;
805
802
806
803
Ok ( ( ) )
807
804
}
808
805
809
806
#[ tokio:: test]
810
807
#[ snafu:: report]
811
808
async fn test_compile_streaming ( ) -> Result < ( ) > {
812
- let mut coordinator = new_coordinator ( ) ;
809
+ let coordinator = new_coordinator ( ) ? ;
813
810
814
- let container = coordinator. allocate ( ) ?;
815
811
let ActiveCompilation {
816
812
task,
817
813
stdout_rx,
818
814
stderr_rx,
819
- } = container
815
+ } = coordinator
820
816
. begin_compile ( new_compile_request ( ) )
821
817
. await
822
818
. unwrap ( ) ;
@@ -841,7 +837,7 @@ mod tests {
841
837
) ;
842
838
assert ! ( stderr. contains( "Finished" ) , "Missing `Finished`: {stderr}" ) ;
843
839
844
- container . shutdown ( ) . await ?;
840
+ coordinator . shutdown ( ) . await ?;
845
841
846
842
Ok ( ( ) )
847
843
}
0 commit comments