Add LoxDBError and refactor to return Result
Add a new error type to capture multiple possible database reading errors and refactor both read_lox_context_from_db and use_last_context to return a Result.
This commit is contained in:
parent
5ed62d239d
commit
7d94b3c861
|
@ -1066,6 +1066,7 @@ dependencies = [
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"serde_with",
|
"serde_with",
|
||||||
"sled",
|
"sled",
|
||||||
|
"thiserror",
|
||||||
"time",
|
"time",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
|
@ -35,6 +35,7 @@ serde_json = "1.0.108"
|
||||||
prometheus = "0.13.3"
|
prometheus = "0.13.3"
|
||||||
sled = "0.34.7"
|
sled = "0.34.7"
|
||||||
prometheus-client = "0.22.0"
|
prometheus-client = "0.22.0"
|
||||||
|
thiserror = "1"
|
||||||
|
|
||||||
[dependencies.chrono]
|
[dependencies.chrono]
|
||||||
version = "0.4.31"
|
version = "0.4.31"
|
||||||
|
|
|
@ -5,6 +5,18 @@ use crate::{lox_context, DbConfig};
|
||||||
use chrono::{naive::Days, DateTime, Local, NaiveDateTime, Utc};
|
use chrono::{naive::Days, DateTime, Local, NaiveDateTime, Utc};
|
||||||
use lox_library::{BridgeAuth, BridgeDb};
|
use lox_library::{BridgeAuth, BridgeDb};
|
||||||
use sled::IVec;
|
use sled::IVec;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum LoxDBError {
|
||||||
|
//failed to get last db entry
|
||||||
|
#[error("Failed to read last database entry")]
|
||||||
|
ReadFailure(#[from] sled::Error),
|
||||||
|
|
||||||
|
//no last db entries
|
||||||
|
#[error("No database entries stored")]
|
||||||
|
DatabaseEmpty,
|
||||||
|
}
|
||||||
|
|
||||||
// Database of Lox Distributor State
|
// Database of Lox Distributor State
|
||||||
pub struct DB {
|
pub struct DB {
|
||||||
|
@ -52,7 +64,10 @@ impl DB {
|
||||||
Ok(lox_db) => {
|
Ok(lox_db) => {
|
||||||
// Check if the lox_db already exists
|
// Check if the lox_db already exists
|
||||||
if lox_db.was_recovered() && !lox_db.is_empty() {
|
if lox_db.was_recovered() && !lox_db.is_empty() {
|
||||||
context = read_lox_context_from_db(lox_db.clone(), roll_back_date);
|
context = match read_lox_context_from_db(lox_db.clone(), roll_back_date) {
|
||||||
|
Ok(ctx) => ctx,
|
||||||
|
Err(e) => panic!("Unable to read lox database {:?}", e)
|
||||||
|
};
|
||||||
context.metrics = metrics;
|
context.metrics = metrics;
|
||||||
//Otherwise, create a new Lox context
|
//Otherwise, create a new Lox context
|
||||||
} else {
|
} else {
|
||||||
|
@ -80,7 +95,7 @@ impl DB {
|
||||||
fn read_lox_context_from_db(
|
fn read_lox_context_from_db(
|
||||||
lox_db: sled::Db,
|
lox_db: sled::Db,
|
||||||
roll_back_date: Option<String>,
|
roll_back_date: Option<String>,
|
||||||
) -> lox_context::LoxServerContext {
|
) -> Result<lox_context::LoxServerContext, LoxDBError> {
|
||||||
let context: lox_context::LoxServerContext;
|
let context: lox_context::LoxServerContext;
|
||||||
// Check if there is a roll back date and try to choose the appropriate context
|
// Check if there is a roll back date and try to choose the appropriate context
|
||||||
// to rollback to, otherwise, take the last saved context
|
// to rollback to, otherwise, take the last saved context
|
||||||
|
@ -119,10 +134,10 @@ fn read_lox_context_from_db(
|
||||||
}
|
}
|
||||||
// Use the last entry to populate the Lox context if no rollback date is set (which should be most common)
|
// Use the last entry to populate the Lox context if no rollback date is set (which should be most common)
|
||||||
None => {
|
None => {
|
||||||
context = use_last_context(lox_db);
|
context = use_last_context(lox_db)?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
context
|
Ok(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compute_startdate_string(date_range_end: String) -> Option<DateTime<Utc>> {
|
fn compute_startdate_string(date_range_end: String) -> Option<DateTime<Utc>> {
|
||||||
|
@ -133,11 +148,15 @@ fn compute_startdate_string(date_range_end: String) -> Option<DateTime<Utc>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use the last context that was entered into the database
|
// Use the last context that was entered into the database
|
||||||
fn use_last_context(lox_db: sled::Db) -> lox_context::LoxServerContext {
|
fn use_last_context(lox_db: sled::Db) -> Result<lox_context::LoxServerContext, LoxDBError> {
|
||||||
let ivec_context = lox_db.last().unwrap().unwrap();
|
match lox_db.last()? {
|
||||||
let ivec_date: String = String::from_utf8(ivec_context.0.to_vec()).unwrap();
|
Some(ivec_context) => {
|
||||||
println!("Using last context with date: {:?}", ivec_date);
|
let ivec_date: String = String::from_utf8(ivec_context.0.to_vec()).unwrap();
|
||||||
serde_json::from_slice(&ivec_context.1).unwrap()
|
println!("Using last context with date: {:?}", ivec_date);
|
||||||
|
Ok(serde_json::from_slice(&ivec_context.1).unwrap())
|
||||||
|
},
|
||||||
|
None => Err(LoxDBError::DatabaseEmpty)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Reference in New Issue