Replace random print statements with proper logging setup

This commit is contained in:
Aleksei Voronov 2023-09-21 11:22:18 +02:00
parent f4ee482ce7
commit 62b00ceed7
7 changed files with 32 additions and 5 deletions

2
Cargo.lock generated
View File

@ -2249,9 +2249,11 @@ dependencies = [
"chrono", "chrono",
"ciborium", "ciborium",
"dotenv", "dotenv",
"env_logger",
"futures", "futures",
"libipld-core", "libipld-core",
"lingua", "lingua",
"log",
"once_cell", "once_cell",
"rs-car", "rs-car",
"scooby", "scooby",

View File

@ -15,9 +15,11 @@ chat-gpt-lib-rs = "0.2.1"
chrono = "0.4.31" chrono = "0.4.31"
ciborium = "0.2.1" ciborium = "0.2.1"
dotenv = "0.15.0" dotenv = "0.15.0"
env_logger = "0.10.0"
futures = "0.3.28" futures = "0.3.28"
libipld-core = { version = "0.16.0", features = ["serde-codec"] } libipld-core = { version = "0.16.0", features = ["serde-codec"] }
lingua = "1.5.0" lingua = "1.5.0"
log = "0.4.20"
once_cell = "1.18.0" once_cell = "1.18.0"
rs-car = "0.4.1" rs-car = "0.4.1"
scooby = "0.5.0" scooby = "0.5.0"

View File

@ -7,6 +7,8 @@ use std::sync::Arc;
use anyhow::Result; use anyhow::Result;
use lingua::LanguageDetectorBuilder; use lingua::LanguageDetectorBuilder;
use log::info;
use env_logger::Env;
use crate::algos::AlgosBuilder; use crate::algos::AlgosBuilder;
use crate::algos::Nederlandskie; use crate::algos::Nederlandskie;
@ -20,11 +22,20 @@ use crate::services::AI;
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
info!("Loading configuration");
let config = Arc::new(Config::load()?); let config = Arc::new(Config::load()?);
info!("Initializing service clients");
let ai = Arc::new(AI::new(&config.chat_gpt_api_key, "https://api.openai.com")); let ai = Arc::new(AI::new(&config.chat_gpt_api_key, "https://api.openai.com"));
let bluesky = Arc::new(Bluesky::new("https://bsky.social")); let bluesky = Arc::new(Bluesky::new("https://bsky.social"));
let database = Arc::new(Database::connect(&config.database_url).await?); let database = Arc::new(Database::connect(&config.database_url).await?);
info!("Initializing language detector");
let language_detector = Arc::new( let language_detector = Arc::new(
LanguageDetectorBuilder::from_all_languages() LanguageDetectorBuilder::from_all_languages()
.with_preloaded_language_models() .with_preloaded_language_models()
@ -41,6 +52,8 @@ async fn main() -> Result<()> {
let profile_classifier = ProfileClassifier::new(database.clone(), ai.clone(), bluesky.clone()); let profile_classifier = ProfileClassifier::new(database.clone(), ai.clone(), bluesky.clone());
let feed_server = FeedServer::new(database.clone(), config.clone(), algos.clone()); let feed_server = FeedServer::new(database.clone(), config.clone(), algos.clone());
info!("Starting everything up");
tokio::try_join!( tokio::try_join!(
post_indexer.start(), post_indexer.start(),
profile_classifier.start(), profile_classifier.start(),

View File

@ -4,6 +4,7 @@ use std::sync::Arc;
use anyhow::Result; use anyhow::Result;
use axum::routing::get; use axum::routing::get;
use axum::{Router, Server}; use axum::{Router, Server};
use log::info;
use crate::algos::Algos; use crate::algos::Algos;
use crate::config::Config; use crate::config::Config;
@ -46,6 +47,9 @@ impl FeedServer {
}); });
let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
info!("Serving feed on {}", addr);
Server::bind(&addr).serve(app.into_make_service()).await?; Server::bind(&addr).serve(app.into_make_service()).await?;
Ok(()) Ok(())
} }

View File

@ -2,6 +2,7 @@ use std::sync::Arc;
use anyhow::Result; use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use log::info;
use crate::algos::Algos; use crate::algos::Algos;
use crate::services::bluesky::{Bluesky, Operation, OperationProcessor}; use crate::services::bluesky::{Bluesky, Operation, OperationProcessor};
@ -25,6 +26,7 @@ impl PostIndexer {
impl PostIndexer { impl PostIndexer {
pub async fn start(&self) -> Result<()> { pub async fn start(&self) -> Result<()> {
info!("Starting");
Ok(self.bluesky.subscribe_to_operations(self).await?) Ok(self.bluesky.subscribe_to_operations(self).await?)
} }
} }
@ -45,7 +47,7 @@ impl OperationProcessor for PostIndexer {
.iter_all() .iter_all()
.any(|a| a.should_index_post(author_did, languages, text)) .any(|a| a.should_index_post(author_did, languages, text))
{ {
println!("received insertable post from {author_did}: {text}"); info!("Received insertable post from {author_did}: {text}");
self.database self.database
.insert_profile_if_it_doesnt_exist(&author_did) .insert_profile_if_it_doesnt_exist(&author_did)
@ -54,7 +56,7 @@ impl OperationProcessor for PostIndexer {
} }
} }
Operation::DeletePost { uri } => { Operation::DeletePost { uri } => {
println!("received a post do delete: {uri}"); info!("Received a post to delete: {uri}");
// TODO: Delete posts from db // TODO: Delete posts from db
// self.database.delete_post(&self.db_connection_pool, &uri).await?; // self.database.delete_post(&self.db_connection_pool, &uri).await?;

View File

@ -2,6 +2,7 @@ use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use anyhow::Result; use anyhow::Result;
use log::info;
use crate::services::Bluesky; use crate::services::Bluesky;
use crate::services::Database; use crate::services::Database;
@ -23,6 +24,7 @@ impl ProfileClassifier {
} }
pub async fn start(&self) -> Result<()> { pub async fn start(&self) -> Result<()> {
info!("Starting");
loop { loop {
// TODO: Don't just exit this function when an error happens, just wait a minute or so? // TODO: Don't just exit this function when an error happens, just wait a minute or so?
self.classify_unclassified_profiles().await?; self.classify_unclassified_profiles().await?;
@ -34,9 +36,10 @@ impl ProfileClassifier {
let dids = self.database.fetch_unprocessed_profile_dids().await?; let dids = self.database.fetch_unprocessed_profile_dids().await?;
if dids.is_empty() { if dids.is_empty() {
println!("No profiles to process: waiting 10 seconds"); info!("No profiles to process: waiting 10 seconds");
tokio::time::sleep(Duration::from_secs(10)).await; tokio::time::sleep(Duration::from_secs(10)).await;
} else { } else {
info!("Classifying {} new profiles", dids.len());
for did in &dids { for did in &dids {
self.fill_in_profile_details(did).await?; self.fill_in_profile_details(did).await?;
} }
@ -52,7 +55,7 @@ impl ProfileClassifier {
.infer_country_of_living(&details.display_name, &details.description) .infer_country_of_living(&details.display_name, &details.description)
.await?; .await?;
self.database.store_profile_details(did, &country).await?; self.database.store_profile_details(did, &country).await?;
println!("Stored inferred country of living for {did}: {country}"); info!("Stored inferred country of living for {did}: {country}");
Ok(()) Ok(())
} }
} }

View File

@ -4,6 +4,7 @@ use atrium_api::client::AtpServiceWrapper;
use atrium_xrpc::client::reqwest::ReqwestClient; use atrium_xrpc::client::reqwest::ReqwestClient;
use futures::StreamExt; use futures::StreamExt;
use tokio_tungstenite::{connect_async, tungstenite}; use tokio_tungstenite::{connect_async, tungstenite};
use log::error;
use super::streaming::{handle_message, OperationProcessor}; use super::streaming::{handle_message, OperationProcessor};
@ -59,7 +60,7 @@ impl Bluesky {
while let Some(Ok(tungstenite::Message::Binary(message))) = stream.next().await { while let Some(Ok(tungstenite::Message::Binary(message))) = stream.next().await {
if let Err(e) = handle_message(&message, processor).await { if let Err(e) = handle_message(&message, processor).await {
println!("Error handling a message: {:?}", e); error!("Error handling a message: {:?}", e);
} }
} }