Skip to content

Commit 176c5c3

Browse files
committed
Pass DB connection string to ClientPool
1 parent 2ecf243 commit 176c5c3

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/db.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ lazy_static::lazy_static! {
2828
pub struct ClientPool {
2929
connections: Arc<Mutex<Vec<tokio_postgres::Client>>>,
3030
permits: Arc<Semaphore>,
31+
db_url: String,
3132
}
3233

3334
pub struct PooledClient {
@@ -59,10 +60,11 @@ impl std::ops::DerefMut for PooledClient {
5960
}
6061

6162
impl ClientPool {
62-
pub fn new() -> ClientPool {
63+
pub fn new(db_url: String) -> ClientPool {
6364
ClientPool {
6465
connections: Arc::new(Mutex::new(Vec::with_capacity(16))),
6566
permits: Arc::new(Semaphore::new(16)),
67+
db_url,
6668
}
6769
}
6870

@@ -84,15 +86,14 @@ impl ClientPool {
8486
}
8587

8688
PooledClient {
87-
client: Some(make_client().await.unwrap()),
89+
client: Some(make_client(&self.db_url).await.unwrap()),
8890
permit,
8991
pool: self.connections.clone(),
9092
}
9193
}
9294
}
9395

94-
async fn make_client() -> anyhow::Result<tokio_postgres::Client> {
95-
let db_url = std::env::var("DATABASE_URL").expect("needs DATABASE_URL");
96+
async fn make_client(db_url: &str) -> anyhow::Result<tokio_postgres::Client> {
9697
if db_url.contains("rds.amazonaws.com") {
9798
let mut builder = TlsConnector::builder();
9899
for cert in make_certificates() {

src/main.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ async fn serve_req(
242242
}
243243

244244
async fn run_server(addr: SocketAddr) -> anyhow::Result<()> {
245-
let pool = db::ClientPool::new();
245+
let db_url = std::env::var("DATABASE_URL").expect("needs DATABASE_URL");
246+
let pool = db::ClientPool::new(db_url.clone());
246247
db::run_migrations(&mut *pool.get().await)
247248
.await
248249
.context("database migrations")?;
@@ -271,7 +272,7 @@ async fn run_server(addr: SocketAddr) -> anyhow::Result<()> {
271272

272273
// Run all jobs that have a schedule (recurring jobs)
273274
if !is_scheduled_jobs_disabled() {
274-
spawn_job_scheduler();
275+
spawn_job_scheduler(db_url);
275276
spawn_job_runner(ctx.clone());
276277
}
277278

@@ -361,11 +362,12 @@ async fn spawn_job_oneoffs(ctx: Arc<Context>) {
361362
/// The scheduler wakes up every `JOB_SCHEDULING_CADENCE_IN_SECS` seconds to
362363
/// check if there are any jobs ready to run. Jobs get inserted into the the
363364
/// database which acts as a queue.
364-
fn spawn_job_scheduler() {
365+
fn spawn_job_scheduler(db_url: String) {
365366
task::spawn(async move {
366367
loop {
368+
let db_url = db_url.clone();
367369
let res = task::spawn(async move {
368-
let pool = db::ClientPool::new();
370+
let pool = db::ClientPool::new(db_url);
369371
let mut interval =
370372
time::interval(time::Duration::from_secs(JOB_SCHEDULING_CADENCE_IN_SECS));
371373

0 commit comments

Comments
 (0)