Skip to content

worker: Inline connection() fn #8384

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 29, 2024
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
10 changes: 3 additions & 7 deletions crates/crates_io_worker/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::worker::Worker;
use crate::{storage, BackgroundJob};
use anyhow::anyhow;
use diesel::prelude::*;
use diesel::r2d2::{ConnectionManager, Pool, PoolError, PooledConnection};
use diesel::r2d2::{ConnectionManager, Pool};
use futures_util::future::join_all;
use std::collections::HashMap;
use std::sync::Arc;
Expand All @@ -16,7 +16,6 @@ use tracing::{info, info_span, warn, Instrument};
const DEFAULT_POLL_INTERVAL: Duration = Duration::from_secs(1);

pub type ConnectionPool = Pool<ConnectionManager<PgConnection>>;
pub type PooledConn = PooledConnection<ConnectionManager<PgConnection>>;

/// The core runner responsible for locking and running jobs
pub struct Runner<Context> {
Expand Down Expand Up @@ -99,16 +98,13 @@ impl<Context: Clone + Send + Sync + 'static> Runner<Context> {
RunHandle { handles }
}

pub fn connection(&self) -> Result<PooledConn, PoolError> {
self.connection_pool.get()
}

/// Check if any jobs in the queue have failed.
///
/// This function is intended for use in tests and will return an error if
/// any jobs have failed.
pub fn check_for_failed_jobs(&self) -> anyhow::Result<()> {
let failed_jobs = storage::failed_job_count(&mut *self.connection()?)?;
let mut conn = self.connection_pool.get()?;
let failed_jobs = storage::failed_job_count(&mut conn)?;
if failed_jobs == 0 {
Ok(())
} else {
Expand Down