From 66f7c1271352b3cf6857c98470f1537b7f32958d Mon Sep 17 00:00:00 2001 From: boxbeam Date: Mon, 29 Apr 2024 14:11:51 -0400 Subject: [PATCH] refactor(webserver): make macro for writing to stderr and warn! at the same time (#2007) --- ee/tabby-webserver/src/cron/db/github.rs | 16 +++++++--------- ee/tabby-webserver/src/cron/db/gitlab.rs | 16 +++++++--------- ee/tabby-webserver/src/cron/mod.rs | 8 ++++++++ 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/ee/tabby-webserver/src/cron/db/github.rs b/ee/tabby-webserver/src/cron/db/github.rs index e2201fc539ec..767ca01db94d 100644 --- a/ee/tabby-webserver/src/cron/db/github.rs +++ b/ee/tabby-webserver/src/cron/db/github.rs @@ -8,6 +8,7 @@ use octocrab::{models::Repository, GitHubError, Octocrab}; use crate::{ cron::controller::JobContext, schema::repository::{GithubRepositoryProvider, GithubRepositoryService}, + warn_stderr, }; pub async fn refresh_all_repositories( @@ -49,18 +50,15 @@ async fn refresh_repositories_for_provider( service .update_provider_status(provider.id.clone(), false) .await?; - context - .stderr_writeline(format!( - "GitHub credentials for provider {} are expired or invalid", - provider.display_name - )) - .await; + warn_stderr!( + context, + "GitHub credentials for provider {} are expired or invalid", + provider.display_name + ); return Err(source.into()); } Err(e) => { - context - .stderr_writeline(format!("Failed to fetch repositories from github: {}", e)) - .await; + warn_stderr!(context, "Failed to fetch repositories from github: {e}"); return Err(e.into()); } }; diff --git a/ee/tabby-webserver/src/cron/db/gitlab.rs b/ee/tabby-webserver/src/cron/db/gitlab.rs index edbf6c81d0f9..f8b66abdaa89 100644 --- a/ee/tabby-webserver/src/cron/db/gitlab.rs +++ b/ee/tabby-webserver/src/cron/db/gitlab.rs @@ -12,6 +12,7 @@ use serde::Deserialize; use crate::{ cron::controller::JobContext, schema::repository::{GitlabRepositoryProvider, GitlabRepositoryService}, + warn_stderr, }; pub async fn refresh_all_repositories( @@ -50,18 +51,15 @@ async fn refresh_repositories_for_provider( service .update_provider_status(provider.id.clone(), false) .await?; - context - .stderr_writeline(format!( - "GitLab credentials for provider {} are expired or invalid", - provider.display_name - )) - .await; + warn_stderr!( + context, + "GitLab credentials for provider {} are expired or invalid", + provider.display_name + ); return Err(e.into()); } Err(e) => { - context - .stderr_writeline(format!("Failed to fetch repositories from gitlab: {e}")) - .await; + warn_stderr!(context, "Failed to fetch repositories from gitlab: {e}"); return Err(e.into()); } }; diff --git a/ee/tabby-webserver/src/cron/mod.rs b/ee/tabby-webserver/src/cron/mod.rs index 2e28b3c469d0..d3b8165ebe23 100644 --- a/ee/tabby-webserver/src/cron/mod.rs +++ b/ee/tabby-webserver/src/cron/mod.rs @@ -11,6 +11,14 @@ use crate::schema::{ worker::WorkerService, }; +#[macro_export] +macro_rules! warn_stderr { + ($ctx:expr, $($params:tt)+) => { + tracing::warn!($($params)+); + $ctx.stderr_writeline(format!($($params)+)).await; + } +} + pub async fn run_cron( auth: Arc, job: Arc,