Skip to content

Commit e580a6d

Browse files
committed
admin/enqueue_job: Enqueue SyncAdmin job only if none are in progress already
1 parent 2964485 commit e580a6d

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/admin/enqueue_job.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::schema::{background_jobs, crates};
33
use crate::worker::jobs;
44
use anyhow::Result;
55
use crates_io_worker::BackgroundJob;
6+
use diesel::dsl::exists;
67
use diesel::prelude::*;
78
use secrecy::{ExposeSecret, SecretString};
89

@@ -30,7 +31,11 @@ pub enum Command {
3031
#[arg()]
3132
name: String,
3233
},
33-
SyncAdmins,
34+
SyncAdmins {
35+
/// Force a sync even if one is already in progress
36+
#[arg(long)]
37+
force: bool,
38+
},
3439
}
3540

3641
pub fn run(command: Command) -> Result<()> {
@@ -59,7 +64,26 @@ pub fn run(command: Command) -> Result<()> {
5964
} => {
6065
jobs::DumpDb::new(database_url.expose_secret(), target_name).enqueue(conn)?;
6166
}
62-
Command::SyncAdmins => {
67+
Command::SyncAdmins { force } => {
68+
if !force {
69+
// By default, we don't want to enqueue a sync if one is already
70+
// in progress. If a sync fails due to e.g. an expired pinned
71+
// certificate we don't want to keep adding new jobs to the
72+
// queue, since the existing job will be retried until it
73+
// succeeds.
74+
75+
let query = background_jobs::table
76+
.filter(background_jobs::job_type.eq(jobs::SyncAdmins::JOB_NAME));
77+
78+
if diesel::select(exists(query)).get_result(conn)? {
79+
info!(
80+
"Did not enqueue {}, existing job already in progress",
81+
jobs::SyncAdmins::JOB_NAME
82+
);
83+
return Ok(());
84+
}
85+
}
86+
6387
jobs::SyncAdmins.enqueue(conn)?;
6488
}
6589
Command::DailyDbMaintenance => {

0 commit comments

Comments
 (0)