Switch to logging.

This commit is contained in:
Tom Alexander 2022-05-08 17:03:49 -04:00
parent 58aa8b249d
commit df00268d35
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
5 changed files with 117 additions and 11 deletions

99
Cargo.lock generated
View File

@ -13,6 +13,15 @@ dependencies = [
"version_check",
]
[[package]]
name = "aho-corasick"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
"memchr",
]
[[package]]
name = "atoi"
version = "0.4.0"
@ -22,6 +31,17 @@ dependencies = [
"num-traits",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.1.0"
@ -169,6 +189,19 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "env_logger"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]]
name = "fastrand"
version = "1.7.0"
@ -310,6 +343,8 @@ dependencies = [
name = "github_watcher"
version = "0.1.0"
dependencies = [
"log",
"pretty_env_logger",
"reqwest",
"serde_json",
"sqlx",
@ -411,6 +446,15 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421"
[[package]]
name = "humantime"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
dependencies = [
"quick-error",
]
[[package]]
name = "hyper"
version = "0.14.18"
@ -543,9 +587,9 @@ dependencies = [
[[package]]
name = "log"
version = "0.4.16"
version = "0.4.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6389c490849ff5bc16be905ae24bc913a9c8892e19b2341dbc175e14c341c2b8"
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
dependencies = [
"cfg-if",
]
@ -796,6 +840,16 @@ version = "0.3.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
[[package]]
name = "pretty_env_logger"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "926d36b9553851b8b0005f1275891b392ee4d2d833852c417ed025477350fb9d"
dependencies = [
"env_logger",
"log",
]
[[package]]
name = "proc-macro2"
version = "1.0.37"
@ -805,6 +859,12 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "quick-error"
version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
[[package]]
name = "quote"
version = "1.0.17"
@ -823,6 +883,23 @@ dependencies = [
"bitflags",
]
[[package]]
name = "regex"
version = "1.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "remove_dir_all"
version = "0.5.3"
@ -1169,6 +1246,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "termcolor"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [
"winapi-util",
]
[[package]]
name = "thiserror"
version = "1.0.30"
@ -1527,6 +1613,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

View File

@ -4,6 +4,8 @@ version = "0.1.0"
edition = "2021"
[dependencies]
log = "0.4.17"
pretty_env_logger = "0.4.0"
reqwest = { version = "0.11.10", features = ["json"] }
serde_json = "1.0.81"
sqlx = { version = "0.5", features = [ "runtime-tokio-rustls", "sqlite", "migrate" ] }

View File

@ -45,12 +45,12 @@ impl GithubEndpointWatcher {
.header("Accept", "application/vnd.github.v3+json");
if let Some(etag) = &self.etag {
let trimmed = etag.to_str()?.trim_matches('"');
println!("Setting etag to {}", trimmed);
info!("Setting etag to {}", trimmed);
request = request.header(reqwest::header::ETAG, trimmed);
}
if let Some(last_modified_at) = &self.last_modified_at {
// let trimmed = etag.to_str()?.trim_matches('"');
println!(
info!(
"Setting If-Modified-Since to {}",
last_modified_at.to_str()?
);
@ -59,7 +59,7 @@ impl GithubEndpointWatcher {
self.sleep_until_next_poll().await?;
self.sleep_until_ratelimit().await?;
let request_started_at = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs();
println!("Hitting url {}", self.url);
info!("Hitting url {}", self.url);
let response = request.send().await?;
let headers = response.headers();
@ -77,13 +77,18 @@ impl GithubEndpointWatcher {
if let reqwest::StatusCode::NOT_MODIFIED = response.status() {
self.etag = etag;
self.last_modified_at = last_modified_at;
let poll_interval_parsed: u64 = poll_interval.unwrap_or_else(|| {
info!("No poll interval returned, defaulting to 5 minute polling.");
300
});
self.next_poll_allowed = request_started_at + poll_interval_parsed;
return Ok(None);
} else {
let body = response.json::<serde_json::Value>().await?;
self.etag = etag;
self.last_modified_at = last_modified_at;
let poll_interval_parsed: u64 = poll_interval.unwrap_or_else(|| {
println!("No poll interval returned, defaulting to 5 minute polling.");
info!("No poll interval returned, defaulting to 5 minute polling.");
300
});
self.next_poll_allowed = request_started_at + poll_interval_parsed;

View File

@ -81,21 +81,21 @@ impl<'a> GithubCtl<'a> {
let api_result = match endpoint_watcher.get_results().await {
Ok(result) => result,
Err(e) => {
println!("Failed to get results. {}", e);
error!("Failed to get results. {}", e);
return;
}
};
if let Some(serde_json::Value::Array(events)) = api_result {
for event in events {
if let Err(_) = event_stream.send(event).await {
println!("Receiver dropped.");
error!("Receiver dropped.");
return;
}
}
} else if let None = api_result {
println!("No new results available.");
info!("No new results available.");
} else {
println!("Unsupported JSON type.");
error!("Unsupported JSON type.");
}
}
});

View File

@ -1,3 +1,6 @@
#[macro_use]
extern crate log;
mod githubctl;
use serde_json;
@ -9,10 +12,11 @@ const REPO: &'static str = include_str!("../.repo");
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
pretty_env_logger::init();
let mut github = githubctl::GithubCtl::new(USERNAME, TOKEN)?;
github.watch_repo(ORG, REPO).await?;
loop {
let event = github.get_event().await?;
// println!("{}", serde_json::to_string(&event)?);
println!("{}", serde_json::to_string(&event)?);
}
}