@@ -6,7 +6,7 @@ use mcpmux_core::branding;
66use std:: sync:: Arc ;
77use tauri:: { Emitter , Manager } ;
88use tokio:: sync:: RwLock ;
9- use tracing:: { error, info, warn} ;
9+ use tracing:: { debug , error, info, warn} ;
1010
1111mod commands;
1212mod services;
@@ -519,6 +519,53 @@ pub fn run() {
519519 } ) ;
520520 }
521521
522+ // Start periodic log cleanup task
523+ {
524+ let log_manager = app_state. server_log_manager . clone ( ) ;
525+ let settings_repo_for_cleanup = app_state. settings_repository . clone ( ) ;
526+
527+ tauri:: async_runtime:: spawn ( async move {
528+ use mcpmux_core:: AppSettingsService ;
529+
530+ let settings = AppSettingsService :: new ( settings_repo_for_cleanup) ;
531+
532+ // Run cleanup once at startup
533+ let retention_days = settings. get_log_retention_days ( ) . await ;
534+ if retention_days > 0 {
535+ info ! (
536+ "[LogCleanup] Running startup cleanup (retention: {} days)" ,
537+ retention_days
538+ ) ;
539+ match log_manager. cleanup_logs_older_than ( retention_days) . await {
540+ Ok ( n) if n > 0 => {
541+ info ! ( "[LogCleanup] Startup cleanup removed {} file(s)" , n)
542+ }
543+ Ok ( _) => debug ! ( "[LogCleanup] No old log files to clean up" ) ,
544+ Err ( e) => warn ! ( "[LogCleanup] Startup cleanup failed: {}" , e) ,
545+ }
546+ }
547+
548+ // Then run every 24 hours
549+ let mut interval =
550+ tokio:: time:: interval ( std:: time:: Duration :: from_secs ( 24 * 60 * 60 ) ) ;
551+ interval. tick ( ) . await ; // skip the first immediate tick (already ran above)
552+
553+ loop {
554+ interval. tick ( ) . await ;
555+ let days = settings. get_log_retention_days ( ) . await ;
556+ if days > 0 {
557+ match log_manager. cleanup_logs_older_than ( days) . await {
558+ Ok ( n) if n > 0 => {
559+ info ! ( "[LogCleanup] Periodic cleanup removed {} file(s)" , n)
560+ }
561+ Ok ( _) => { }
562+ Err ( e) => warn ! ( "[LogCleanup] Periodic cleanup failed: {}" , e) ,
563+ }
564+ }
565+ }
566+ } ) ;
567+ }
568+
522569 // Setup system tray
523570 tray:: setup_tray ( app. handle ( ) ) ?;
524571
@@ -749,6 +796,8 @@ pub fn run() {
749796 commands:: get_server_logs,
750797 commands:: clear_server_logs,
751798 commands:: get_server_log_file,
799+ commands:: get_log_retention_days,
800+ commands:: set_log_retention_days,
752801 // App log commands
753802 get_logs_path,
754803 open_logs_folder,
0 commit comments