Skip to content

Commit 764d799

Browse files
committed
Sleep between github requests
And add github_updater to main executable file
1 parent fe37b70 commit 764d799

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/bin/cratesfyi.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ pub fn main() {
8787
.about("Database operations")
8888
.subcommand(SubCommand::with_name("init")
8989
.about("Initialize database. Currently \
90-
only creates tables in database.")))
90+
only creates tables in database."))
91+
.subcommand(SubCommand::with_name("update-github-fields")
92+
.about("Updates github stats for crates.")))
9193
.get_matches();
9294

9395

@@ -157,6 +159,8 @@ pub fn main() {
157159
writeln!(&mut io::stderr(), "Failed to initialize database: {}", err).unwrap();
158160
process::exit(1);
159161
}
162+
} else if let Some(_) = matches.subcommand_matches("update-github-fields") {
163+
cratesfyi::utils::github_updater().expect("Failed to update github fields");
160164
}
161165
} else {
162166
println!("{}", matches.usage());

src/utils/github_updater.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ pub fn github_updater() -> Result<(), DocBuilderError> {
2323

2424
// TODO: This query assumes repository field in Cargo.toml is
2525
// always the same across all versions of a crate
26-
for row in &try!(conn.query("SELECT DISTINCT ON (crates.name) crate.name, crates.id, \
27-
repository_url FROM crates, releases WHERE releases.crate_id \
28-
= crates.id AND repository_url ~ '^https*://github.com' AND \
29-
github_last_update < NOW() + INTERVAL '1 day'",
26+
for row in &try!(conn.query("SELECT DISTINCT ON (crates.name) \
27+
crates.name, \
28+
crates.id, \
29+
releases.repository_url \
30+
FROM crates \
31+
INNER JOIN releases ON releases.crate_id = crates.id \
32+
WHERE releases.repository_url ~ '^https*://github.com' AND \
33+
(crates.github_last_update < NOW() - INTERVAL '1 day' OR \
34+
crates.github_last_update IS NULL)",
3035
&[])) {
3136
let crate_name: String = row.get(0);
3237
let crate_id: i32 = row.get(1);
@@ -51,6 +56,11 @@ pub fn github_updater() -> Result<(), DocBuilderError> {
5156
}) {
5257
error!("Failed to update github fields of: {} {}", crate_name, err);
5358
}
59+
60+
// sleep for rate limits
61+
use std::thread;
62+
use std::time::Duration;
63+
thread::sleep(Duration::from_secs(2));
5464
}
5565

5666
Ok(())

0 commit comments

Comments
 (0)