-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjava.rs
128 lines (120 loc) · 3.92 KB
/
java.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use std::{
env,
fs::File,
os::linux::process::CommandExt,
process::{Child, Command, Stdio},
};
use crate::error::SimulatorError;
use super::{GameType, Runnable};
pub struct Runner {
current_dir: String,
game_id: String,
player_dir: String,
}
impl Runner {
pub fn new(current_dir: String, game_id: String, player_dir: String) -> Self {
Runner {
current_dir,
game_id,
player_dir,
}
}
}
impl Runnable for Runner {
fn run(&self, stdin: File, stdout: File, game_type: GameType) -> Result<Child, SimulatorError> {
let compile = Command::new("docker")
.args([
"run",
&format!("--memory={}", env::var("COMPILATION_MEMORY_LIMIT").unwrap()),
&format!(
"--memory-swap={}",
env::var("COMPILATION_MEMORY_LIMIT").unwrap()
),
"--cpus=1.5",
"--ulimit",
&format!(
"cpu={}:{}",
env::var("COMPILATION_TIME_LIMIT").unwrap(),
env::var("COMPILATION_TIME_LIMIT").unwrap()
),
"--rm",
"--name",
&format!(
"{}_{}_java_compiler",
self.game_id,
self.player_dir.replace('/', "_")
),
"-v",
format!(
"{}/{}:/player_code",
self.current_dir.as_str(),
self.player_dir.as_str()
)
.as_str(),
&env::var("JAVA_COMPILER_IMAGE").unwrap(),
])
.current_dir(&self.current_dir)
.stdout(Stdio::null())
.stderr(Stdio::piped())
.spawn()
.map_err(|err| {
SimulatorError::UnidentifiedError(format!(
"Couldnt spawn compilation command: {err}"
))
})?;
let out = compile.wait_with_output().map_err(|err| {
SimulatorError::UnidentifiedError(format!(
"Unable to wait for compilation to finish, {err}"
))
})?;
if !out.status.success() {
let stderr = String::from_utf8(out.stderr).unwrap();
return Err(SimulatorError::CompilationError(stderr));
}
Command::new("docker")
.args([
"run",
&format!("--memory={}", env::var("RUNTIME_MEMORY_LIMIT").unwrap()),
&format!(
"--memory-swap={}",
env::var("RUNTIME_MEMORY_LIMIT").unwrap()
),
"--cpus=1",
"--ulimit",
&format!(
"cpu={}:{}",
env::var("RUNTIME_TIME_LIMIT").unwrap(),
env::var("RUNTIME_TIME_LIMIT").unwrap()
),
"--rm",
"--name",
&format!(
"{}_{}_java_runner",
self.game_id,
self.player_dir.replace('/', "_")
),
"-i",
"-v",
format!(
"{}/{}/run.jar:/run.jar",
self.current_dir.as_str(),
self.player_dir.as_str(),
)
.as_str(),
&env::var("JAVA_RUNNER_IMAGE").unwrap(),
"run.jar", //jar file name
&game_type.to_string(),
])
.create_pidfd(true)
.current_dir(&self.current_dir)
.stdin(stdin)
.stdout(stdout)
.stderr(Stdio::piped())
.spawn()
.map_err(|err| {
SimulatorError::UnidentifiedError(format!(
"Couldnt spawn the java runner process: {err}"
))
})
}
}