Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make dev more convient #155

Merged
merged 3 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
.github/
.vscode/
tmp/
test-collection/
target/
rclone/
postman/
docker/
.devcontainer/
target/
2 changes: 1 addition & 1 deletion .github/workflows/docker-publish-judger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
id: build-and-push
uses: docker/build-push-action@v5
with:
file: docker/judger.dockerfile
file: Dockerfile
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target/
Cargo.lock
scripts/thirdparty/
override.env
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"recommendations": [
"tamasfe.even-better-toml",
"rust-lang.rust-analyzer",
"mads-hartmann.bash-ide-vscode"
"mads-hartmann.bash-ide-vscode",
"mikestead.dotenv"
]
}
53 changes: 53 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Judger Serve (Debug)",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/target/debug/judger",
"args": [
"serve"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/judger/workdirs/development",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "build judger",
"setupCommands": [
{
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "Judger Judge (Debug)",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/target/debug/judger",
"args": [
"judge",
"--problem-slug", "hello_world",
"--language", "cpp",
"--src-path", "${workspaceFolder}/judger/workdirs/development/problem-package/hello_world/ans.cpp",
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/judger/workdirs/development",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "build judger",
"setupCommands": [
{
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
12 changes: 2 additions & 10 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
{
"material-icon-theme.folders.associations": {
"judge-core": "core",
"judge-cli": "command",
"judge-service": "helper",
"judge-server": "server",
"postman": "api"
},
"rust-analyzer.linkedProjects": [
"./judger/Cargo.toml",
"./judger/Cargo.toml",
"./judger/Cargo.toml"
]
"judger": "app"
}
}
13 changes: 13 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build judger",
"type": "shell",
"command": "cargo",
"args": ["build", "--bin", "judger"]
}
]
}
14 changes: 7 additions & 7 deletions docker/judger.dockerfile → Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM rust:latest as build

COPY judge-core /usr/src/judge-core
COPY judger /usr/src/judger
WORKDIR /usr/src/judger
COPY judge-core/ /usr/src/judge-core
COPY judger/ /usr/src/judger

WORKDIR /usr/src/judger
RUN apt update && apt install -y libseccomp-dev gcc
RUN cargo build --bin judger --release

Expand All @@ -15,11 +15,11 @@ COPY --from=build /usr/src/judger/target/release/judger /usr/local/bin/judger

RUN curl https://rclone.org/install.sh | bash

RUN mkdir /workspace
WORKDIR /workspace
COPY data/default-rclone.conf /workspace/data/default-rclone.conf
RUN mkdir /workspace/data/problem-package
RUN mkdir /workdir
RUN mkdir /workdir/problem-package
COPY judger/workdirs/docker/rclone.conf /workdir/rclone.conf

WORKDIR /workdir
ENV RUST_LOG=DEBUG
EXPOSE 8000
CMD [ "judger-server" ]
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
# Judger

![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/OJ-lab/judger/rust-check.yml?logo=github&label=Tests)
[![Discord](https://img.shields.io/discord/916955582181822486?label=Discord&color=blue&logo=discord&logoColor=white)](https://discord.gg/vh8NCgdp8J)
![Codespace Supported](https://img.shields.io/badge/Codespace_Supported-000000?style=flat&logo=github)

Judger is supposed to be a simple **sandbox service** which works for online-judge systems.
Library & application which supports running code in a sandboxed environment.

## System
## System Prerequisite

judger-rs currently use `nix` to make necessary system invoke like `fork()`.
Judger currently use `nix` to make necessary system invoke like `fork()`.
So you might need to check whether you are using the supported system from the main-page of [nix](https://github.com/nix-rust/nix).

**Briefly speaking, judger-rs is now supposing you are decided to run it on linux.**
We'll consider other platform, but in a lower priority.

## Contribute
## Development

We have a guide in judger's [WIKI](https://github.com/OJ-lab/judger/wiki/Contribution-Guide)
Judger should works fine in VSCode with all recommended extensions installed.

### Before you start

You may need to setup your environment before you start.
There is a setup script to help you quickly get ready.

> 🥰 You won't need to run this script if you are using GitHub Codespaces.

```sh
./scripts/env_setup.bash
```

### Debugging

Launch settings are already configured in `.vscode/launch.json`, try in the debug panel.

## Deeper Docs

Expand Down
1 change: 0 additions & 1 deletion data/.gitignore

This file was deleted.

47 changes: 0 additions & 47 deletions data/README.md

This file was deleted.

1 change: 0 additions & 1 deletion data/dev-problem-package/hello-world/.timelimit

This file was deleted.

9 changes: 0 additions & 9 deletions docker/README.md

This file was deleted.

2 changes: 0 additions & 2 deletions judge-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ See what we've got (or plan to do) currently:
- a **sandbox** mainly based on `rlimit` and `seccomp`, helps you to spawn process safely
- a **monitor** (or judger) with sandboxes,
enables you to run single part of judge test_case (if you got everything needed for judge)
- a **judge_builder** (WIP) to provide a higher level interface to start a judge,
supposing the judge directory structure is following [ICPC Problem Package format](https://icpc.io/problem-package-format/examples/directory_structure)

## Use strace for seccomp withlist dev

Expand Down
4 changes: 0 additions & 4 deletions judger/.env.development

This file was deleted.

2 changes: 1 addition & 1 deletion judger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ judge-core = { path = "../judge-core" }
clap = { version = "4.0", features = ["derive"] }

# Client
reqwest = { version = "0.12.1", features = ["json"] }
reqwest = { version = "0.12", features = ["json"] }

# Async runtime
tokio = { version = "1", features = ["full"] }
Expand Down
30 changes: 8 additions & 22 deletions judger/README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
# Judger
# Judger Application

## Cli
Judge server & cmd-line application built with `judger-core`.
the two mode are base on the same core logic, cmd-line mode is more convenient for debugging and testing.

This will be the easiest and closest way to try out the basic feature of Judger.
In server mode, it fetches the judge tasks from [oj-lab-platform](https://github.com/OJ-lab/oj-lab-platform)
and reports the result back.

### Develop Usage
In cmd-line mode, by providing the necessary arguments, it can run the code in a sandboxed environment.

To run from the source code, try:
## How to use

``` shell
cargo run --bin judger-cli -- [COMMAND]
```

``` shell
cargo run --bin judger-cli -- batch-judge --help
```

## Server

### How to run

`cargo run --bin judger-server -- --env-path ./judger/src/server/environment/.env.development`

### How to visit OpenAPI

visit `{HOST}/swagger-ui/`
Run `cargo run --bin judger` to get help.
11 changes: 7 additions & 4 deletions judger/src/agent/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl PlatformClient {
}
}

#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, Clone)]
pub struct JudgeTask {
#[serde(rename = "submissionUID")]
pub submission_uid: String,
Expand All @@ -46,15 +46,18 @@ struct PickTaskResponse {
}

async fn pick_task(client: &HttpClient) -> Result<JudgeTask, anyhow::Error> {
let pick_url = "/api/v1/judge/task/pick";
let pick_url = "api/v1/judge/task/pick";
let body = PickTaskBody {
consumer: "".to_string(),
};
let response = client.post(pick_url.to_string()).json(&body).send().await?;

match response.status() {
reqwest::StatusCode::OK => Ok(response.json::<PickTaskResponse>().await?.task),
_ => Err(anyhow::anyhow!("Queue is empty")),
_ => {
log::error!("Failed to pick task: {:?}", response);
Err(anyhow::anyhow!("Queue is empty"))
}
}
}

Expand All @@ -74,7 +77,7 @@ async fn report_task(
stream_id: &str,
results: Vec<JudgeResultInfo>,
) -> Result<(), anyhow::Error> {
let report_url = "/api/v1/judge/task/report";
let report_url = "api/v1/judge/task/report";
let body = ReportTaskBody {
consumer: "".to_string(),
stream_id: stream_id.to_owned(),
Expand Down
25 changes: 17 additions & 8 deletions judger/src/agent/rclone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,35 @@ impl RcloneClient {
}

pub fn is_avaliable(&self) -> bool {
let status = Command::new("rclone")
let mut binding = Command::new("rclone");
let command = binding
.arg("--config")
.arg(format!("{}", self.config_path.to_string_lossy()))
.arg("--contimeout")
.arg("5s")
// HTTP request is a typical low level operation, which will retry for many times
// https://rclone.org/docs/#low-level-retries-number
// Here we set it to 3 to avoid long waiting time
.arg("--low-level-retries")
.arg("3")
.arg("ls")
.arg("minio:")
.status()
.expect("Failed to rclone");
.arg("minio:");
log::debug!("Checking rclone with command: {:?}", command);
let status = command.status().expect("Failed to rclone");

status.success()
}

pub fn sync_bucket(&self, bucket_name: &str, target_dir: &Path) -> Result<(), Error> {
let status = Command::new("rclone")
let mut binding = Command::new("rclone");
let command = binding
.arg("--config")
.arg(format!("{}", self.config_path.to_string_lossy()))
.arg("sync")
.arg(format!("minio:{}", bucket_name))
.arg(format!("{}", target_dir.to_string_lossy()))
.status()
.expect("Failed to rclone");
.arg(format!("{}", target_dir.to_string_lossy()));
log::debug!("Syncing bucket with command: {:?}", command);
let status = command.status().expect("Failed to rclone");
if status.success() {
Ok(())
} else {
Expand Down
Loading