Skip to content

Move update-downloads to a background job #1798

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
Aug 7, 2019
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
17 changes: 17 additions & 0 deletions src/bin/enqueue-job.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use cargo_registry::util::{CargoError, CargoResult};
use cargo_registry::{db, tasks};
use std::env::args;
use swirl::Job;

fn main() -> CargoResult<()> {
let conn = db::connect_now()?;

match &*args().nth(1).unwrap_or_default() {
"update_downloads" => tasks::update_downloads()
.enqueue(&conn)
.map_err(|e| CargoError::from_std_error(e))?,
other => panic!("Unrecognized job type `{}`", other),
};

Ok(())
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub mod middleware;
mod publish_rate_limit;
pub mod render;
pub mod schema;
pub mod tasks;
mod test_util;
pub mod uploaders;
pub mod util;
Expand Down
3 changes: 3 additions & 0 deletions src/tasks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod update_downloads;

pub use update_downloads::update_downloads;
34 changes: 15 additions & 19 deletions src/bin/update-downloads.rs → src/tasks/update_downloads.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
#![deny(warnings, clippy::all, rust_2018_idioms)]

#[macro_use]
extern crate diesel;

use cargo_registry::{
db,
use crate::{
background_jobs::Environment,
models::VersionDownload,
schema::{crates, metadata, version_downloads, versions},
util::CargoResult,
};

use diesel::prelude::*;
use swirl::PerformError;

fn main() -> CargoResult<()> {
let conn = db::connect_now()?;
#[swirl::background_job]
pub fn update_downloads(env: &Environment) -> Result<(), PerformError> {
let conn = env.connection()?;
update(&conn)?;
Ok(())
}

fn update(conn: &PgConnection) -> QueryResult<()> {
use crate::version_downloads::dsl::*;
use self::version_downloads::dsl::*;
use diesel::dsl::now;
use diesel::select;

Expand Down Expand Up @@ -84,7 +80,7 @@ fn collect(conn: &PgConnection, rows: &[VersionDownload]) -> QueryResult<()> {
#[cfg(test)]
mod test {
use super::*;
use cargo_registry::{
use crate::{
env,
models::{Crate, NewCrate, NewUser, NewVersion, User, Version},
};
Expand Down Expand Up @@ -143,7 +139,7 @@ mod test {
.execute(&conn)
.unwrap();

crate::update(&conn).unwrap();
super::update(&conn).unwrap();
let version_downloads = versions::table
.find(version.id)
.select(versions::downloads)
Expand All @@ -154,7 +150,7 @@ mod test {
.select(crates::downloads)
.first(&conn);
assert_eq!(Ok(1), crate_downloads);
crate::update(&conn).unwrap();
super::update(&conn).unwrap();
let version_downloads = versions::table
.find(version.id)
.select(versions::downloads)
Expand All @@ -179,7 +175,7 @@ mod test {
))
.execute(&conn)
.unwrap();
crate::update(&conn).unwrap();
super::update(&conn).unwrap();
let processed = version_downloads::table
.filter(version_downloads::version_id.eq(version.id))
.select(version_downloads::processed)
Expand All @@ -203,7 +199,7 @@ mod test {
))
.execute(&conn)
.unwrap();
crate::update(&conn).unwrap();
super::update(&conn).unwrap();
let processed = version_downloads::table
.filter(version_downloads::version_id.eq(version.id))
.select(version_downloads::processed)
Expand Down Expand Up @@ -253,7 +249,7 @@ mod test {
.filter(crates::id.eq(krate.id))
.first::<Crate>(&conn)
.unwrap();
crate::update(&conn).unwrap();
super::update(&conn).unwrap();
let version2 = versions::table
.find(version.id)
.first::<Version>(&conn)
Expand All @@ -266,7 +262,7 @@ mod test {
.unwrap();
assert_eq!(krate2.downloads, 2);
assert_eq!(krate2.updated_at, krate_before.updated_at);
crate::update(&conn).unwrap();
super::update(&conn).unwrap();
let version3 = versions::table
.find(version.id)
.first::<Version>(&conn)
Expand Down Expand Up @@ -301,7 +297,7 @@ mod test {
.execute(&conn)
.unwrap();

crate::update(&conn).unwrap();
super::update(&conn).unwrap();
let versions_changed = versions::table
.select(versions::updated_at.ne(now - 2.days()))
.get_result(&conn);
Expand Down