async fn check_postgres(
host: String,
port: u16,
password: String,
db_name: String,
path: String,
leader: Arc<Sender<String>>,
pool: Arc<Mutex<Option<tokio_postgres::Client>>>,
process_lock: Arc<Mutex<()>>,
) -> Result<(), anyhow::Error> {
loop {
let leader = leader.borrow().to_string();
if leader != host {
let mut needs_rewind = false;
let pool_ = { pool.lock().await.take() };
if let Some(pool_) = pool_ {
needs_rewind = pool_
.query_opt("SELECT 1 FROM pg_stat_wal_receiver", &[])
.await?
.is_none();
*pool.lock().await = Some(pool_);
}
if needs_rewind {
debug!("leader: {:?}", leader);
rewind(&process_lock, &path, port, &db_name, &leader, &password).await
}
}
tokio::time::sleep(std::time::Duration::from_secs(10)).await
}
}