private async Task ProcessRuleAsync(MonitoringRule rule) { try { var lastNotificationDate = await _notificationsCache.GetLastNotificationDateAsync(rule.Id); if (rule.NeedsNotification(lastNotificationDate)) { var action = new SendNotificationMonitoringAction(); rule.ActionsByTypeName[action.TypeName].CopyTo(action); if (string.IsNullOrWhiteSpace(action.NotificationChannelId)) { await _notificationSender.SendAsync(rule.GetNotificationText()); } else { await _notificationSender.SendAsync(action.NotificationChannelId, rule.GetNotificationText()); } await _notificationsCache.AddOrUpdateAsync(rule.Id, DateTime.UtcNow.Add(MonitoringRuleExtensions.NotificationRemindPeriod)); } } catch (Exception ex) { _logger.LogError(ex, "Failed to ProcessRule. {@Rule}", rule.Name); } }
public async Task <SendNotificationResponse> SendAsync(SendNotificationRequest request) { try { await _notificationSender.SendAsync(request.ChannelId, request.Text); return(new SendNotificationResponse()); } catch (Exception ex) { _logger.LogError(ex, "Failed to SendMessage {@request}", request); return(new SendNotificationResponse { IsError = true, ErrorMessage = ex.Message }); } }
private ValueTask Handle(NewAlertMessage message) { _ = Task.Run(async() => { try { var telegramChannels = message.AlertChannels? .Where(c => c.Type == AlertChannelType.Telegram) .ToArray() ?? Array.Empty <AlertChannel>(); if (telegramChannels.Any()) { var text = $"{message.Alert.EventType.Humanize()}{Environment.NewLine}{message.Alert.Message}"; foreach (var channel in telegramChannels) { if (channel.Params.TryGetValue(AlertChannelParam.TelegramChatId, out var chatId)) { await _notificationSender.SendToChat(chatId, text); } } } else if (message.Alert.Destinations.HasFlag(AlertDestinations .Telegram)) // todo: remove after migration to channels { var text = $"{message.Alert.EventType.Humanize()}{Environment.NewLine}{message.Alert.Message}"; await _notificationSender.SendAsync(text); } } catch (Exception e) { _logger.LogError(e, "Failed to handle {@Message}. {@ExMessage}", nameof(NewAlertMessage), e.Message); } }); return(ValueTask.CompletedTask); }
public async Task <IActionResult> Process() { using (logger.BeginScope("Processing Pending Notification")) { try { logger.LogTrace("Process and send notifications ..."); await notificationSender.SendAsync( formatter, submitter, async() => await context.SaveChangesAsync()); logger.LogInformation("Pending notifications processed successfully."); return(StatusCode(200)); } catch (Exception ex) { logger.LogError(ex, "Error while processing notifications!"); return(BadRequest()); } } }
public async Task Handle(CourseCreated notification, CancellationToken cancellationToken) { await _notification.SendAsync("Course has been Created", notification.CourseId.ToString(), "Course"); }
public async Task Handle(GetCarsMessage eventMessage) { var result = await SendHttpRequestAsync(eventMessage); await _notificationSender.SendAsync(result); }