diff --git a/Cargo.toml b/Cargo.toml index 9d94d14..7d4469b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,4 @@ chrono = "0.4.23" google-youtube3 = "4.0" serde_json = "1.0.91" rand = "0.8.5" +log = "0.4.17" diff --git a/src/bigquery.rs b/src/bigquery.rs index b9267dc..ecac698 100644 --- a/src/bigquery.rs +++ b/src/bigquery.rs @@ -1,3 +1 @@ -use std::error::Error; - //TODO: implement bigquery backoff diff --git a/src/errors.rs b/src/errors.rs index 25bb98f..43c3a69 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,5 +1,7 @@ use std::fmt; +use log::warn; + #[derive(Debug, Clone)] pub struct BackoffError { message: String, @@ -8,6 +10,7 @@ pub struct BackoffError { impl BackoffError { pub fn new>(message: S) -> BackoffError { let message = message.into(); + warn!("BackoffError: {}", message); BackoffError { message } } } diff --git a/src/lib.rs b/src/lib.rs index 44550dc..5dce01a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,7 @@ use rand::Rng; +use log::{trace, info}; + const EXTRA_BUFFER_TIME: u64 = 100; pub enum Api { @@ -21,6 +23,11 @@ async fn sleep_for_backoff_time(backoff_time: u64, with_extra_buffer_time: bool) true => EXTRA_BUFFER_TIME, false => 0, }; + trace!( + "sleep_for_backoff_time->backoff_time: {}, extra_buffer_time: {}", + backoff_time, + extra_buffer_time + ); //convert to milliseconds let backoff_time = backoff_time * 1000; @@ -28,7 +35,7 @@ async fn sleep_for_backoff_time(backoff_time: u64, with_extra_buffer_time: bool) //add some random extra time for good measure (in milliseconds) let random_extra = rand::thread_rng().gen_range(0..100); let total_millis = backoff_time + extra_buffer_time + random_extra; - println!( + info!( "sleep_for_backoff_time->Sleeping for {} milliseconds", total_millis ); diff --git a/src/twitch.rs b/src/twitch.rs index d2c76ef..eb8e56c 100644 --- a/src/twitch.rs +++ b/src/twitch.rs @@ -1,15 +1,14 @@ use std::error::Error; use chrono::NaiveDateTime; -use reqwest::header::{HeaderMap, HeaderValue}; -use reqwest::{Body, Client, IntoUrl, Request, RequestBuilder, Response}; +use reqwest::{Body, Client, IntoUrl, Request, Response}; +use reqwest::header::HeaderMap; + +use log::{info, trace}; use crate::errors::BackoffError; use crate::sleep_for_backoff_time; -enum ErrorTypes { - E429(String), -} //region reqwest //region convenience functions @@ -46,7 +45,9 @@ pub async fn check_backoff_twitch_post_with_client>( body: Option, client: &Client, ) -> Result> { - let mut request = client.post(url.into_url()?); + let url = url.into_url()?; + trace!("check_backoff_twitch_post_with_client {:?}", url); + let mut request = client.post(url); if let Some(headers) = headers { request = request.headers(headers); @@ -71,7 +72,10 @@ pub async fn check_backoff_twitch_with_client( request: Request, client: &Client, ) -> Result> { + trace!("check_backoff_twitch_with_client {:?}", request); + let mut counter = 0; loop { + trace!("check_backoff_twitch_with_client loop ({})", counter); let r: Request = request .try_clone() .ok_or::("Request is None".into())?; @@ -95,10 +99,13 @@ pub async fn check_backoff_twitch_with_client( _ => return Ok(response), // _ => todo!("Handle other errors or "), } + + counter += 1; } } async fn handle_e429(value: String) -> Result<(), Box> { + trace!("handle_e429 {}", value); let value = value.parse::()?; let timestamp = NaiveDateTime::from_timestamp_opt(value, 0).ok_or(BackoffError::new( format!("Could not convert the provided timestamp: {}", value), @@ -110,7 +117,7 @@ async fn handle_e429(value: String) -> Result<(), Box> { } let duration = timestamp - now; let duration = duration.num_seconds() as u64; - println!("Sleeping for {} seconds", duration); + info!("Sleeping for {} seconds", duration); sleep_for_backoff_time(duration, true).await; //TODO: test this somehow Ok(()) diff --git a/src/youtube.rs b/src/youtube.rs index e98f831..8f4fd21 100644 --- a/src/youtube.rs +++ b/src/youtube.rs @@ -1,8 +1,8 @@ use std::error::Error; use std::future::Future; -use std::path::{Path, PathBuf}; -use google_youtube3::api::Video; +use log::{warn, info, trace}; + use google_youtube3::Error::BadRequest; use google_youtube3::hyper::{Body, Response}; use google_youtube3::hyper::client::HttpConnector; @@ -31,23 +31,25 @@ pub async fn generic_check_backoff_youtube<'a, 'b, 'c, ( client: &'a YouTube>, para: &'b Para, - function: impl Fn(&'a YouTube>, &'b Para) -> Fut + function: impl Fn(&'a YouTube>, &'b Para) -> Fut, ) -> Result, T)>, Box> { + trace!("generic_check_backoff_youtube"); let mut backoff = 0; let mut res: google_youtube3::Result<(Response, T)>; 'try_upload: loop { + trace!("generic_check_backoff_youtube loop ({})", backoff); res = function(&client, para).await; match res { Ok(_) => break 'try_upload, Err(e) => { - println!("Error: {}", e); + warn!("Error: {}", e); if let BadRequest(e1) = &e { let is_quota_error = get_is_quota_error(&e1); if is_quota_error { - println!("quota_error: {}", e); + info!("quota_error: {}", e); backoff += 1; if !wait_for_backoff(backoff).await { return Err(e.into()); @@ -67,8 +69,9 @@ pub async fn generic_check_backoff_youtube<'a, 'b, 'c, } async fn wait_for_backoff<'a>(backoff: u32) -> bool { + trace!("wait_for_backoff ({})", backoff); let mut backoff_time = YOUTUBE_DEFAULT_BACKOFF_TIME_S.pow(backoff); - println!("backoff_time: {}", backoff_time); + info!("backoff_time: {}", backoff_time); if backoff as u64 > YOUTUBE_MAX_TRIES { return false; } @@ -112,6 +115,7 @@ async fn wait_for_backoff<'a>(backoff: u32) -> bool { // } fn get_is_quota_error(e: &serde_json::value::Value) -> bool { + trace!("get_is_quota_error"); let is_quota_error = e.get("error") .and_then(|e| e.get("errors")) .and_then(|e| e.get(0))