Skip to content
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

Add more logging to background jobs #2066

Merged
merged 1 commit into from
Jan 5, 2020
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
1 change: 1 addition & 0 deletions src/bin/background-worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fn main() {

let repository_config = RepositoryConfig::from_environment();
let repository = Repository::open(&repository_config).expect("Failed to clone index");
println!("Index cloned");

let environment = Environment::new(
repository,
Expand Down
6 changes: 5 additions & 1 deletion src/bin/enqueue-job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use diesel::PgConnection;
fn main() -> AppResult<()> {
let conn = db::connect_now()?;
let mut args = std::env::args().skip(1);
match &*args.next().unwrap_or_default() {

let job = args.next().unwrap_or_default();
println!("Enqueueing background job: {}", job);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When called locally without a command-line argument, the eror output isn't great, but I guess it's good enough – this isn't a tool meant to be used by many end users.


match &*job {
"update_downloads" => tasks::update_downloads().enqueue(&conn),
"dump_db" => {
let database_url = args.next().unwrap_or_else(|| env("DATABASE_URL"));
Expand Down
14 changes: 10 additions & 4 deletions src/tasks/dump_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ pub fn dump_db(
target_name: String,
) -> Result<(), PerformError> {
let directory = DumpDirectory::create()?;

println!("Begin exporting database");
directory.populate(&database_url)?;

println!("Creating tarball");
let tarball = DumpTarball::create(&directory.export_dir)?;
tarball.upload(&target_name, &env.uploader)?;
println!("Database dump uploaded to {}.", &target_name);

println!("Uploading tarball");
let size = tarball.upload(&target_name, &env.uploader)?;
println!("Database dump uploaded {} bytes to {}.", size, &target_name);
Ok(())
}

Expand Down Expand Up @@ -145,7 +151,7 @@ impl DumpTarball {
Ok(result)
}

fn upload(&self, target_name: &str, uploader: &Uploader) -> Result<(), PerformError> {
fn upload(&self, target_name: &str, uploader: &Uploader) -> Result<u64, PerformError> {
let client = reqwest::Client::new();
let tarfile = File::open(&self.tarball_path)?;
let content_length = tarfile.metadata()?.len();
Expand All @@ -160,7 +166,7 @@ impl DumpTarball {
header::HeaderMap::new(),
)
.map_err(std_error_no_send)?;
Ok(())
Ok(content_length)
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/tasks/update_downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ fn update(conn: &PgConnection) -> QueryResult<()> {
.filter(processed.eq(false))
.filter(downloads.ne(counted))
.load(conn)?;

println!("Updating {} versions", rows.len());
collect(conn, &rows)?;
println!("Finished updating versions");

// Anything older than 24 hours ago will be frozen and will not be queried
// against again.
Expand All @@ -33,17 +36,18 @@ fn update(conn: &PgConnection) -> QueryResult<()> {
.filter(downloads.eq(counted))
.filter(processed.eq(false))
.execute(conn)?;
println!("Finished freezing old version_downloads");

no_arg_sql_function!(refresh_recent_crate_downloads, ());
select(refresh_recent_crate_downloads).execute(conn)?;
println!("Finished running refresh_recent_crate_downloads");

Ok(())
}

fn collect(conn: &PgConnection, rows: &[VersionDownload]) -> QueryResult<()> {
use diesel::update;

println!("updating {} versions", rows.len());

for download in rows {
let amt = download.downloads - download.counted;

Expand Down