Skip to content

Commit 991f53c

Browse files
committed
Auto merge of #1677 - sgrif:sg-update-swirl, r=jtgeibel
Update swirl New features in this version: - Codegen for jobs which reduces boilerplate when defining them - Jobs no longer need to be registered with the runner - Rework of how `run_all_pending_jobs` works so errors loading the job actually bubble up - Configurable timeout for how long to wait for a job to begin running We aren't fully taking advantage of the timeout yet, I'd eventually like to try rebuilding the runner in-process a few times when an error happens, and then crash the process if it fails a few times in a row. This needs a bit more refactoring though (I'm not sure if we want to keep the `Repository` in memory like we are now, and whether we want to assume a full re-clone is needed on error). For now I've set it to ensure our jobs don't hang forever though. Warts in this version: - Rust thinks imports only used in the job body are unused. (sgrif/swirl#6) - Worked around for now by moving the imports into the job body - `swirl::Job` has to be imported by calling code - Codegen assumes that `serde::Deserialize` and `serde::Serialize` are available. (sgrif/swirl#9) Issues with this version: - The new impl of `run_all_pending_jobs` will return an error if the DB is in read only mode. If the DB is read only, we couldn't have enqueued jobs anyway, so the workaround is "fine", but we need some more robust APIs in swirl to fix this. This only affects us in tests. - sgrif/swirl#8
2 parents f41626e + be5f0de commit 991f53c

File tree

14 files changed

+195
-155
lines changed

14 files changed

+195
-155
lines changed

Cargo.lock

Lines changed: 62 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ dotenv = "0.11.0"
4848
toml = "0.4"
4949
diesel = { version = "1.4.0", features = ["postgres", "serde_json", "chrono", "r2d2"] }
5050
diesel_full_text_search = "1.0.0"
51-
swirl = { git = "https://github.com/sgrif/swirl.git", rev = "95d3a35bc39a7274335cad6d7cab64acd5eb3904" }
51+
swirl = { git = "https://github.com/sgrif/swirl.git", rev = "de5d8bb" }
5252
serde_json = "1.0.0"
53-
serde_derive = "1.0.0"
54-
serde = "1.0.0"
53+
serde = { version = "1.0.0", features = ["derive"] }
5554
chrono = { version = "0.4.0", features = ["serde"] }
5655
comrak = { version = "0.4.0", default-features = false }
5756
ammonia = "2.0.0"

src/background_jobs.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
use std::panic::AssertUnwindSafe;
22
use std::sync::{Mutex, MutexGuard};
3-
use swirl::{Builder, Runner};
43

54
use crate::db::{DieselPool, DieselPooledConn};
6-
use crate::git::{AddCrate, Repository, Yank};
5+
use crate::git::Repository;
76
use crate::util::errors::{CargoErrToStdErr, CargoResult};
87

9-
impl<'a> swirl::DieselPool<'a> for DieselPool {
8+
impl<'a> swirl::db::BorrowedConnection<'a> for DieselPool {
109
type Connection = DieselPooledConn<'a>;
10+
}
11+
12+
impl swirl::db::DieselPool for DieselPool {
1113
type Error = CargoErrToStdErr;
1214

13-
fn get(&'a self) -> Result<Self::Connection, Self::Error> {
15+
fn get(&self) -> Result<swirl::db::DieselPooledConn<'_, Self>, Self::Error> {
1416
self.get().map_err(CargoErrToStdErr)
1517
}
1618
}
1719

18-
pub fn job_runner(config: Builder<Environment, DieselPool>) -> Runner<Environment, DieselPool> {
19-
config.register::<AddCrate>().register::<Yank>().build()
20-
}
21-
2220
#[allow(missing_debug_implementations)]
2321
pub struct Environment {
2422
index: Mutex<Repository>,

src/bin/background-worker.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ fn main() {
3737

3838
let environment = Environment::new(repository, credentials, db_pool.clone());
3939

40-
let builder = swirl::Runner::builder(db_pool, environment).thread_count(1);
41-
let runner = job_runner(builder);
40+
let runner = swirl::Runner::builder(db_pool, environment)
41+
.thread_count(1)
42+
.job_start_timeout(Duration::from_secs(10))
43+
.build();
4244

4345
println!("Runner booted, running jobs");
4446

src/bin/monitor.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
77
#![deny(warnings)]
88

9-
#[macro_use]
10-
extern crate serde_derive;
11-
129
mod on_call;
1310

1411
use cargo_registry::{db, util::CargoResult};

src/bin/on_call/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use cargo_registry::util::{internal, CargoResult};
22

33
use reqwest::{header, StatusCode as Status};
44

5-
#[derive(Serialize, Debug)]
5+
#[derive(serde::Serialize, Debug)]
66
#[serde(rename_all = "snake_case", tag = "event_type")]
77
pub enum Event {
88
Trigger {
@@ -54,14 +54,14 @@ impl Event {
5454
}
5555
}
5656

57-
#[derive(Serialize, Debug)]
57+
#[derive(serde::Serialize, Debug)]
5858
struct FullEvent {
5959
service_key: String,
6060
#[serde(flatten)]
6161
event: Event,
6262
}
6363

64-
#[derive(Deserialize, Debug)]
64+
#[derive(serde::Deserialize, Debug)]
6565
struct InvalidEvent {
6666
message: String,
6767
errors: Vec<String>,

src/bin/render-readmes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#![deny(warnings)]
77

88
#[macro_use]
9-
extern crate serde_derive;
9+
extern crate serde;
1010

1111
use cargo_registry::{
1212
db,

src/bin/test-pagerduty.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
88
#![deny(warnings)]
99

10-
#[macro_use]
11-
extern crate serde_derive;
12-
1310
mod on_call;
1411

1512
use std::env::args;

src/controllers/krate/publish.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
//! Functionality related to publishing a new crate or version of a crate.
22
3+
use hex::ToHex;
34
use std::collections::HashMap;
45
use std::sync::Arc;
5-
6-
use hex::ToHex;
7-
8-
use crate::git;
9-
use crate::render;
10-
use crate::util::{internal, ChainError, Maximums};
11-
use crate::util::{read_fill, read_le_u32};
6+
use swirl::Job;
127

138
use crate::controllers::prelude::*;
9+
use crate::git;
1410
use crate::models::dependency;
1511
use crate::models::{Badge, Category, Keyword, NewCrate, NewVersion, Rights, User};
12+
use crate::render;
13+
use crate::util::{internal, CargoError, ChainError, Maximums};
14+
use crate::util::{read_fill, read_le_u32};
1615
use crate::views::{EncodableCrateUpload, GoodCrate, PublishWarnings};
1716

1817
/// Handles the `PUT /crates/new` route.
@@ -196,12 +195,15 @@ pub fn publish(req: &mut dyn Request) -> CargoResult<Response> {
196195
yanked: Some(false),
197196
links,
198197
};
199-
git::add_crate(&conn, git_crate).chain_error(|| {
200-
internal(&format_args!(
201-
"could not add crate `{}` to the git repo",
202-
name
203-
))
204-
})?;
198+
git::add_crate(git_crate)
199+
.enqueue(&conn)
200+
.map_err(|e| CargoError::from_std_error(e))
201+
.chain_error(|| {
202+
internal(&format_args!(
203+
"could not add crate `{}` to the git repo",
204+
name
205+
))
206+
})?;
205207

206208
// Now that we've come this far, we're committed!
207209
crate_bomb.path = None;

src/controllers/version/yank.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! Endpoints for yanking and unyanking specific versions of crates
22
3-
use crate::controllers::prelude::*;
3+
use swirl::Job;
44

5+
use super::version_and_crate;
6+
use crate::controllers::prelude::*;
57
use crate::git;
6-
78
use crate::models::Rights;
8-
9-
use super::version_and_crate;
9+
use crate::util::CargoError;
1010

1111
/// Handles the `DELETE /crates/:crate_id/:version/yank` route.
1212
/// This does not delete a crate version, it makes the crate
@@ -36,7 +36,9 @@ fn modify_yank(req: &mut dyn Request, yanked: bool) -> CargoResult<Response> {
3636
return Err(human("must already be an owner to yank or unyank"));
3737
}
3838

39-
git::yank(&conn, krate.name, version, yanked)?;
39+
git::yank(krate.name, version, yanked)
40+
.enqueue(&conn)
41+
.map_err(|e| CargoError::from_std_error(e))?;
4042

4143
#[derive(Serialize)]
4244
struct R {

0 commit comments

Comments
 (0)