-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #739 from helium/andymck/boost-man-purger
purge successfully processed activations from db after a period
- Loading branch information
Showing
6 changed files
with
210 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use crate::db; | ||
use chrono::Duration as ChronoDuration; | ||
use futures::{future::LocalBoxFuture, TryFutureExt}; | ||
use sqlx::{Pool, Postgres}; | ||
use std::time::Duration; | ||
use task_manager::ManagedTask; | ||
|
||
const PURGE_INTERVAL: Duration = Duration::from_secs(30); | ||
|
||
pub struct Purger { | ||
pool: Pool<Postgres>, | ||
retention_period: ChronoDuration, | ||
} | ||
|
||
impl ManagedTask for Purger { | ||
fn start_task( | ||
self: Box<Self>, | ||
shutdown: triggered::Listener, | ||
) -> LocalBoxFuture<'static, anyhow::Result<()>> { | ||
let handle = tokio::spawn(self.run(shutdown)); | ||
Box::pin( | ||
handle | ||
.map_err(anyhow::Error::from) | ||
.and_then(|result| async move { result.map_err(anyhow::Error::from) }), | ||
) | ||
} | ||
} | ||
|
||
impl Purger { | ||
pub fn new(pool: Pool<Postgres>, retention_period: ChronoDuration) -> Self { | ||
Self { | ||
pool, | ||
retention_period, | ||
} | ||
} | ||
|
||
async fn run(self, shutdown: triggered::Listener) -> anyhow::Result<()> { | ||
tracing::info!("starting Purger"); | ||
loop { | ||
tokio::select! { | ||
biased; | ||
_ = shutdown.clone() => break, | ||
_ = tokio::time::sleep(PURGE_INTERVAL) => { | ||
purge(&self.pool, self.retention_period).await?; | ||
} | ||
} | ||
} | ||
tracing::info!("stopping Purger"); | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub async fn purge(pool: &Pool<Postgres>, retention_period: ChronoDuration) -> anyhow::Result<()> { | ||
let num_records_purged = db::purge_stale_records(pool, retention_period).await?; | ||
tracing::info!("purged {} stale records", num_records_purged); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
mod common; | ||
use boost_manager::purger; | ||
use boost_manager::OnChainStatus; | ||
use chrono::{DateTime, Duration, Utc}; | ||
use sqlx::{PgPool, Postgres, Transaction}; | ||
|
||
const BOOST_HEX_PUBKEY: &str = "J9JiLTpjaShxL8eMvUs8txVw6TZ36E38SiJ89NxnMbLU"; | ||
const BOOST_CONFIG_PUBKEY: &str = "BZM1QTud72B2cpTW7PhEnFmRX7ZWzvY7DpPpNJJuDrWG"; | ||
|
||
#[sqlx::test] | ||
async fn test_purge(pool: PgPool) -> anyhow::Result<()> { | ||
// seed test data to db | ||
seed_data(&pool).await?; | ||
|
||
// assert the db contains the expected number of records pre purge | ||
let count: i64 = sqlx::query_scalar("select count(*) from activated_hexes") | ||
.fetch_one(&pool) | ||
.await?; | ||
assert_eq!(7, count); | ||
|
||
// do da purge | ||
purger::purge(&pool, Duration::days(7)).await?; | ||
|
||
// assert the db contains the expected number of records post purge | ||
let count: i64 = sqlx::query_scalar("select count(*) from activated_hexes") | ||
.fetch_one(&pool) | ||
.await?; | ||
assert_eq!(4, count); | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn seed_data(db: &PgPool) -> anyhow::Result<()> { | ||
let now = Utc::now(); | ||
let mut txn = db.begin().await?; | ||
|
||
insert_data( | ||
&mut txn, | ||
0x8c2681a306601ff_u64, | ||
OnChainStatus::Queued, | ||
now - Duration::days(1), | ||
) | ||
.await?; | ||
insert_data( | ||
&mut txn, | ||
0x8c2681a306602ff_u64, | ||
OnChainStatus::Queued, | ||
now - Duration::days(8), | ||
) | ||
.await?; | ||
insert_data( | ||
&mut txn, | ||
0x8c2681a306603ff_u64, | ||
OnChainStatus::Success, | ||
now - Duration::days(2), | ||
) | ||
.await?; | ||
insert_data( | ||
&mut txn, | ||
0x8c2681a306604ff_u64, | ||
OnChainStatus::Success, | ||
now - Duration::days(6), | ||
) | ||
.await?; | ||
insert_data( | ||
&mut txn, | ||
0x8c2681a306605ff_u64, | ||
OnChainStatus::Success, | ||
now - Duration::days(8), | ||
) | ||
.await?; | ||
insert_data( | ||
&mut txn, | ||
0x8c2681a306606ff_u64, | ||
OnChainStatus::Success, | ||
now - Duration::days(9), | ||
) | ||
.await?; | ||
insert_data( | ||
&mut txn, | ||
0x8c2681a306607ff_u64, | ||
OnChainStatus::Success, | ||
now - Duration::days(10), | ||
) | ||
.await?; | ||
|
||
txn.commit().await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn insert_data( | ||
txn: &mut Transaction<'_, Postgres>, | ||
location: u64, | ||
status: OnChainStatus, | ||
last_updated_at: DateTime<Utc>, | ||
) -> Result<(), sqlx::Error> { | ||
sqlx::query( | ||
r#" | ||
insert into activated_hexes ( | ||
location, | ||
activation_ts, | ||
boosted_hex_pubkey, | ||
boost_config_pubkey, | ||
status, | ||
updated_at | ||
) values ($1, $2, $3, $4, $5, $6) | ||
on conflict do nothing | ||
"#, | ||
) | ||
.bind(location as i64) | ||
.bind(last_updated_at) | ||
.bind(BOOST_HEX_PUBKEY) | ||
.bind(BOOST_CONFIG_PUBKEY) | ||
.bind(status) | ||
.bind(last_updated_at) | ||
.execute(txn) | ||
.await?; | ||
Ok(()) | ||
} |