private async Task ProcessTaskItemAsync(BackgroundTask task) { using var scope = Provider.CreateScope(); var service = (IBackgroundTaskObserver)scope.ServiceProvider.GetService(task.TaskType); if (service == null) { return; } try { var logMessage = new LogMessage(LogSeverity.Debug, nameof(BackgroundTaskService), $"Executing background task {task.TaskType.FullName}"); await BotLoggingService.OnLogAsync(logMessage); await service.TriggerBackgroundTaskAsync(task); } catch (Exception ex) { var logMessage = new LogMessage(LogSeverity.Error, nameof(BackgroundTaskService), $"Executing background task {task.TaskType.FullName} failed.", ex); await BotLoggingService.OnLogAsync(logMessage); } finally { var logMessage = new LogMessage(LogSeverity.Info, nameof(BackgroundTaskService), $"Background task executing finished ({task.TaskType.FullName})"); await BotLoggingService.OnLogAsync(logMessage); } }
public BackgroundTaskService(BackgroundTaskQueue queue, IServiceProvider provider, BotLoggingService botLoggingService) { Queue = queue; BotLoggingService = botLoggingService; Provider = provider; }