Skip to content

Commit

Permalink
Major Refactors, pending finish
Browse files Browse the repository at this point in the history
  • Loading branch information
oestradiol committed Feb 25, 2024
1 parent a32c3c5 commit 823f3d7
Show file tree
Hide file tree
Showing 29 changed files with 689 additions and 444 deletions.
59 changes: 59 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ url = "^2.5"
paste = "^1.0"
regex = "^1.0"
uuid = { version = "^1.7", features = ["v4", "fast-rng", "macro-diagnostics"] }
git2 = "^0.18"

[target.'cfg(unix)'.dependencies]
jemallocator = "0.5"
Expand Down
66 changes: 43 additions & 23 deletions src/business/repositories/deployments/delete.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,59 @@
use crate::{
business::repositories::{DB_CONTEXT, REPOSITORIES_RUNTIME},
types::other::voyager_error::VoyagerError,
utils::{runtime_helpers::RuntimeSpawnHandled, Error},
};
use axum::http::StatusCode;
use mongodb::bson::doc;
use tracing::{event, Level};

pub async fn delete(name: String) -> Option<()> {
pub async fn delete(name: String) -> Result<(), VoyagerError> {
event!(
Level::DEBUG,
"Deleting deployment of name {name} from database."
);

let future = async move {
let result = DB_CONTEXT
.deployments
.delete_one(doc! {"name": name}, None)
.await;
let result = REPOSITORIES_RUNTIME
.spawn_handled(
"repositories::deployments::delete",
DB_CONTEXT
.deployments
.delete_one(doc! {"name": &name}, None),
)
.await?;

result.map_err(Error::from) // MongoDB Error
};
result.map_or_else(
|e| Err(VoyagerError::delete_mongo(Box::new(e), &name)),
|r| {
if r.deleted_count == 0 {
Err(VoyagerError::delete(&name))
} else {
Ok(())
}
},
)
}

let result = REPOSITORIES_RUNTIME
.spawn_handled("repositories::deployments::delete", future)
.await;
impl VoyagerError {
pub fn delete_mongo(e: Error, name: &str) -> Self {
let message = format!("Failed to delete deployment named '{name}'! Error:{e}");

event!(Level::ERROR, message);
VoyagerError {
message,
status_code: StatusCode::INTERNAL_SERVER_ERROR,
source: Some(e),
}
}

pub fn delete(name: &str) -> Self {
let message = format!("Failed to find deployment named '{name}'!");

result
.map(|r| {
r.map_or_else(
|e| {
event!(Level::ERROR, "Failed to retrieve deployments: {}", e);
false
},
|d| d.deleted_count > 0,
)
})
.map(|r| if r { Some(()) } else { None })
.and_then(|r| r)
event!(Level::ERROR, message);
VoyagerError {
message,
status_code: StatusCode::NOT_FOUND,
source: None,
}
}
}
72 changes: 38 additions & 34 deletions src/business/repositories/deployments/find_by_host.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,50 @@
use crate::{
business::repositories::{DB_CONTEXT, REPOSITORIES_RUNTIME},
types::model::deployment::Deployment,
utils::runtime_helpers::RuntimeSpawnHandled,
utils::Error,
types::{model::deployment::Deployment, other::voyager_error::VoyagerError},
utils::{runtime_helpers::RuntimeSpawnHandled, Error},
};
use axum::http::StatusCode;
use mongodb::bson::doc;
use tracing::{event, Level};

pub async fn find_by_host(host: String) -> Option<Deployment> {
pub async fn find_by_host(host: String) -> Result<Deployment, VoyagerError> {
event!(Level::DEBUG, "Finding deployment by host {}", &host);

let host_clone = host.clone();
let future = async move {
let result = DB_CONTEXT
.deployments
.find_one(doc! { "host": &host }, None)
.await;
let result = REPOSITORIES_RUNTIME
.spawn_handled(
"repositories::deployments::find_by_host",
DB_CONTEXT
.deployments
.find_one(doc! { "host": &host }, None),
)
.await?;

result
.map_err(Error::from) // MongoDB Error
.map(|d| d.ok_or_else(|| Error::from("Deployment not found"))) // 'None' Error
.and_then(|inner| inner) // Flatten
};
result.map_or_else(
|e| Err(VoyagerError::find_mongo_host(Box::new(e), &host)),
|r| r.ok_or_else(|| VoyagerError::find_null_host(&host)),
)
}

let result = REPOSITORIES_RUNTIME
.spawn_handled("repositories::deployments::find_by_host", future)
.await;
impl VoyagerError {
pub fn find_mongo_host(e: Error, host: &str) -> Self {
let message = format!("Failed to find deployment by host '{host}'! Error:{e}");

event!(Level::ERROR, message);
VoyagerError {
message,
status_code: StatusCode::INTERNAL_SERVER_ERROR,
source: Some(e),
}
}

pub fn find_null_host(host: &str) -> Self {
let message = format!("Failed to find deployment by host '{host}'!");

result
.map(|r| {
r.map_or_else(
|e| {
event!(
Level::ERROR,
"Failed to find deployment with host {}: {}",
host_clone,
e
);
None
},
Some,
)
})
.and_then(|d| d)
event!(Level::ERROR, message);
VoyagerError {
message,
status_code: StatusCode::NOT_FOUND,
source: None,
}
}
}
71 changes: 36 additions & 35 deletions src/business/repositories/deployments/find_by_id.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
use crate::{
business::repositories::{DB_CONTEXT, REPOSITORIES_RUNTIME},
types::model::deployment::Deployment,
utils::runtime_helpers::RuntimeSpawnHandled,
utils::Error,
types::{model::deployment::Deployment, other::voyager_error::VoyagerError},
utils::{runtime_helpers::RuntimeSpawnHandled, Error},
};
use axum::http::StatusCode;
use mongodb::bson::doc;
use tracing::{event, Level};


pub async fn find_by_id(id: String) -> Option<Deployment> {
pub async fn find_by_id(id: String) -> Result<Deployment, VoyagerError> {
event!(Level::DEBUG, "Finding deployment with id {}", &id);

let id_clone = id.clone();
let future = async move {
let result = DB_CONTEXT
.deployments
.find_one(doc! { "_id": id }, None)
.await;
let result = REPOSITORIES_RUNTIME
.spawn_handled(
"repositories::deployments::find_by_id",
DB_CONTEXT.deployments.find_one(doc! { "_id": &id }, None),
)
.await?;

result.map_or_else(
|e| Err(VoyagerError::find_mongo_id(Box::new(e), &id)),
|r| r.ok_or_else(|| VoyagerError::find_null_id(&id)),
)
}

result
.map_err(Error::from) // MongoDB Error
.map(|d| d.ok_or_else(|| Error::from("Deployment not found"))) // 'None' Error
.and_then(|inner| inner) // Flatten
};
impl VoyagerError {
pub fn find_mongo_id(e: Error, id: &str) -> Self {
let message = format!("Failed to find deployment by id '{id}'! Error:{e}");

let result = REPOSITORIES_RUNTIME
.spawn_handled("repositories::deployments::find_by_id", future)
.await;
event!(Level::ERROR, message);
VoyagerError {
message,
status_code: StatusCode::INTERNAL_SERVER_ERROR,
source: Some(e),
}
}

pub fn find_null_id(id: &str) -> Self {
let message = format!("Failed to find deployment by id '{id}'!");

result
.map(|r| {
r.map_or_else(
|e| {
event!(
Level::ERROR,
"Failed to find deployment with id {}: {}",
id_clone,
e
);
None
},
Some,
)
})
.and_then(|d| d)
event!(Level::ERROR, message);
VoyagerError {
message,
status_code: StatusCode::NOT_FOUND,
source: None,
}
}
}
Loading

0 comments on commit 823f3d7

Please sign in to comment.