Skip to content

Create a new DB pool when rebuilding the worker #2267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/background_jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,23 @@ impl Environment {
connection_pool: DieselPool,
uploader: Uploader,
http_client: Client,
) -> Self {
Self::new_shared(
Arc::new(Mutex::new(index)),
connection_pool,
uploader,
http_client,
)
}

pub fn new_shared(
index: Arc<Mutex<Repository>>,
connection_pool: DieselPool,
uploader: Uploader,
http_client: Client,
) -> Self {
Self {
index: Arc::new(Mutex::new(index)),
index,
connection_pool: AssertUnwindSafe(connection_pool),
uploader,
http_client: AssertUnwindSafe(http_client),
Expand Down
32 changes: 19 additions & 13 deletions src/bin/background-worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use cargo_registry::git::{Repository, RepositoryConfig};
use cargo_registry::{background_jobs::*, db};
use diesel::r2d2;
use reqwest::blocking::Client;
use std::sync::{Arc, Mutex};
use std::thread::sleep;
use std::time::Duration;

Expand All @@ -24,15 +25,6 @@ fn main() {

let config = cargo_registry::Config::default();

// 2x the thread pool size -- not all our jobs need a DB connection,
// but we want to always be able to run our jobs in parallel, rather
// than adjusting based on how many concurrent jobs need a connection.
// Eventually swirl will do this for us, and this will be the default
// -- we should just let it do a thread pool size of CPU count, and a
// a connection pool size of 2x that when that lands.
let db_config = r2d2::Pool::builder().max_size(4);
let db_pool = db::diesel_pool(&config.db_url, config.env, db_config);

let job_start_timeout = dotenv::var("BACKGROUND_JOB_TIMEOUT")
.unwrap_or_else(|_| "30".into())
.parse()
Expand All @@ -41,13 +33,27 @@ fn main() {
println!("Cloning index");

let repository_config = RepositoryConfig::from_environment();
let repository = Repository::open(&repository_config).expect("Failed to clone index");
let repository = Arc::new(Mutex::new(
Repository::open(&repository_config).expect("Failed to clone index"),
));
println!("Index cloned");

let environment = Environment::new(repository, db_pool.clone(), config.uploader, Client::new());

let build_runner = || {
swirl::Runner::builder(db_pool.clone(), environment.clone())
// 2x the thread pool size -- not all our jobs need a DB connection,
// but we want to always be able to run our jobs in parallel, rather
// than adjusting based on how many concurrent jobs need a connection.
// Eventually swirl will do this for us, and this will be the default
// -- we should just let it do a thread pool size of CPU count, and a
// a connection pool size of 2x that when that lands.
let db_config = r2d2::Pool::builder().max_size(4);
let db_pool = db::diesel_pool(&config.db_url, config.env, db_config);
let environment = Environment::new_shared(
repository.clone(),
db_pool.clone(),
config.uploader.clone(),
Client::new(),
);
swirl::Runner::builder(db_pool, environment)
.thread_count(2)
.job_start_timeout(Duration::from_secs(job_start_timeout))
.build()
Expand Down