Skip to content

Commit

Permalink
Stop logging failures leading to a recursive loop
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Nov 7, 2024
1 parent 1da0545 commit b80f83d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions tts_tasks/src/analytics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ impl crate::Looper for Arc<analytics::Handler> {
const NAME: &'static str = "Analytics";
const MILLIS: u64 = 5000;

type Error = anyhow::Error;
async fn loop_func(&self) -> anyhow::Result<()> {
let log_buffer = self.log_buffer.clone();
self.log_buffer.clear();
Expand Down
1 change: 1 addition & 0 deletions tts_tasks/src/bot_list_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl crate::Looper for BotListUpdater {
const NAME: &'static str = "Bot List Updater";
const MILLIS: u64 = 1000 * 60 * 60;

type Error = anyhow::Error;
async fn loop_func(&self) -> Result<()> {
let perform = |BotListReq { url, body, token }| async move {
let headers = reqwest::header::HeaderMap::from_iter([
Expand Down
5 changes: 4 additions & 1 deletion tts_tasks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(async_fn_in_trait)]
#![feature(never_type)]

mod analytics;
pub mod bot_list_updater;
Expand All @@ -9,7 +10,9 @@ pub trait Looper {
const NAME: &'static str;
const MILLIS: u64;

async fn loop_func(&self) -> anyhow::Result<()>;
type Error: std::fmt::Debug;

async fn loop_func(&self) -> Result<(), Self::Error>;
async fn start(self)
where
Self: Sized,
Expand Down
12 changes: 8 additions & 4 deletions tts_tasks/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ impl Looper for Arc<WebhookLogger> {
const NAME: &'static str = "Logging";
const MILLIS: u64 = 1100;

async fn loop_func(&self) -> Result<()> {
type Error = !;
async fn loop_func(&self) -> Result<(), Self::Error> {
let pending_logs = self.pending_logs.lock().drain().collect::<HashMap<_, _>>();

for (severity, messages) in pending_logs {
Expand All @@ -59,7 +60,8 @@ impl Looper for Arc<WebhookLogger> {

for (target, log_message) in messages {
for line in log_message.lines() {
writeln!(pre_chunked, "`[{target}]`: {line}")?;
writeln!(pre_chunked, "`[{target}]`: {line}")
.expect("String::write_fmt should never not fail");
}
}

Expand Down Expand Up @@ -92,11 +94,13 @@ impl Looper for Arc<WebhookLogger> {

for chunk in chunks {
let builder = ExecuteWebhook::default()
.content(chunk)
.content(&chunk)
.username(webhook_name.as_str())
.avatar_url(get_avatar(severity));

webhook.execute(&self.http, false, builder).await?;
if let Err(err) = webhook.execute(&self.http, false, builder).await {
eprintln!("Failed to send log message: {err:?}\n{chunk}");
}
}
}

Expand Down
1 change: 1 addition & 0 deletions tts_tasks/src/web_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl crate::Looper for Updater {
const NAME: &'static str = "WebUpdater";
const MILLIS: u64 = 1000 * 60 * 60;

type Error = anyhow::Error;
async fn loop_func(&self) -> Result<()> {
#[derive(sqlx::FromRow)]
struct AnalyticsQueryResult {
Expand Down

0 comments on commit b80f83d

Please sign in to comment.