Skip to content

bin/background-worker: Simplify restart behavior #7459

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 2 commits into from
Nov 7, 2023
Merged
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
34 changes: 11 additions & 23 deletions src/bin/background-worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,35 +85,23 @@ fn main() -> anyhow::Result<()> {

let environment = Arc::new(environment);

let build_runner = || {
let connection_pool = r2d2::Pool::builder()
.max_size(10)
.min_idle(Some(0))
.build_unchecked(ConnectionManager::new(&db_url));
let connection_pool = r2d2::Pool::builder()
.max_size(10)
.min_idle(Some(0))
.build_unchecked(ConnectionManager::new(&db_url));

let connection_pool = DieselPool::new_background_worker(connection_pool);
let connection_pool = DieselPool::new_background_worker(connection_pool);

Runner::new(connection_pool, environment.clone())
.num_workers(5)
.job_start_timeout(Duration::from_secs(job_start_timeout))
.register_crates_io_job_types()
};

let mut runner = build_runner();
let runner = Runner::new(connection_pool, environment.clone())
.num_workers(5)
.job_start_timeout(Duration::from_secs(job_start_timeout))
.register_crates_io_job_types();

info!("Runner booted, running jobs");

let mut failure_count = 0;

loop {
if let Err(e) = runner.run_all_pending_jobs() {
failure_count += 1;
if failure_count < 5 {
warn!(?failure_count, err = ?e, "Error running jobs -- retrying");
runner = build_runner();
} else {
panic!("Failed to begin running jobs 5 times. Restarting the process");
}
if let Err(err) = runner.run_all_pending_jobs() {
warn!(%err, "Failed to run background jobs");
}
sleep(Duration::from_secs(1));
}
Expand Down