@@ -20,15 +20,19 @@ public TaskResumeService(IServiceProvider serviceProvider, ILogger<TaskResumeSer
2020
2121 public async Task StartAsync ( CancellationToken cancellationToken )
2222 {
23- _logger . LogInformation ( "TaskResumeService starting - checking for pending tasks" ) ;
23+ _logger . LogInformation ( "========== TaskResumeService STARTING ==========" ) ;
24+ _logger . LogInformation ( "AutoResumeOnStartup config value: {Value}" , GeneralConfigStatic . config ? . AutoResumeOnStartup ) ;
25+ _logger . LogInformation ( "EnableTaskPersistence config value: {Value}" , GeneralConfigStatic . config ? . EnableTaskPersistence ) ;
2426
2527 // Check if auto-resume is enabled
2628 if ( ! ( GeneralConfigStatic . config ? . AutoResumeOnStartup ?? true ) )
2729 {
28- _logger . LogInformation ( "Auto-resume on startup is disabled " ) ;
30+ _logger . LogWarning ( "Auto-resume on startup is DISABLED in config - skipping task resume " ) ;
2931 return ;
3032 }
3133
34+ _logger . LogInformation ( "Auto-resume is ENABLED - will resume tasks in background" ) ;
35+
3236 // Run in background to not block application startup
3337 _ = Task . Run ( async ( ) =>
3438 {
@@ -38,61 +42,78 @@ public async Task StartAsync(CancellationToken cancellationToken)
3842 }
3943 catch ( Exception ex )
4044 {
41- _logger . LogError ( ex , "Error during task resume" ) ;
45+ _logger . LogError ( ex , "CRITICAL ERROR during task resume: {Message}" , ex . Message ) ;
4246 }
4347 } , cancellationToken ) ;
4448 }
4549
4650 private async Task ResumeTasksAsync ( CancellationToken cancellationToken )
4751 {
52+ _logger . LogInformation ( "ResumeTasksAsync - Starting, waiting 5 seconds for services..." ) ;
53+
4854 // Wait a bit for services to be fully initialized
4955 await Task . Delay ( 5000 , cancellationToken ) ;
5056
57+ _logger . LogInformation ( "ResumeTasksAsync - Creating service scope..." ) ;
58+
5159 using var scope = _serviceProvider . CreateScope ( ) ;
5260 var telegramService = scope . ServiceProvider . GetRequiredService < ITelegramService > ( ) ;
5361 var persistence = scope . ServiceProvider . GetRequiredService < ITaskPersistenceService > ( ) ;
5462 var fileService = scope . ServiceProvider . GetRequiredService < IFileService > ( ) ;
5563 var transactionInfo = scope . ServiceProvider . GetRequiredService < TransactionInfoService > ( ) ;
5664
65+ _logger . LogInformation ( "ResumeTasksAsync - Services resolved. Persistence enabled: {Enabled}, FileService type: {Type}" ,
66+ persistence . IsEnabled , fileService . GetType ( ) . Name ) ;
67+
5768 // Wait for Telegram session to be ready
5869 var maxWaitTime = TimeSpan . FromSeconds ( 60 ) ;
5970 var waited = TimeSpan . Zero ;
6071 var checkInterval = TimeSpan . FromSeconds ( 3 ) ;
6172
62- _logger . LogInformation ( "Waiting for Telegram session to be ready..." ) ;
73+ _logger . LogInformation ( "Waiting for Telegram session to be ready (max {MaxWait}s) ..." , maxWaitTime . TotalSeconds ) ;
6374
6475 while ( ! telegramService . checkUserLogin ( ) && waited < maxWaitTime )
6576 {
6677 if ( cancellationToken . IsCancellationRequested )
78+ {
79+ _logger . LogWarning ( "Cancellation requested while waiting for Telegram session" ) ;
6780 return ;
81+ }
6882
69- _logger . LogDebug ( "Waiting for Telegram session... {Waited}s" , waited . TotalSeconds ) ;
83+ _logger . LogInformation ( "Waiting for Telegram session... {Waited}s elapsed " , waited . TotalSeconds ) ;
7084 await Task . Delay ( checkInterval , cancellationToken ) ;
7185 waited += checkInterval ;
7286 }
7387
7488 if ( ! telegramService . checkUserLogin ( ) )
7589 {
76- _logger . LogWarning ( "Telegram session not ready after {MaxWait}s - tasks will not be resumed automatically. " +
90+ _logger . LogWarning ( "Telegram session NOT ready after {MaxWait}s - tasks will NOT be resumed automatically. " +
7791 "They will resume when user logs in." , maxWaitTime . TotalSeconds ) ;
7892 return ;
7993 }
8094
81- _logger . LogInformation ( "Telegram session ready - loading pending tasks" ) ;
95+ _logger . LogInformation ( "========== Telegram session READY - Loading pending tasks from MongoDB ========== " ) ;
8296
8397 // Cleanup stale tasks first
8498 await persistence . CleanupStaleTasks ( ) ;
8599
86100 // Load pending tasks
101+ _logger . LogInformation ( "Calling LoadPendingTasks..." ) ;
87102 var pendingTasks = await persistence . LoadPendingTasks ( ) ;
103+ _logger . LogInformation ( "LoadPendingTasks returned {Count} tasks" , pendingTasks ? . Count ?? 0 ) ;
88104
89- if ( pendingTasks . Count == 0 )
105+ if ( pendingTasks == null || pendingTasks . Count == 0 )
90106 {
91- _logger . LogInformation ( "No pending tasks to resume" ) ;
107+ _logger . LogInformation ( "========== No pending tasks found in MongoDB - nothing to resume ========== " ) ;
92108 return ;
93109 }
94110
95- _logger . LogInformation ( "Found {Count} pending tasks to resume" , pendingTasks . Count ) ;
111+ _logger . LogInformation ( "========== Found {Count} pending tasks to resume ==========" , pendingTasks . Count ) ;
112+ foreach ( var t in pendingTasks )
113+ {
114+ _logger . LogInformation ( " - Task: {Name}, Type: {Type}, State: {State}, Channel: {Channel}, Progress: {Progress}%" ,
115+ t . Name , t . Type , t . State , t . ChannelId , t . Progress ) ;
116+ }
96117
97118 // Group tasks by type for better processing
98119 var downloads = pendingTasks . Where ( t => t . Type == TaskType . Download ) . ToList ( ) ;
@@ -223,10 +244,13 @@ private async Task ResumeDownload(
223244 // Note: FileServiceV2.downloadFromTelegramV2 will need modification to accept resumeOffset
224245 if ( fileService is FileServiceV2 fsv2 )
225246 {
247+ _logger . LogInformation ( "FileService is FileServiceV2 - setting up download callback" ) ;
248+
226249 // Add to pending list with the existing internal ID
227250 downloadModel . callbacks = new Callbacks ( ) ;
228251 downloadModel . callbacks . callback = async ( ) =>
229252 {
253+ _logger . LogInformation ( "Download callback executing for: {Name}" , task . Name ) ;
230254 await fsv2 . DownloadFileNowV2WithOffset (
231255 task . ChannelId ,
232256 task . MessageId ?? ( task . MessageIds ? . FirstOrDefault ( ) ?? 0 ) ,
@@ -235,11 +259,17 @@ await fsv2.DownloadFileNowV2WithOffset(
235259 resumeOffset ) ;
236260 } ;
237261
262+ _logger . LogInformation ( "Adding download to pending list: {Name}, InternalId: {Id}" ,
263+ downloadModel . name , downloadModel . _internalId ) ;
264+
238265 transactionInfo . addToPendingDownloadList ( downloadModel , atFirst : false , chekDownloads : true ) ;
266+
267+ _logger . LogInformation ( "Download added to pending list successfully: {Name}" , task . Name ) ;
239268 }
240269 else
241270 {
242- _logger . LogWarning ( "FileService is not FileServiceV2 - cannot resume with offset" ) ;
271+ _logger . LogWarning ( "FileService is NOT FileServiceV2 (actual type: {Type}) - cannot resume with offset" ,
272+ fileService . GetType ( ) . Name ) ;
243273 // Fallback: restart download from beginning
244274 // This would require implementing resume in FileService as well
245275 }
0 commit comments