Skip to content

Commit f668188

Browse files
authored
refactor: move config vars to env (#5)
* refactor: move config vars to env * fix: build workflow
1 parent 214f6fb commit f668188

File tree

11 files changed

+60
-38
lines changed

11 files changed

+60
-38
lines changed

.github/workflows/rust.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ jobs:
2222
with:
2323
toolchain: nightly
2424
override: true
25+
- name: Copy config
26+
run: cp config.example.toml config.toml
2527
- name: Build
2628
run: cargo build --verbose
2729
- name: Run tests

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ Cargo.lock
33
*.o
44
driver.log
55
codecharacter-driver-2022
6+
config.toml

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
1. Install [Rust](https://www.rust-lang.org/tools/install)
44

55
2. Switch to Rust Nightly
6+
67
```
78
rustup install nightly && rustup default nightly
89
```
@@ -25,7 +26,11 @@
2526
git submodule update --init
2627
```
2728

28-
3. Run
29+
3. Create config files
30+
```
31+
cp config.example.toml config.toml
32+
```
33+
4. Run
2934
```
3035
cargo run
3136
```

config.example.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[env]
2+
SIMULATOR_IMAGE=ghcr.io/delta/codecharacter-simulator:latest
3+
CPP_COMPILER_IMAGE=ghcr.io/delta/codecharacter-cpp-compiler:latest
4+
CPP_RUNNER_IMAGE=ghcr.io/delta/codecharacter-cpp-runner:latest
5+
JAVA_COMPILER_IMAGE=ghcr.io/delta/codecharacter-java-compiler:latest
6+
JAVA_RUNNER_IMAGE=ghcr.io/delta/codecharacter-java-runner:latest
7+
PYTHON_RUNNER_IMAGE=ghcr.io/delta/codecharacter-python-runner:latest
8+
9+
MAX_LOG_SIZE=200000
10+
COMPILATION_TIME_LIMIT=5
11+
RUNTIME_TIME_LIMIT=10
12+
COMPILATION_MEMORY_LIMIT=300m
13+
RUNTIME_MEMORY_LIMIT=100m
14+
EPOLL_WAIT_TIMEOUT=30_000
15+
16+
RABBITMQ_HOST=amqp://guest:guest@localhost
17+
REQUEST_QUEUE=gameRequestQueue
18+
RESPONSE_QUEUE=gameStatusUpdateQueue

src/lib.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@ pub mod response;
1414
pub mod runner;
1515
pub mod utils;
1616

17-
// maximum size for log will be around 200KBs, everything after that is ignored
18-
const MAXLOGSIZE: usize = 200000;
19-
const COMPILATION_TIME_LIMIT: &str = "5";
20-
const RUNTIME_TIME_LIMIT: &str = "10";
21-
const COMPILATION_MEMORY_LIMIT: &str = "300m";
22-
const RUNTIME_MEMORY_LIMIT: &str = "100m";
23-
pub const EPOLL_WAIT_TIMEOUT: isize = 30_000;
24-
2517
fn get_turnwise_logs(player_log: String) -> HashMap<usize, Vec<String>> {
2618
let mut turnwise_logs = HashMap::new();
2719

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::sync::Arc;
2+
use std::env;
23

34
use cc_driver::{
45
create_error_response, create_executing_response,
@@ -12,8 +13,7 @@ use cc_driver::{
1213
},
1314
request::{GameRequest, Language},
1415
response::GameStatus,
15-
runner::{cpp, java, py, simulator, Runnable},
16-
EPOLL_WAIT_TIMEOUT,
16+
runner::{cpp, java, py, simulator, Runnable}
1717
};
1818
use log::{info, LevelFilter};
1919
use log4rs::{
@@ -29,7 +29,7 @@ use nix::sys::epoll::EpollFlags;
2929
fn handle_event(
3030
epoll_handle: &mut EpollGeneric<EpollEntryType>,
3131
) -> Result<Vec<Option<ProcessOutput>>, SimulatorError> {
32-
let events = epoll_handle.poll(EPOLL_WAIT_TIMEOUT, epoll_handle.get_registered_fds().len())?;
32+
let events = epoll_handle.poll(env::var("EPOLL_WAIT_TIMEOUT").unwrap().parse().unwrap(), epoll_handle.get_registered_fds().len())?;
3333
let mut res = vec![];
3434
for e in events {
3535
match epoll_handle.process_event(e)? {

src/poll/epoll_entry.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use nix::sys::epoll::EpollFlags;
22

33
use crate::error::EpollError;
4-
use crate::MAXLOGSIZE;
54

65
use std::io::Read;
76
use std::os::fd::AsRawFd;
87
use std::os::linux::process::ChildExt;
98
use std::process::ChildStderr;
9+
use std::env;
1010

1111
use crate::error::SimulatorError;
1212

@@ -90,7 +90,10 @@ impl ProcessOutput {
9090

9191
match self.process_type {
9292
ProcessType::Runner => {
93-
let limit = MAXLOGSIZE;
93+
let limit: usize = env::var("MAX_LOG_SIZE")
94+
.unwrap()
95+
.parse()
96+
.unwrap();
9497
let stderr = &mut self.stderr;
9598

9699
let _ = stderr

src/runner/cpp.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use std::{
22
fs::File,
33
os::linux::process::CommandExt,
44
process::{Child, Command, Stdio},
5+
env,
56
};
67

78
use crate::{
8-
error::SimulatorError, COMPILATION_MEMORY_LIMIT, COMPILATION_TIME_LIMIT, RUNTIME_MEMORY_LIMIT,
9-
RUNTIME_TIME_LIMIT,
9+
error::SimulatorError
1010
};
1111

1212
use super::Runnable;
@@ -30,11 +30,11 @@ impl Runnable for Runner {
3030
let compile = Command::new("docker")
3131
.args([
3232
"run",
33-
&format!("--memory={COMPILATION_MEMORY_LIMIT}"),
34-
&format!("--memory-swap={COMPILATION_MEMORY_LIMIT}"),
33+
&format!("--memory={}", env::var("COMPILATION_MEMORY_LIMIT").unwrap()),
34+
&format!("--memory-swap={}", env::var("COMPILATION_MEMORY_LIMIT").unwrap()),
3535
"--cpus=2",
3636
"--ulimit",
37-
&format!("cpu={COMPILATION_TIME_LIMIT}:{COMPILATION_TIME_LIMIT}"),
37+
&format!("cpu={}:{}", env::var("COMPILATION_TIME_LIMIT").unwrap(), env::var("COMPILATION_TIME_LIMIT").unwrap()),
3838
"--rm",
3939
"--name",
4040
&format!("{}_cpp_compiler", self.game_id),
@@ -68,11 +68,11 @@ impl Runnable for Runner {
6868
Command::new("docker")
6969
.args([
7070
"run",
71-
&format!("--memory={RUNTIME_MEMORY_LIMIT}"),
72-
&format!("--memory-swap={RUNTIME_MEMORY_LIMIT}"),
71+
&format!("--memory={}", env::var("RUNTIME_MEMORY_LIMIT").unwrap()),
72+
&format!("--memory-swap={}", env::var("RUNTIME_MEMORY_LIMIT").unwrap()),
7373
"--cpus=1",
7474
"--ulimit",
75-
&format!("cpu={RUNTIME_TIME_LIMIT}:{RUNTIME_TIME_LIMIT}"),
75+
&format!("cpu={}:{}", env::var("RUNTIME_TIME_LIMIT").unwrap(), env::var("RUNTIME_TIME_LIMIT").unwrap()),
7676
"--rm",
7777
"--name",
7878
&format!("{}_cpp_runner", self.game_id),

src/runner/java.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use std::{
22
fs::File,
33
os::linux::process::CommandExt,
44
process::{Child, Command, Stdio},
5+
env
56
};
67

78
use crate::{
8-
error::SimulatorError, COMPILATION_MEMORY_LIMIT, COMPILATION_TIME_LIMIT, RUNTIME_MEMORY_LIMIT,
9-
RUNTIME_TIME_LIMIT,
9+
error::SimulatorError
1010
};
1111

1212
use super::Runnable;
@@ -30,11 +30,11 @@ impl Runnable for Runner {
3030
let compile = Command::new("docker")
3131
.args([
3232
"run",
33-
&format!("--memory={COMPILATION_MEMORY_LIMIT}"),
34-
&format!("--memory-swap={COMPILATION_MEMORY_LIMIT}"),
33+
&format!("--memory={}", env::var("COMPILATION_MEMORY_LIMIT").unwrap()),
34+
&format!("--memory-swap={}", env::var("COMPILATION_MEMORY_LIMIT").unwrap()),
3535
"--cpus=1.5",
3636
"--ulimit",
37-
&format!("cpu={COMPILATION_TIME_LIMIT}:{COMPILATION_TIME_LIMIT}"),
37+
&format!("cpu={}:{}", env::var("COMPILATION_TIME_LIMIT").unwrap(), env::var("COMPILATION_TIME_LIMIT").unwrap()),
3838
"--rm",
3939
"--name",
4040
&format!("{}_java_compiler", self.game_id),
@@ -72,11 +72,11 @@ impl Runnable for Runner {
7272
Command::new("docker")
7373
.args([
7474
"run",
75-
&format!("--memory={RUNTIME_MEMORY_LIMIT}"),
76-
&format!("--memory-swap={RUNTIME_MEMORY_LIMIT}"),
75+
&format!("--memory={}", env::var("RUNTIME_MEMORY_LIMIT").unwrap()),
76+
&format!("--memory-swap={}", env::var("RUNTIME_MEMORY_LIMIT").unwrap()),
7777
"--cpus=1",
7878
"--ulimit",
79-
&format!("cpu={RUNTIME_TIME_LIMIT}:{RUNTIME_TIME_LIMIT}"),
79+
&format!("cpu={}:{}", env::var("RUNTIME_TIME_LIMIT").unwrap(), env::var("RUNTIME_TIME_LIMIT").unwrap()),
8080
"--rm",
8181
"--name",
8282
&format!("{}_java_runner", self.game_id),

src/runner/py.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use std::{
22
fs::File,
33
os::linux::process::CommandExt,
44
process::{Command, Stdio},
5+
env
56
};
67

7-
use crate::{error::SimulatorError, RUNTIME_MEMORY_LIMIT, RUNTIME_TIME_LIMIT};
8+
use crate::{error::SimulatorError};
89

910
use super::Runnable;
1011

@@ -27,11 +28,11 @@ impl Runnable for Runner {
2728
Command::new("docker")
2829
.args([
2930
"run",
30-
&format!("--memory={RUNTIME_MEMORY_LIMIT}"),
31-
&format!("--memory-swap={RUNTIME_MEMORY_LIMIT}"),
31+
&format!("--memory={}", env::var("RUNTIME_MEMORY_LIMIT").unwrap()),
32+
&format!("--memory-swap={}", env::var("RUNTIME_MEMORY_LIMIT").unwrap()),
3233
"--cpus=1",
3334
"--ulimit",
34-
&format!("cpu={RUNTIME_TIME_LIMIT}:{RUNTIME_TIME_LIMIT}"),
35+
&format!("cpu={}:{}", env::var("RUNTIME_TIME_LIMIT").unwrap(), env::var("RUNTIME_TIME_LIMIT").unwrap()),
3536
"--rm",
3637
"--name",
3738
&format!("{}_python_runner", self.game_id),

0 commit comments

Comments
 (0)