mirror of
https://github.com/moghtech/komodo.git
synced 2026-09-11 00:01:21 +00:00
further improve BuildState if cancel.
This commit is contained in:
@@ -9,14 +9,18 @@ use monitor_client::entities::{
|
||||
permission::PermissionLevel,
|
||||
server::{Server, ServerState},
|
||||
tag::Tag,
|
||||
update::ResourceTargetVariant,
|
||||
update::{ResourceTargetVariant, Update},
|
||||
user::{admin_service_user, User},
|
||||
variable::Variable,
|
||||
Operation,
|
||||
};
|
||||
use mungos::{
|
||||
by_id::find_one_by_id,
|
||||
find::find_collect,
|
||||
mongodb::bson::{doc, oid::ObjectId, Document},
|
||||
mongodb::{
|
||||
bson::{doc, oid::ObjectId, Document},
|
||||
options::FindOneOptions,
|
||||
},
|
||||
};
|
||||
|
||||
use crate::{config::core_config, resource, state::db_client};
|
||||
@@ -231,3 +235,25 @@ pub async fn get_variable(name: &str) -> anyhow::Result<Variable> {
|
||||
format!("no variable found with given name: {name}")
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn get_latest_update(
|
||||
resource_type: ResourceTargetVariant,
|
||||
id: &str,
|
||||
operation: Operation,
|
||||
) -> anyhow::Result<Option<Update>> {
|
||||
db_client()
|
||||
.await
|
||||
.updates
|
||||
.find_one(
|
||||
doc! {
|
||||
"target.type": resource_type.as_ref(),
|
||||
"target.id": id,
|
||||
"operation": operation.as_ref()
|
||||
},
|
||||
FindOneOptions::builder()
|
||||
.sort(doc! { "start_ts": -1 })
|
||||
.build(),
|
||||
)
|
||||
.await
|
||||
.context("failed to query db for latest update")
|
||||
}
|
||||
|
||||
@@ -16,11 +16,11 @@ use monitor_client::entities::{
|
||||
};
|
||||
use mungos::{
|
||||
find::find_collect,
|
||||
mongodb::{bson::doc, options::FindOneOptions, Collection},
|
||||
mongodb::{bson::doc, options::FindOptions, Collection},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
helpers::empty_or_only_spaces,
|
||||
helpers::{empty_or_only_spaces, query::get_latest_update},
|
||||
state::{action_states, build_state_cache, db_client},
|
||||
};
|
||||
|
||||
@@ -205,28 +205,40 @@ async fn get_build_state(id: &String) -> BuildState {
|
||||
|
||||
async fn get_build_state_from_db(id: &str) -> BuildState {
|
||||
async {
|
||||
let state = db_client()
|
||||
.await
|
||||
.updates
|
||||
.find_one(
|
||||
doc! {
|
||||
"target.type": "Build",
|
||||
"target.id": id,
|
||||
"operation": "RunBuild"
|
||||
},
|
||||
FindOneOptions::builder()
|
||||
.sort(doc! { "start_ts": -1 })
|
||||
.build(),
|
||||
)
|
||||
.await?
|
||||
.map(|u| {
|
||||
if u.success {
|
||||
let state = match tokio::try_join!(
|
||||
latest_2_build_updates(id),
|
||||
get_latest_update(
|
||||
ResourceTargetVariant::Build,
|
||||
id,
|
||||
Operation::CancelBuild
|
||||
),
|
||||
)? {
|
||||
([Some(build), second], Some(cancel))
|
||||
if cancel.start_ts > build.start_ts =>
|
||||
{
|
||||
match second {
|
||||
Some(build) => {
|
||||
if build.success {
|
||||
BuildState::Ok
|
||||
} else {
|
||||
BuildState::Failed
|
||||
}
|
||||
}
|
||||
None => BuildState::Ok,
|
||||
}
|
||||
}
|
||||
([Some(build), _], _) => {
|
||||
if build.success {
|
||||
BuildState::Ok
|
||||
} else {
|
||||
BuildState::Failed
|
||||
}
|
||||
})
|
||||
.unwrap_or(BuildState::Ok);
|
||||
}
|
||||
_ => {
|
||||
// No build update ever, should be fine
|
||||
BuildState::Ok
|
||||
}
|
||||
};
|
||||
anyhow::Ok(state)
|
||||
}
|
||||
.await
|
||||
@@ -235,3 +247,25 @@ async fn get_build_state_from_db(id: &str) -> BuildState {
|
||||
})
|
||||
.unwrap_or(BuildState::Unknown)
|
||||
}
|
||||
|
||||
async fn latest_2_build_updates(
|
||||
id: &str,
|
||||
) -> anyhow::Result<[Option<Update>; 2]> {
|
||||
let mut builds = find_collect(
|
||||
&db_client().await.updates,
|
||||
doc! {
|
||||
"target.type": "Build",
|
||||
"target.id": id,
|
||||
"operation": "RunBuild"
|
||||
},
|
||||
FindOptions::builder()
|
||||
.sort(doc! { "start_ts": -1 })
|
||||
.limit(2)
|
||||
.build(),
|
||||
)
|
||||
.await
|
||||
.context("failed to query for latest updates")?;
|
||||
let second = builds.pop();
|
||||
let first = builds.pop();
|
||||
Ok([first, second])
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use clap::Parser;
|
||||
use derive_empty_traits::EmptyTraits;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serror::Serror;
|
||||
use strum::{Display, EnumString};
|
||||
use strum::{AsRefStr, Display, EnumString};
|
||||
use typeshare::typeshare;
|
||||
|
||||
/// Subtypes of [Alert][alert::Alert].
|
||||
@@ -370,17 +370,18 @@ impl TryInto<async_timing_util::Timelength> for Timelength {
|
||||
|
||||
#[typeshare]
|
||||
#[derive(
|
||||
Debug,
|
||||
Clone,
|
||||
Copy,
|
||||
PartialEq,
|
||||
Eq,
|
||||
Hash,
|
||||
Serialize,
|
||||
Deserialize,
|
||||
Debug,
|
||||
Default,
|
||||
Display,
|
||||
EnumString,
|
||||
PartialEq,
|
||||
Hash,
|
||||
Eq,
|
||||
Clone,
|
||||
Copy,
|
||||
AsRefStr,
|
||||
)]
|
||||
pub enum Operation {
|
||||
// do nothing
|
||||
|
||||
Reference in New Issue
Block a user