Skip to content

Commit

Permalink
refactor: prints replaced by logs
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowHatpro committed Sep 29, 2024
1 parent 22e4106 commit 85b1421
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 70 deletions.
31 changes: 31 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ config = "0.14.0"
once_cell = "1.19.0"
sentry = "0.34.0"
prometheus = {version = "0.13.4", features = ["push"]}
log = "0.4.14"
env_logger = "0.11.5"

[lib]
path = "src/lib.rs"
Expand Down
6 changes: 6 additions & 0 deletions config/default.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[logs]
debug = true
error = true
warning = true
info = true

[retry_task]
retry_interval = 86400 # 60*60*24 seconds
select_limit = 10
Expand Down
2 changes: 0 additions & 2 deletions config/development.example.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
debug = true

[wayback_machine_api]
myaccesskey = "OqvSkX8Sv5hblwrv"
mysecret = "tjow7aOFJAWokDsX"
Expand Down
2 changes: 0 additions & 2 deletions config/production.example.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
debug = false

[wayback_machine_api]
myaccesskey = "wayback-machine-access-key"
mysecret = "wayback-machine-secret"
Expand Down
23 changes: 11 additions & 12 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::archival;
use crate::archival::notifier::Notifier;
use crate::configuration::SETTINGS;
use crate::poller::Poller;
use log::{error, info};
use sqlx::PgPool;
use std::sync::Arc;
use std::time::Duration;
Expand All @@ -20,16 +21,16 @@ pub async fn start(pool: &PgPool) -> Result<(), sqlx::Error> {
);

if let Err(e) = poll_result {
eprintln!("Polling task failed: {:?}", e);
error!("Polling task failed: {:?}", e);
}
if let Err(e) = notify_result {
eprintln!("Notification task failed: {:?}", e);
error!("Notification task failed: {:?}", e);
}
if let Err(e) = listener_result {
eprintln!("Listener task failed: {:?}", e);
error!("Listener task failed: {:?}", e);
}
if let Err(e) = retry_and_cleanup_result {
eprintln!("Retry and cleanup failed: {:?}", e);
error!("Retry and cleanup failed: {:?}", e);
}

