Earlier db initialization + change unwrap for error.

Closes #147.
This commit is contained in:
Orne Brocaar 2023-03-28 14:04:14 +01:00
parent d81af6b607
commit 4917de2b32
2 changed files with 12 additions and 7 deletions

View File

@ -11,12 +11,12 @@ pub async fn run() -> Result<()> {
"Starting ChirpStack LoRaWAN Network Server" "Starting ChirpStack LoRaWAN Network Server"
); );
storage::setup().await?;
region::setup()?; region::setup()?;
backend::setup()?; backend::setup()?;
adr::setup().await?; adr::setup().await?;
integration::setup().await?; integration::setup().await?;
gateway::backend::setup().await?; gateway::backend::setup().await?;
storage::setup().await?;
downlink::setup().await; downlink::setup().await;
api::setup().await?; api::setup().await?;

View File

@ -219,20 +219,25 @@ pub async fn setup() -> Result<()> {
Ok(()) Ok(())
} }
pub fn get_db_pool() -> PgPool { pub fn get_db_pool() -> Result<PgPool> {
let pool_r = PG_POOL.read().unwrap(); let pool_r = PG_POOL.read().unwrap();
let pool = pool_r.as_ref().unwrap().clone(); let pool = pool_r
pool .as_ref()
.ok_or_else(|| anyhow!("PostgreSQL connection pool is not initialized (yet)"))?
.clone();
Ok(pool)
} }
pub fn get_db_conn() -> Result<PgPoolConnection> { pub fn get_db_conn() -> Result<PgPoolConnection> {
let pool = get_db_pool(); let pool = get_db_pool()?;
pool.get().context("Get connection from pool error") Ok(pool.get()?)
} }
pub fn get_redis_conn() -> Result<RedisPoolConnection> { pub fn get_redis_conn() -> Result<RedisPoolConnection> {
let pool_r = REDIS_POOL.read().unwrap(); let pool_r = REDIS_POOL.read().unwrap();
let pool = pool_r.as_ref().unwrap(); let pool = pool_r
.as_ref()
.ok_or_else(|| anyhow!("Redis connection pool is not initialized (yet)"))?;
Ok(match pool { Ok(match pool {
RedisPool::Client(v) => RedisPoolConnection::Client(v.get()?), RedisPool::Client(v) => RedisPoolConnection::Client(v.get()?),
RedisPool::ClusterClient(v) => RedisPoolConnection::ClusterClient(v.get()?), RedisPool::ClusterClient(v) => RedisPoolConnection::ClusterClient(v.get()?),