diff --git a/api/src/handlers/compile.rs b/api/src/handlers/compile.rs index 10305efd..8f9c256e 100644 --- a/api/src/handlers/compile.rs +++ b/api/src/handlers/compile.rs @@ -2,15 +2,14 @@ use crate::errors::{ApiError, Result}; use crate::handlers::process::{do_process_command, fetch_process_result}; use crate::handlers::types::{ApiCommand, ApiCommandResult}; use crate::handlers::types::{FileContentMap, ScarbCompileResponse}; -use crate::handlers::utils::{do_metered_action, get_files_recursive, init_directories, AutoCleanUp}; +use crate::handlers::utils::{get_files_recursive, init_directories, AutoCleanUp}; use crate::handlers::{STATUS_COMPILATION_FAILED, STATUS_SUCCESS, STATUS_UNKNOWN_ERROR}; -use crate::metrics::{Metrics, COMPILATION_LABEL_VALUE}; +use crate::metrics::Metrics; use crate::rate_limiter::RateLimited; use crate::worker::WorkerEngine; use rocket::serde::json; use rocket::serde::json::Json; use rocket::{tokio, State}; -use serde::{Deserialize, Serialize}; use std::path::PathBuf; use std::process::{Command, Stdio}; use tracing::instrument; @@ -37,7 +36,12 @@ pub async fn compile_async( engine: &State, ) -> String { tracing::info!("/compile/{:?}", request_json.0.file_names()); - do_process_command(ApiCommand::Compile { compilation_request: request_json.0 }, engine) + do_process_command( + ApiCommand::Compile { + compilation_request: request_json.0, + }, + engine, + ) } #[instrument(skip(engine))] @@ -54,12 +58,19 @@ pub async fn get_compile_result(process_id: &str, engine: &State) }) } -async fn ensure_scarb_toml(mut compilation_request: CompilationRequest) -> Result { - +async fn ensure_scarb_toml( + mut compilation_request: CompilationRequest, +) -> Result { // Check if Scarb.toml exists in the root if !compilation_request.has_scarb_toml() { // number of files cairo files in the request - if compilation_request.files.iter().filter(|f| f.file_name.ends_with(".cairo")).count() != 1 { + if compilation_request + .files + .iter() + .filter(|f| f.file_name.ends_with(".cairo")) + .count() + != 1 + { return Err(ApiError::InvalidRequest); } @@ -70,7 +81,11 @@ async fn ensure_scarb_toml(mut compilation_request: CompilationRequest) -> Resul }); // change the name of the file to the first cairo file to src/lib.cairo - let first_cairo_file = compilation_request.files.iter_mut().find(|f| f.file_name.ends_with(".cairo")).unwrap(); + let first_cairo_file = compilation_request + .files + .iter_mut() + .find(|f| f.file_name.ends_with(".cairo")) + .unwrap(); first_cairo_file.file_name = "src/lib.cairo".to_string(); } @@ -99,7 +114,6 @@ pub async fn do_compile( dirs: vec![&temp_dir], }; - let mut compile = Command::new("scarb"); compile .current_dir(&temp_dir) @@ -109,18 +123,15 @@ pub async fn do_compile( tracing::debug!("Executing scarb command: {:?}", compile); - let result = tokio::time::timeout( - std::time::Duration::from_secs(300), - async { - compile - .spawn() - .map_err(ApiError::FailedToExecuteCommand)? - .wait_with_output() - .map_err(ApiError::FailedToReadOutput) - }, - ) - .await - .map_err(|_| ApiError::CompilationTimeout)??; + let result = tokio::time::timeout(std::time::Duration::from_secs(300), async { + compile + .spawn() + .map_err(ApiError::FailedToExecuteCommand)? + .wait_with_output() + .map_err(ApiError::FailedToReadOutput) + }) + .await + .map_err(|_| ApiError::CompilationTimeout)??; let file_content_map_array = get_files_recursive(&PathBuf::from(&temp_dir).join("target/dev"))?; @@ -136,7 +147,7 @@ pub async fn do_compile( Some(_) => STATUS_COMPILATION_FAILED, None => STATUS_UNKNOWN_ERROR, } - .to_string(); + .to_string(); auto_clean_up.clean_up().await; @@ -146,4 +157,3 @@ pub async fn do_compile( status, })) } - diff --git a/api/src/handlers/mod.rs b/api/src/handlers/mod.rs index 2efbe4a1..6569849d 100644 --- a/api/src/handlers/mod.rs +++ b/api/src/handlers/mod.rs @@ -1,9 +1,9 @@ -pub mod scarb_version; +pub mod compile; pub mod process; pub mod scarb_test; +pub mod scarb_version; pub mod types; pub mod utils; -pub mod compile; use tracing::info; use tracing::instrument; diff --git a/api/src/handlers/process.rs b/api/src/handlers/process.rs index 9a338714..cf11e584 100644 --- a/api/src/handlers/process.rs +++ b/api/src/handlers/process.rs @@ -47,16 +47,12 @@ pub fn do_process_command(command: ApiCommand, engine: &State) -> } } -pub fn fetch_process_result( - process_id: &str, - engine: &State, - do_work: F, -) -> String +pub fn fetch_process_result(process_id: &str, engine: &State, do_work: F) -> String where F: FnOnce(Result<&ApiCommandResult, &ApiError>) -> String, { // get status of process by ID - match Uuid::parse_str(&process_id) { + match Uuid::parse_str(process_id) { Ok(process_uuid) => { if engine.arc_process_states.contains_key(&process_uuid) { match engine diff --git a/api/src/handlers/scarb_test.rs b/api/src/handlers/scarb_test.rs index 1a3f2ac6..6b06228b 100644 --- a/api/src/handlers/scarb_test.rs +++ b/api/src/handlers/scarb_test.rs @@ -71,21 +71,21 @@ pub async fn do_scarb_test(remix_file_path: PathBuf) -> Result "Success", Some(_) => "SierraCompilationFailed", None => "UnknownError", } - .to_string(); + .to_string(); Ok(Json(ScarbTestResponse { message, status })) } diff --git a/api/src/handlers/scarb_version.rs b/api/src/handlers/scarb_version.rs index 0e6dc57a..e92af5c8 100644 --- a/api/src/handlers/scarb_version.rs +++ b/api/src/handlers/scarb_version.rs @@ -1,6 +1,4 @@ -use rocket::tokio::fs::read_dir; use rocket::State; -use std::path::Path; use std::process::{Command, Stdio}; use tracing::{error, info, instrument}; @@ -8,7 +6,6 @@ use crate::errors::{ApiError, Result}; use crate::handlers::process::{do_process_command, fetch_process_result}; use crate::handlers::types::{ApiCommand, ApiCommandResult}; use crate::rate_limiter::RateLimited; -use crate::utils::lib::{CAIRO_COMPILERS_DIR, DEFAULT_CAIRO_DIR}; use crate::worker::WorkerEngine; #[instrument(skip(engine, _rate_limited))] @@ -36,14 +33,16 @@ pub async fn get_scarb_version_result(process_id: &str, engine: &State Result { - let mut version_caller = Command::new("scarb") + let version_caller = Command::new("scarb") .arg("--version") .stdout(Stdio::piped()) .stderr(Stdio::piped()) .spawn() .map_err(ApiError::FailedToExecuteCommand)?; - let output = version_caller.wait_with_output().map_err(ApiError::FailedToReadOutput)?; + let output = version_caller + .wait_with_output() + .map_err(ApiError::FailedToReadOutput)?; if output.status.success() { let result = String::from_utf8_lossy(&output.stdout).to_string(); diff --git a/api/src/handlers/utils.rs b/api/src/handlers/utils.rs index d088c7fc..d10a52ee 100644 --- a/api/src/handlers/utils.rs +++ b/api/src/handlers/utils.rs @@ -9,7 +9,12 @@ use crate::errors::{ApiError, Result}; use crate::metrics::{Metrics, COMPILATION_LABEL_VALUE}; use super::types::{CompilationRequest, FileContentMap, Successful}; -use super::{compile::do_compile, scarb_test::do_scarb_test, scarb_version::do_cairo_version, types::{ApiCommand, ApiCommandResult}}; +use super::{ + compile::do_compile, + scarb_test::do_scarb_test, + scarb_version::do_cairo_version, + types::{ApiCommand, ApiCommandResult}, +}; #[instrument] #[post("/on-plugin-launched")] @@ -18,7 +23,7 @@ pub async fn on_plugin_launched() { } pub(crate) async fn do_metered_action( - action: impl Future>>, + action: impl Future>>, action_label_value: &str, metrics: &Metrics, ) -> Result> { @@ -101,7 +106,6 @@ impl AutoCleanUp<'_> { } } - pub async fn dispatch_command(command: ApiCommand, metrics: &Metrics) -> Result { match command { ApiCommand::ScarbVersion => match do_cairo_version() { @@ -113,12 +117,14 @@ pub async fn dispatch_command(command: ApiCommand, metrics: &Metrics) -> Result< Ok(result) => Ok(ApiCommandResult::ScarbTest(result.into_inner())), Err(e) => Err(e), }, - ApiCommand::Compile { compilation_request } => match do_metered_action( - do_compile(compilation_request, &metrics), + ApiCommand::Compile { + compilation_request, + } => match do_metered_action( + do_compile(compilation_request, metrics), COMPILATION_LABEL_VALUE, metrics, ) - .await + .await { Ok(result) => Ok(ApiCommandResult::Compile(result.into_inner())), Err(e) => Err(e), @@ -139,7 +145,10 @@ pub async fn create_temp_dir() -> Result { } pub async fn init_directories(compilation_request: CompilationRequest) -> Result { - println!("init_directories, compilation_request: {:?}", compilation_request); + println!( + "init_directories, compilation_request: {:?}", + compilation_request + ); let temp_dir = create_temp_dir().await?; @@ -160,11 +169,16 @@ pub async fn init_directories(compilation_request: CompilationRequest) -> Result println!("init_directories, temp_dir: {:?}", temp_dir); // check the path content - println!("init_directories, temp_dir content: {:?}", tokio::fs::read_dir(&temp_dir).await); + println!( + "init_directories, temp_dir content: {:?}", + tokio::fs::read_dir(&temp_dir).await + ); temp_dir .to_str() - .ok_or_else(|| ApiError::FailedToInitializeDirectories("Failed to convert path to string".to_string())) + .ok_or_else(|| { + ApiError::FailedToInitializeDirectories("Failed to convert path to string".to_string()) + }) .map(|s| s.to_string()) } diff --git a/api/src/main.rs b/api/src/main.rs index c9f91200..ea6ecaa2 100644 --- a/api/src/main.rs +++ b/api/src/main.rs @@ -14,9 +14,7 @@ use anyhow::Context; use handlers::compile::{compile_async, get_compile_result}; use handlers::process::get_process_status; use handlers::scarb_test::{get_scarb_test_result, scarb_test_async}; -use handlers::scarb_version::{ - get_scarb_version_result, scarb_version_async, -}; +use handlers::scarb_version::{get_scarb_version_result, scarb_version_async}; use handlers::utils::on_plugin_launched; use handlers::{health, who_is_this}; use prometheus::Registry; diff --git a/api/src/worker.rs b/api/src/worker.rs index fa93f15e..a78f9a12 100644 --- a/api/src/worker.rs +++ b/api/src/worker.rs @@ -108,7 +108,7 @@ impl WorkerEngine { arc_process_states, process_timestamps_to_purge, ) - .await; + .await; }))); } } @@ -125,7 +125,7 @@ impl WorkerEngine { arc_process_states, process_timestamps_to_purge, ) - .await; + .await; }))); } @@ -213,7 +213,7 @@ impl WorkerEngine { match dispatch_command(command, &metrics).await { Ok(result) => { println!("Command completed: {:?}", result); - + arc_process_states .insert(process_id, ProcessState::Completed(result)); @@ -229,7 +229,6 @@ impl WorkerEngine { arc_process_states.insert(process_id, ProcessState::Error(e)); - arc_timestamps_to_purge .push(( process_id,