Skip to content

Commit

Permalink
Refactor judger code
Browse files Browse the repository at this point in the history
  • Loading branch information
slhmy committed Mar 23, 2024
1 parent 3c512b5 commit e23c9ee
Show file tree
Hide file tree
Showing 23 changed files with 283 additions and 619 deletions.
File renamed without changes.
10 changes: 1 addition & 9 deletions judger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,4 @@ chrono = { version = "0.4", features = ["serde"] }
anyhow = "1"
thiserror = "1"

uuid = { version = "1.4", features = ["serde", "v4"] }

[[bin]]
name ="judger-cli"
path ="src/cli/main.rs"

[[bin]]
name ="judger-server"
path ="src/server/main.rs"
uuid = { version = "1.4", features = ["serde", "v4"] }
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ impl HttpClient {
let client = reqwest::Client::new();
Self { client, base_url }
}
pub fn _get(&self, path: String) -> reqwest::RequestBuilder {
self.client.get(format!("{}{}", self.base_url, path))
}

pub fn post(&self, path: String) -> reqwest::RequestBuilder {
self.client.post(format!("{}{}", self.base_url, path))
}
Expand Down
2 changes: 2 additions & 0 deletions judger/src/agent/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod http;
pub mod rclone;
41 changes: 41 additions & 0 deletions judger/src/agent/rclone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use anyhow::{self, Error};
use std::path::PathBuf;
use std::process::Command;

pub struct RcloneClient {
config_path: PathBuf,
}

impl RcloneClient {
pub fn new(config_path: PathBuf) -> Self {
Self { config_path }
}

pub fn is_avaliable(&self) -> bool {
let status = Command::new("rclone")
.arg("--config")
.arg(format!("{}", self.config_path.to_string_lossy()))
.arg("ls")
.arg("minio:")
.status()
.expect("Failed to rclone");

status.success()
}

pub fn sync_bucket(&self, bucket_name: &str, target_dir: PathBuf) -> Result<(), Error> {
let status = Command::new("rclone")
.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");
if status.success() {
Ok(())
} else {
Err(anyhow::anyhow!("rclone sync failed, please check config."))
}
}
}
108 changes: 0 additions & 108 deletions judger/src/cli/main.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub struct JudgeServerOpt {
#[structopt(long, default_value = "data/dev-problem-package")]
pub problem_package_dir: PathBuf,

#[structopt(long, default_value = "data/rclone.conf")]
pub rclone_config: PathBuf,

#[structopt(env = "BASE_URL", default_value = "http://localhost:8080/api/v1/judge")]
pub base_url: String,

Expand Down
2 changes: 0 additions & 2 deletions judger/src/server/error.rs → judger/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use actix_web::{HttpResponse, ResponseError};
use judge_core::error::JudgeCoreError;
use judger::service::error::JudgeServiceError;

#[derive(Debug, thiserror::Error)]
pub enum ServiceError {
Expand All @@ -23,7 +22,6 @@ pub enum ServiceError {
#[derive(Debug)]
pub enum ClientError {
InternalError(anyhow::Error),
PackageError(JudgeServiceError),
}

#[derive(Serialize)]
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{fs, path::PathBuf};

use crate::{error::ServiceError, service::state};
use crate::{error::ServiceError, handler::state};
use actix_web::{post, web, HttpResponse};

use judge_core::{
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion judger/src/lib.rs

This file was deleted.

33 changes: 28 additions & 5 deletions judger/src/server/main.rs → judger/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
mod client;
mod agent;
mod environment;
mod error;
mod service;
mod handler;
mod worker;

#[macro_use]
extern crate serde_derive;
Expand All @@ -23,8 +24,30 @@ async fn main() -> std::io::Result<()> {
// }
// });

let new_worker_result = worker::JudgeWorker::new(
opt.base_url,
opt.interval as u64,
opt.rclone_config,
opt.problem_package_dir.clone(),
);

let worker = match new_worker_result {
Ok(maybe_worker) => {
if let Some(worker) = maybe_worker {
worker
} else {
log::error!("Failed to create worker");
return Ok(());
}
}
Err(e) => {
log::error!("Failed to create worker: {:?}", e);
return Ok(());
}
};

tokio::spawn(async move {
client::run_client(opt.base_url, opt.interval as u64).await;
worker.run().await;
});

let port = opt.port;
Expand All @@ -33,11 +56,11 @@ async fn main() -> std::io::Result<()> {
App::new()
.wrap(actix_web::middleware::Logger::default())
.app_data(Data::new(opt.problem_package_dir.clone()))
.configure(service::route)
.configure(handler::route)
.service(
utoipa_swagger_ui::SwaggerUi::new("/swagger-ui/{_:.*}").urls(vec![(
utoipa_swagger_ui::Url::new("api", "/api-docs/openapi.json"),
service::ApiDoc::openapi(),
handler::ApiDoc::openapi(),
)]),
)
})
Expand Down
Loading

0 comments on commit e23c9ee

Please sign in to comment.