outsource cancel to method and handle errors
This commit is contained in:
parent
114a34de8f
commit
f6af87dc27
@ -19,6 +19,7 @@ pub fn build_api() -> Vec<Route> {
|
||||
stats,
|
||||
get_build,
|
||||
get_package,
|
||||
package_update_endpoint
|
||||
package_update_endpoint,
|
||||
cancel_build
|
||||
]
|
||||
}
|
||||
|
@ -147,7 +147,7 @@ pub async fn delete_build(
|
||||
|
||||
#[openapi(tag = "build")]
|
||||
#[post("/build/<buildid>/cancel")]
|
||||
pub async fn cancle_build(
|
||||
pub async fn cancel_build(
|
||||
db: &State<DatabaseConnection>,
|
||||
tx: &State<Sender<Action>>,
|
||||
buildid: i32,
|
||||
|
@ -43,7 +43,7 @@ pub async fn get_info_by_name(pkg_name: &str) -> anyhow::Result<Package> {
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
pub async fn download_pkgbuild(url: &str, dest_dir: &str) -> anyhow::Result<String> {
|
||||
pub async fn download_pkgbuild(url: &str, dest_dir: &str, clear_build_dir: bool) -> anyhow::Result<String> {
|
||||
let (file_data, file_name) = match download_file(url).await {
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
@ -51,6 +51,10 @@ pub async fn download_pkgbuild(url: &str, dest_dir: &str) -> anyhow::Result<Stri
|
||||
}
|
||||
};
|
||||
|
||||
if clear_build_dir {
|
||||
fs::remove_dir_all(dest_dir)?;
|
||||
}
|
||||
|
||||
// Check if the directory exists
|
||||
if fs::metadata(dest_dir).is_err() {
|
||||
// Create the directory if it does not exist
|
||||
|
@ -1,5 +1,5 @@
|
||||
use std::collections::HashMap;
|
||||
use std::iter::Map;
|
||||
use std::ops::Add;
|
||||
use crate::builder::types::Action;
|
||||
use crate::db::builds::ActiveModel;
|
||||
use crate::db::prelude::{Builds, Packages};
|
||||
@ -7,7 +7,6 @@ use crate::db::{builds, packages, versions};
|
||||
use crate::repo::repo::add_pkg;
|
||||
use anyhow::anyhow;
|
||||
use sea_orm::{ActiveModelTrait, DatabaseConnection, EntityTrait, Set};
|
||||
use std::ops::{Add, Deref};
|
||||
use std::sync::Arc;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use tokio::sync::broadcast::error::RecvError;
|
||||
@ -37,11 +36,18 @@ pub async fn init(db: DatabaseConnection, tx: Sender<Action>) {
|
||||
.await;
|
||||
}
|
||||
Action::Cancel(build_id) => {
|
||||
let _ = cancel_build(build_id, job_handles.clone(), db.clone()).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn cancel_build(build_id: i32, job_handles: Arc<Mutex<HashMap<i32, JoinHandle<()>>>>, db: DatabaseConnection) -> anyhow::Result<()> {
|
||||
let build = Builds::find_by_id(build_id)
|
||||
.one(&db)
|
||||
.await
|
||||
.expect("TODO: panic message")
|
||||
.expect("TODO: panic message");
|
||||
.await?
|
||||
.ok_or(anyhow!("No build found"))?;
|
||||
|
||||
let mut build: builds::ActiveModel = build.into();
|
||||
build.status = Set(Some(4));
|
||||
@ -57,12 +63,9 @@ pub async fn init(db: DatabaseConnection, tx: Sender<Action>) {
|
||||
.lock()
|
||||
.await
|
||||
.remove(&build.id.clone().unwrap())
|
||||
.expect("TODO: panic message")
|
||||
.ok_or(anyhow!("No build found"))?
|
||||
.abort();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn queue_package(
|
||||
@ -76,7 +79,6 @@ async fn queue_package(
|
||||
job_handles: Arc<Mutex<HashMap<i32, JoinHandle<()>>>>,
|
||||
) -> anyhow::Result<()> {
|
||||
let permits = Arc::clone(&semaphore);
|
||||
let mut job_handles = Arc::clone(&job_handles);
|
||||
let build_id = build_model.id.clone().unwrap();
|
||||
|
||||
// spawn new thread for each pkg build
|
||||
@ -116,7 +118,7 @@ async fn build_package(
|
||||
pkg.status = Set(0);
|
||||
pkg = pkg.update(&db).await?.into();
|
||||
|
||||
match add_pkg(url, version, name, tx).await {
|
||||
match add_pkg(url, version, name, tx, false).await {
|
||||
Ok(pkg_file_name) => {
|
||||
println!("successfully built package");
|
||||
// update package success status
|
||||
|
@ -20,8 +20,9 @@ pub async fn add_pkg(
|
||||
version: String,
|
||||
name: String,
|
||||
tx: Sender<String>,
|
||||
clear_build_dir: bool,
|
||||
) -> anyhow::Result<String> {
|
||||
let fname = download_pkgbuild(format!("{}{}", BASEURL, url).as_str(), "./builds").await?;
|
||||
let fname = download_pkgbuild(format!("{}{}", BASEURL, url).as_str(), "./builds", clear_build_dir).await?;
|
||||
let pkg_file_names = build_pkgbuild(
|
||||
format!("./builds/{fname}"),
|
||||
version.as_str(),
|
||||
|
Loading…
Reference in New Issue
Block a user