@@ -11,28 +11,29 @@ use crate::{parse_channel, parse_crate_type, parse_edition, parse_mode, parse_ta
11
11
12
12
use worker_message:: { CoordinatorMessage , Job , JobReport , WorkerMessage } ;
13
13
14
+ use lazy_static:: lazy_static;
14
15
use snafu:: prelude:: * ;
15
16
use tokio:: io:: { AsyncReadExt , AsyncWriteExt , BufReader } ;
16
17
use tokio:: process:: { ChildStdin , ChildStdout , Command } ;
17
18
use tokio:: sync:: { mpsc, oneshot} ;
18
19
use tokio:: task:: { JoinHandle , JoinSet } ;
19
- use lazy_static:: lazy_static;
20
20
21
21
pub type Container = ( mpsc:: Sender < String > , mpsc:: Receiver < String > ) ;
22
22
23
23
lazy_static ! {
24
- pub ( crate ) static ref COORDINATOR : Arc <Mutex <Coordinator >> = Arc :: new( Mutex :: new( Coordinator :: new( ) ) ) ;
24
+ pub ( crate ) static ref COORDINATOR : Arc <Mutex <Coordinator >> =
25
+ Arc :: new( Mutex :: new( Coordinator :: new( ) ) ) ;
25
26
}
26
27
27
28
#[ derive( Debug ) ]
28
29
pub struct Coordinator {
29
- free_containers : VecDeque < Container >
30
+ free_containers : VecDeque < Container > ,
30
31
}
31
32
32
33
impl Coordinator {
33
34
pub fn new ( ) -> Self {
34
35
Coordinator {
35
- free_containers : VecDeque :: new ( )
36
+ free_containers : VecDeque :: new ( ) ,
36
37
}
37
38
}
38
39
@@ -47,7 +48,7 @@ impl Coordinator {
47
48
pub fn recycle ( & mut self , container : Container ) -> bool {
48
49
// Seems not rigorous.
49
50
let closed = container. 0 . is_closed ( ) ;
50
- if !closed {
51
+ if !closed {
51
52
self . free_containers . push_back ( container) ;
52
53
}
53
54
@@ -56,7 +57,6 @@ impl Coordinator {
56
57
}
57
58
}
58
59
59
-
60
60
#[ derive( Debug , Snafu ) ]
61
61
pub enum Error {
62
62
#[ snafu( display( "Reached system process limit" ) ) ]
@@ -690,16 +690,16 @@ fn basic_secure_docker_command() -> Command {
690
690
)
691
691
}
692
692
693
-
694
693
fn run_worker_in_background ( ) -> Result < ( ChildStdin , ChildStdout ) > {
695
694
// For local development.
696
- // const WORKER_FILEPATH: &str = "../worker-message/target/debug /worker";
695
+ // const WORKER_FILEPATH: &str = "../worker-message/target/release /worker";
697
696
698
697
// No need to track Child.
699
698
// We know whether it exits via channels.
700
699
// let mut child = Command::new(WORKER_FILEPATH)
701
700
let mut child = basic_secure_docker_command ( )
702
701
. arg ( "-i" )
702
+ . args ( [ "-a" , "stdin" , "-a" , "stdout" , "-a" , "stderr" ] )
703
703
. arg ( "adwinw/rust-playground-worker" )
704
704
. stdin ( Stdio :: piped ( ) )
705
705
. stdout ( Stdio :: piped ( ) )
@@ -836,7 +836,7 @@ async fn pair_websocket_worker(
836
836
WorkerMessage :: StdoutPacket ( packet) => {
837
837
let Ok ( _) = ws_sender. send(
838
838
serde_json:: to_string( & PlaygroundMessage :: Stdout { packet} ) . context( PlaygroundMessageSerializationSnafu ) ?) . await
839
- . context( UnableToSendPlaygroundMessageToWebSocketSnafu )
839
+ . context( UnableToSendPlaygroundMessageToWebSocketSnafu )
840
840
else {
841
841
// TODO: log the failure.
842
842
continue ;
@@ -845,7 +845,7 @@ async fn pair_websocket_worker(
845
845
WorkerMessage :: StderrPacket ( packet) => {
846
846
let Ok ( _) = ws_sender. send(
847
847
serde_json:: to_string( & PlaygroundMessage :: Stderr { packet} ) . context( PlaygroundMessageSerializationSnafu ) ?) . await
848
- . context( UnableToSendPlaygroundMessageToWebSocketSnafu )
848
+ . context( UnableToSendPlaygroundMessageToWebSocketSnafu )
849
849
else {
850
850
// TODO: log the failure.
851
851
continue ;
@@ -854,7 +854,7 @@ async fn pair_websocket_worker(
854
854
WorkerMessage :: StreamFailure => {
855
855
let Ok ( _) = ws_sender. send(
856
856
serde_json:: to_string( & PlaygroundMessage :: StreamFailure ) . context( PlaygroundMessageSerializationSnafu ) ?) . await
857
- . context( UnableToSendPlaygroundMessageToWebSocketSnafu )
857
+ . context( UnableToSendPlaygroundMessageToWebSocketSnafu )
858
858
else {
859
859
// TODO: log the failure.
860
860
continue ;
@@ -896,12 +896,11 @@ pub fn spawn_worker() -> Result<Container> {
896
896
#[ cfg( test) ]
897
897
mod tests {
898
898
899
- use crate :: coordinator:: { PlaygroundMessage , WSMessage , WSRequest , WSResponse , spawn_worker } ;
899
+ use crate :: coordinator:: { spawn_worker , PlaygroundMessage , WSMessage , WSRequest , WSResponse } ;
900
900
use std:: sync:: atomic:: AtomicU64 ;
901
901
use std:: sync:: Arc ;
902
902
use std:: time:: Duration ;
903
903
904
-
905
904
use serde_json:: json;
906
905
use tokio:: time:: error:: Elapsed ;
907
906
@@ -1023,7 +1022,7 @@ mod tests {
1023
1022
let mut client = MockClient :: new ( ) ;
1024
1023
let compile_resp = check_websocket_request_response (
1025
1024
WSRequest :: Compile ( client. new_compile_request ( ) ) ,
1026
- 5000 ,
1025
+ 15000 ,
1027
1026
)
1028
1027
. await
1029
1028
. expect ( "Failed to receive response from coordinator in time" ) ;
@@ -1038,7 +1037,7 @@ mod tests {
1038
1037
async fn format_request ( ) {
1039
1038
let mut client = MockClient :: new ( ) ;
1040
1039
let compile_resp =
1041
- check_websocket_request_response ( WSRequest :: Format ( client. new_format_request ( ) ) , 5000 )
1040
+ check_websocket_request_response ( WSRequest :: Format ( client. new_format_request ( ) ) , 15000 )
1042
1041
. await
1043
1042
. expect ( "Failed to receive response from coordinator in time" ) ;
1044
1043
if let WSResponse :: Format ( resp) = compile_resp {
@@ -1064,7 +1063,7 @@ mod tests {
1064
1063
. expect ( "Failed to send request to coordinator" ) ;
1065
1064
1066
1065
let user_input = "Hello Playground!\n " ;
1067
- tokio:: time:: timeout ( Duration :: from_millis ( 5000 ) , async move {
1066
+ tokio:: time:: timeout ( Duration :: from_millis ( 15000 ) , async move {
1068
1067
// Check execute response and send user input.
1069
1068
if let Some ( msg) = client_receiver. recv ( ) . await {
1070
1069
let playground_msg = serde_json:: from_str ( & msg)
@@ -1091,7 +1090,7 @@ mod tests {
1091
1090
match playground_msg {
1092
1091
PlaygroundMessage :: Stdout { packet } => {
1093
1092
assert ! (
1094
- packet == user_input,
1093
+ dbg! ( packet) == dbg! ( user_input) ,
1095
1094
"Running program has incorrect standard output."
1096
1095
) ;
1097
1096
break
@@ -1124,7 +1123,7 @@ mod tests {
1124
1123
)
1125
1124
. await
1126
1125
. expect ( "Failed to send request to coordinator" ) ;
1127
- tokio:: time:: timeout ( Duration :: from_millis ( 5000 ) , async move {
1126
+ tokio:: time:: timeout ( Duration :: from_millis ( 15000 ) , async move {
1128
1127
// Check format response and send compilation request.
1129
1128
if let Some ( msg) = client_receiver. recv ( ) . await {
1130
1129
let playground_msg = serde_json:: from_str ( & msg)
@@ -1190,7 +1189,7 @@ mod tests {
1190
1189
. await
1191
1190
. expect ( "Failed to send request to coordinator" ) ;
1192
1191
let latest_seqno = 1 ;
1193
- tokio:: time:: timeout ( Duration :: from_millis ( 5000 ) , async move {
1192
+ tokio:: time:: timeout ( Duration :: from_millis ( 15000 ) , async move {
1194
1193
loop {
1195
1194
let server_msg = client_receiver. recv ( ) . await ;
1196
1195
match server_msg {
@@ -1248,7 +1247,7 @@ mod tests {
1248
1247
. await
1249
1248
. expect ( "Failed to send request to coordinator" ) ;
1250
1249
let latest_seqno = 1 ;
1251
- tokio:: time:: timeout ( Duration :: from_millis ( 5000 ) , async move {
1250
+ tokio:: time:: timeout ( Duration :: from_millis ( 15000 ) , async move {
1252
1251
loop {
1253
1252
let server_msg = client_receiver. recv ( ) . await ;
1254
1253
match server_msg {
0 commit comments