make free tier even better and improve migration locking
This commit is contained in:
parent
0731d92dec
commit
17d8ea0b7f
|
@ -1355,7 +1355,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "entities"
|
name = "entities"
|
||||||
version = "0.12.0"
|
version = "0.13.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ethers",
|
"ethers",
|
||||||
"sea-orm",
|
"sea-orm",
|
||||||
|
@ -2746,7 +2746,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "migration"
|
name = "migration"
|
||||||
version = "0.12.0"
|
version = "0.13.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"sea-orm-migration",
|
"sea-orm-migration",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
@ -5565,7 +5565,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "web3_proxy"
|
name = "web3_proxy"
|
||||||
version = "0.12.0"
|
version = "0.13.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"arc-swap",
|
"arc-swap",
|
||||||
|
|
|
@ -104,7 +104,7 @@ web3_proxy_cli --config ... change_user_tier_by_key "$RPC_ULID_KEY_FROM_PREV_COM
|
||||||
Health check 3 servers and error if the first one doesn't match the others.
|
Health check 3 servers and error if the first one doesn't match the others.
|
||||||
|
|
||||||
```
|
```
|
||||||
web3_proxy_cli https://eth.llamarpc.com/ https://rpc.ankr.com/eth https://cloudflare-eth.com
|
web3_proxy_cli health_compass https://eth.llamarpc.com/ https://rpc.ankr.com/eth https://cloudflare-eth.com
|
||||||
```
|
```
|
||||||
|
|
||||||
## Adding new database tables
|
## Adding new database tables
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "entities"
|
name = "entities"
|
||||||
version = "0.12.0"
|
version = "0.13.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "migration"
|
name = "migration"
|
||||||
version = "0.12.0"
|
version = "0.13.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
- Generate a new migration file
|
- Generate a new migration file
|
||||||
```sh
|
```sh
|
||||||
cargo run -- migrate generate MIGRATION_NAME
|
cargo run -- generate MIGRATION_NAME
|
||||||
```
|
```
|
||||||
- Apply all pending migrations
|
- Apply all pending migrations
|
||||||
```sh
|
```sh
|
||||||
|
|
|
@ -12,6 +12,7 @@ mod m20221101_222349_archive_request;
|
||||||
mod m20221108_200345_save_anon_stats;
|
mod m20221108_200345_save_anon_stats;
|
||||||
mod m20221211_124002_request_method_privacy;
|
mod m20221211_124002_request_method_privacy;
|
||||||
mod m20221213_134158_move_login_into_database;
|
mod m20221213_134158_move_login_into_database;
|
||||||
|
mod m20230119_204135_better_free_tier;
|
||||||
|
|
||||||
pub struct Migrator;
|
pub struct Migrator;
|
||||||
|
|
||||||
|
@ -31,6 +32,7 @@ impl MigratorTrait for Migrator {
|
||||||
Box::new(m20221108_200345_save_anon_stats::Migration),
|
Box::new(m20221108_200345_save_anon_stats::Migration),
|
||||||
Box::new(m20221211_124002_request_method_privacy::Migration),
|
Box::new(m20221211_124002_request_method_privacy::Migration),
|
||||||
Box::new(m20221213_134158_move_login_into_database::Migration),
|
Box::new(m20221213_134158_move_login_into_database::Migration),
|
||||||
|
Box::new(m20230119_204135_better_free_tier::Migration),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "web3_proxy"
|
name = "web3_proxy"
|
||||||
version = "0.12.0"
|
version = "0.13.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
default-run = "web3_proxy_cli"
|
default-run = "web3_proxy_cli"
|
||||||
|
|
||||||
|
|
|
@ -273,18 +273,14 @@ pub async fn drop_migration_lock(db_conn: &DatabaseConnection) -> Result<(), DbE
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Connect to the database and run migrations
|
/// Be super careful with override_existing_lock! It is very important that only one process is running the migrations at a time!
|
||||||
pub async fn get_migrated_db(
|
pub async fn migrate_db(
|
||||||
db_url: String,
|
db_conn: &DatabaseConnection,
|
||||||
min_connections: u32,
|
override_existing_lock: bool,
|
||||||
max_connections: u32,
|
) -> Result<(), DbErr> {
|
||||||
) -> anyhow::Result<DatabaseConnection> {
|
|
||||||
// TODO: this seems to fail silently
|
|
||||||
let db_conn = get_db(db_url, min_connections, max_connections).await?;
|
|
||||||
|
|
||||||
let db_backend = db_conn.get_database_backend();
|
let db_backend = db_conn.get_database_backend();
|
||||||
|
|
||||||
// TODO: put the timestamp into this?
|
// TODO: put the timestamp and hostname into this as columns?
|
||||||
let create_lock_statment = db_backend.build(
|
let create_lock_statment = db_backend.build(
|
||||||
Table::create()
|
Table::create()
|
||||||
.table(Alias::new("migration_lock"))
|
.table(Alias::new("migration_lock"))
|
||||||
|
@ -294,19 +290,25 @@ pub async fn get_migrated_db(
|
||||||
loop {
|
loop {
|
||||||
if Migrator::get_pending_migrations(&db_conn).await?.is_empty() {
|
if Migrator::get_pending_migrations(&db_conn).await?.is_empty() {
|
||||||
info!("no migrations to apply");
|
info!("no migrations to apply");
|
||||||
return Ok(db_conn);
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
// there are migrations to apply
|
// there are migrations to apply
|
||||||
// acquire a lock
|
// acquire a lock
|
||||||
if let Err(err) = db_conn.execute(create_lock_statment.clone()).await {
|
if let Err(err) = db_conn.execute(create_lock_statment.clone()).await {
|
||||||
debug!("Unable to acquire lock. err={:?}", err);
|
if override_existing_lock {
|
||||||
|
warn!("OVERRIDING EXISTING LOCK in 10 seconds! ctrl+c now if other migrations are actually running!");
|
||||||
|
|
||||||
// TODO: exponential backoff with jitter
|
sleep(Duration::from_secs(10)).await
|
||||||
|
} else {
|
||||||
|
debug!("Unable to acquire lock. if you are positive no migration is running, run \"web3_proxy_cli drop_migration_lock\". err={:?}", err);
|
||||||
|
|
||||||
|
// TODO: exponential backoff with jitter?
|
||||||
sleep(Duration::from_secs(1)).await;
|
sleep(Duration::from_secs(1)).await;
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
debug!("migration lock acquired");
|
debug!("migration lock acquired");
|
||||||
break;
|
break;
|
||||||
|
@ -318,7 +320,19 @@ pub async fn get_migrated_db(
|
||||||
drop_migration_lock(&db_conn).await?;
|
drop_migration_lock(&db_conn).await?;
|
||||||
|
|
||||||
// return if migrations erred
|
// return if migrations erred
|
||||||
migration_result?;
|
migration_result
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Connect to the database and run migrations
|
||||||
|
pub async fn get_migrated_db(
|
||||||
|
db_url: String,
|
||||||
|
min_connections: u32,
|
||||||
|
max_connections: u32,
|
||||||
|
) -> Result<DatabaseConnection, DbErr> {
|
||||||
|
// TODO: this seems to fail silently
|
||||||
|
let db_conn = get_db(db_url, min_connections, max_connections).await?;
|
||||||
|
|
||||||
|
migrate_db(&db_conn, false).await?;
|
||||||
|
|
||||||
Ok(db_conn)
|
Ok(db_conn)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,24 @@
|
||||||
use argh::FromArgs;
|
use argh::FromArgs;
|
||||||
use migration::sea_orm::DatabaseConnection;
|
use migration::sea_orm::DatabaseConnection;
|
||||||
use web3_proxy::app::drop_migration_lock;
|
use web3_proxy::app::{drop_migration_lock, migrate_db};
|
||||||
|
|
||||||
#[derive(FromArgs, PartialEq, Debug, Eq)]
|
#[derive(FromArgs, PartialEq, Debug, Eq)]
|
||||||
/// In case of emergency, break glass.
|
/// In case of emergency, break glass.
|
||||||
#[argh(subcommand, name = "drop_migration_lock")]
|
#[argh(subcommand, name = "drop_migration_lock")]
|
||||||
pub struct DropMigrationLockSubCommand {}
|
pub struct DropMigrationLockSubCommand {
|
||||||
|
#[argh(option)]
|
||||||
|
/// run migrations after dropping the lock
|
||||||
|
and_migrate: bool,
|
||||||
|
}
|
||||||
|
|
||||||
impl DropMigrationLockSubCommand {
|
impl DropMigrationLockSubCommand {
|
||||||
pub async fn main(&self, db_conn: &DatabaseConnection) -> anyhow::Result<()> {
|
pub async fn main(&self, db_conn: &DatabaseConnection) -> anyhow::Result<()> {
|
||||||
|
if self.and_migrate {
|
||||||
|
migrate_db(db_conn, true).await?;
|
||||||
|
} else {
|
||||||
|
// just drop the lock
|
||||||
drop_migration_lock(db_conn).await?;
|
drop_migration_lock(db_conn).await?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue