loop until it works

This commit is contained in:
Bryan Stitt 2023-06-29 23:13:22 -07:00
parent 5da334fcb7
commit 4a03d2e331
3 changed files with 31 additions and 8 deletions

View File

@ -5,7 +5,7 @@ use migration::sea_query::table::ColumnDef;
use migration::{Alias, DbErr, Migrator, MigratorTrait, Table};
use std::time::Duration;
use tokio::time::sleep;
use tracing::{debug, info, warn};
use tracing::{debug, info, trace, warn};
pub use migration::sea_orm::DatabaseConnection;
@ -39,7 +39,7 @@ pub async fn get_db(
.connect_timeout(Duration::from_secs(5))
.min_connections(min_connections)
.max_connections(max_connections)
.sqlx_logging_level(tracing::log::LevelFilter::Warn)
.sqlx_logging_level(tracing::log::LevelFilter::Trace)
.sqlx_logging(true);
Database::connect(db_opt).await

View File

@ -291,6 +291,11 @@ impl Web3Rpcs {
// TODO: check known uncles
}
if hash == &H256::zero() {
// TODO: think more about this
return Err(Web3ProxyError::UnknownBlockHash(*hash));
}
// block not in cache. we need to ask an rpc for it
let get_block_params = (*hash, false);

View File

@ -23,7 +23,7 @@ use tokio::{
task::JoinHandle,
time::{sleep, Instant},
};
use tracing::{info, trace};
use tracing::{info, trace, warn};
use web3_proxy::{
config::{AppConfig, TopConfig, Web3RpcConfig},
relational_db::get_migrated_db,
@ -192,12 +192,31 @@ impl TestApp {
sleep(Duration::from_secs(1)).await;
}
info!(%db_url, "db is ready for connections");
// TODO: make sure mysql is actually ready for connections
sleep(Duration::from_secs(7)).await;
info!(%db_url, elapsed=%start.elapsed().as_secs_f32(), "db is ready for connections. Migrating now...");
// try to migrate
let _ = get_migrated_db(db_url, 1, 1)
.await
.expect("failed migration");
let start = Instant::now();
let max_wait = Duration::from_secs(30);
loop {
if start.elapsed() > max_wait {
panic!("db took too long to start");
}
if let Err(err) = get_migrated_db(db_url.clone(), 1, 1).await {
// not connected. sleep and then try again
warn!(?err, "unable to migrate db");
sleep(Duration::from_secs(1)).await;
continue;
}
// it worked! yey!
break;
}
info!(%db_url, elapsed=%start.elapsed().as_secs_f32(), "db is migrated");
Some(db_data)
} else {
@ -314,7 +333,6 @@ impl Drop for TestApp {
impl Drop for DbData {
fn drop(&mut self) {
// TODO: this doesn't seem to run
info!(%self.container_name, "killing db");
let _ = SyncCommand::new("docker")