Ok(())
Expand All @@ -46,11 +47,8 @@ pub async fn spawn_poller_task(db_pool: PgPool) -> JoinHandle<()> {

tokio::spawn(async move {
if let Err(e) = poller.poll().await {
eprintln!("[POLLER] Task Failed, Error: {}", e);
sentry::capture_message(
format!("[POLLER] Task Failed, Error: {}", e).as_str(),
sentry::Level::Warning,
);
error!("[POLLER] Task Failed, Error: {}", e);
sentry::capture_error(&e);
}
})
}
Expand All @@ -72,7 +70,7 @@ async fn spawn_notification_task(db_pool: PgPool) -> JoinHandle<()> {

if notifier.should_notify().await {
if let Err(e) = notifier.notify().await {
eprintln!("[NOTIFIER] Task Failed, Error: {}", e);
error!("[NOTIFIER] Task Failed, Error: {}", e);
sentry::capture_error(&e);
};
}
Expand All @@ -89,7 +87,7 @@ async fn spawn_archiver_task(db_pool: PgPool) -> JoinHandle<()> {
.await
.unwrap_or_else(|e| {
sentry::capture_error(&e);
eprintln!("[LISTENER] Task Failed, Error {}", e)
error!("[LISTENER] Task Failed, Error {}", e)
})
})
}
Expand All @@ -104,6 +102,7 @@ async fn spawn_retry_and_cleanup_task(db_pool: PgPool) -> JoinHandle<()> {
while !db_pool.is_closed() {
interval.tick().await;
sentry::capture_message("[RETRY_AND_CLEANUP] Task started", sentry::Level::Info);
info!("[RETRY_AND_CLEANUP] Task started");
let archival_retry_task = archival::retry::start(db_pool.clone()).await;
match archival_retry_task {
Ok(_) => {
Expand All @@ -114,7 +113,7 @@ async fn spawn_retry_and_cleanup_task(db_pool: PgPool) -> JoinHandle<()> {
}
Err(e) => {
sentry::capture_error(&e);
eprintln!("[RETRY_AND_CLEANUP] Task Failed, Error: {}", e)
error!("[RETRY_AND_CLEANUP] Task Failed, Error: {}", e)
}
}
}
Expand Down
19 changes: 9 additions & 10 deletions src/archival/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::archival::utils::{

use crate::archival::error::ArchivalError;
use crate::configuration::SETTINGS;
use crate::debug_println;
use crate::metrics::Metrics;
use crate::structs::internet_archive_urls::{ArchivalStatus, InternetArchiveUrls};
use log::{error, info, warn};
use sentry::Level::Error;
use sqlx::postgres::PgListener;
use sqlx::PgPool;
Expand All @@ -21,7 +21,7 @@ pub async fn listen(pool: PgPool) -> Result<(), ArchivalError> {
loop {
while let Some(notification) = listener.try_recv().await? {
time::sleep(Duration::from_secs(SETTINGS.listen_task.listen_interval)).await;
debug_println!(
info!(
"[LISTENER] Received payload from archive_urls channel: {}",
notification.payload()
);
Expand All @@ -47,15 +47,15 @@ pub async fn handle_payload(
"[LISTENER] ARCHIVAL FAILED after 3 retry counts: internet_archive_urls id {}, URL: {}, reason: {}",
id, url, status_message
);
debug_println!("{}", status_ext);
warn!("{}", status_ext);
set_status_with_message(pool, id, ArchivalStatus::Failed as i32, status_ext.as_str())
.await?;
sentry::capture_message(status_ext.as_str(), Error);
metrics.record_archival_status("failed archival").await;
} else {
let archival_result = archive(url, url_row.id, pool).await;
if let Err(e) = archival_result {
eprintln!(
warn!(
"[LISTENER] ARCHIVAL ERROR: for internet_archive_urls id {}: {}",
url_row.id, e
);
Expand All @@ -78,7 +78,7 @@ pub async fn handle_payload(
pub async fn archive(url: String, id: i32, pool: &PgPool) -> Result<(), ArchivalError> {
let success = make_archival_network_request(url.as_str()).await?;
set_job_id_ia_url(pool, success.job_id.clone(), id).await?;
debug_println!("[LISTENER] ARCHIVAL REQUEST SUCCESSFUL: url: {}, internet_archive_url id: {} and Job Id: {}", url, id, success.job_id);
info!("[LISTENER] ARCHIVAL REQUEST SUCCESSFUL: url: {}, internet_archive_url id: {} and Job Id: {}", url, id, success.job_id);
let metrics = Metrics::new().await;
metrics.record_archival_status("archival started").await;

Expand All @@ -94,13 +94,12 @@ pub async fn archive(url: String, id: i32, pool: &PgPool) -> Result<(), Archival
inc_archive_request_retry_count(&status_pool, id)
.await
.unwrap_or_else(|e| {
eprintln!("[LISTENER] Could not increment archive request retry count for internet_archive_urls id: {}, error: {}", id, e);
error!("[LISTENER] Could not increment archive request retry count for internet_archive_urls id: {}, error: {}", id, e);
sentry::capture_error(&e);
});
debug_println!(
warn!(
"[LISTENER] STATUS CHECK ERROR: for internet_archive_urls id: {}, error: {}",
id,
e
id, e
);
set_status_with_message(
&status_pool,
Expand All @@ -110,7 +109,7 @@ pub async fn archive(url: String, id: i32, pool: &PgPool) -> Result<(), Archival
)
.await
.unwrap_or_else(|e| {
eprintln!(
error!(
"[LISTENER] Could not set status for internet_archive_urls id: {}, error: {}",
id, e
);
Expand Down
8 changes: 4 additions & 4 deletions src/archival/notifier.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::archival::utils::{get_first_id_to_start_notifier_from, is_row_exists};
use crate::debug_println;
use log::{debug, info};
use sqlx::{Error, PgPool};

pub struct Notifier {
Expand All @@ -11,7 +11,7 @@ impl Notifier {
pub async fn new(pool: PgPool) -> Notifier {
let start_notifier_from = get_first_id_to_start_notifier_from(pool.clone()).await;
if start_notifier_from.is_some() {
debug_println!("[NOTIFIER] starts from : {}", start_notifier_from.unwrap());
info!("[NOTIFIER] starts from : {}", start_notifier_from.unwrap());
}
Notifier {
start_notifier_from,
Expand All @@ -28,7 +28,7 @@ impl Notifier {
.bind(self.start_notifier_from)
.execute(&pool)
.await?;
debug_println!(
info!(
"[NOTIFIER] Adding internet_archive_urls id {} to the archive_urls channel",
self.start_notifier_from.unwrap()
);
Expand All @@ -40,7 +40,7 @@ impl Notifier {
Ok(())
} else {
//Case: It could be that there is no URL in InternetArchiveURL table when we call `notify`, so we check for the id here, to start notifier from it in the next notify call
debug_println!("[NOTIFIER] No row detected to archive, checking again");
debug!("[NOTIFIER] No row detected to archive, checking again");
self.start_notifier_from = get_first_id_to_start_notifier_from(self.pool.clone()).await;
Ok(())
}
Expand Down
28 changes: 14 additions & 14 deletions src/archival/retry.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::archival::utils::check_if_permanent_error;
use crate::configuration::SETTINGS;
use crate::debug_println;
use crate::structs::internet_archive_urls::{ArchivalStatus, InternetArchiveUrls};
use chrono::{Duration, Utc};
use log::info;
use sqlx::{Error, PgPool};
use std::ops::Sub;

Expand Down Expand Up @@ -31,7 +31,7 @@ pub async fn start(pool: PgPool) -> Result<(), Error> {
}
last_id += select_limit;
}
debug_println!("[RETRY_AND_CLEANUP] Task Complete");
info!("[RETRY_AND_CLEANUP] Task Complete");
Ok(())
}

Expand All @@ -56,10 +56,10 @@ pub async fn retry_and_cleanup_ia_row(
.bind(row.id)
.execute(pool)
.await?;
debug_println!(
info!(
"[RETRY_AND_CLEANUP] Removing row {} due to failed URL: {} after {} seconds",
row.id,
row.url.unwrap_or("<unknown>".to_string()),
row.url.unwrap_or("<null>".to_string()),
SETTINGS.retry_task.allow_remove_row_after
)
}
Expand All @@ -74,10 +74,10 @@ pub async fn retry_and_cleanup_ia_row(
.bind(row.id)
.execute(pool)
.await?;
debug_println!(
info!(
"[RETRY_AND_CLEANUP] Removing row {} containing URL: {} due to permanent error. status_ext: {}",
row.id,
row.url.unwrap_or("<unknown>".to_string()),
row.url.unwrap_or("<null>".to_string()),
status_ext
)
} else {
Expand All @@ -86,10 +86,10 @@ pub async fn retry_and_cleanup_ia_row(
.bind(row.id)
.execute(pool)
.await?;
debug_println!(
info!(
"[RETRY_AND_CLEANUP] Retrying notifying errored row {} containing URL: {}",
row.id,
row.url.unwrap_or("<unknown>".to_string())
row.url.unwrap_or("<null>".to_string())
)
}
}
Expand All @@ -105,26 +105,26 @@ pub async fn retry_and_cleanup_ia_row(
.bind(row.id)
.execute(pool)
.await?;
debug_println!(
info!(
"[RETRY_AND_CLEANUP] Removing row {} containing URL: {} after {} seconds. Previous status: {} and status message: {}",
row.id,
row.url.unwrap_or("<unknown>".to_string()),
row.url.unwrap_or("<null>".to_string()),
SETTINGS.retry_task.allow_remove_row_after,
row.status,
row.status_message.unwrap_or("<unknown>".to_string())
row.status_message.unwrap_or("<null>".to_string())
)
} else if row.status.try_into() != Ok(ArchivalStatus::Success) {
sqlx::query("SELECT external_url_archiver.notify_archive_urls($1)")
.bind(row.id)
.execute(pool)
.await?;
debug_println!(
info!(
"[RETRY_AND_CLEANUP] Retrying row {} containing URL: {} after {} seconds. Previous status: {} and status message: {}",
row.id,
row.url.unwrap_or("<unknown>".to_string()),
row.url.unwrap_or("<null>".to_string()),
SETTINGS.retry_task.allow_remove_row_after,
row.status,
row.status_message.unwrap_or("<unknown>".to_string())
row.status_message.unwrap_or("<null>".to_string())
)
}
}
Expand Down
Loading

0 comments on commit 85b1421

Please sign in to comment.