/
SmsStatusConsumer.cs
59 lines (51 loc) · 2.05 KB
/
SmsStatusConsumer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using Altinn.Notifications.Core.Integrations;
using Altinn.Notifications.Core.Models.Notification;
using Altinn.Notifications.Core.Services.Interfaces;
using Altinn.Notifications.Integrations.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Altinn.Notifications.Integrations.Kafka.Consumers;
/// <summary>
/// Kafka consumer class for status messages about sms notifications
/// </summary>
public class SmsStatusConsumer : KafkaConsumerBase<SmsStatusConsumer>
{
private readonly ISmsNotificationService _smsNotificationsService;
private readonly IKafkaProducer _producer;
private readonly string _retryTopicName;
private readonly ILogger<SmsStatusConsumer> _logger;
/// <summary>
/// Initializes a new instance of the <see cref="SmsStatusConsumer"/> class.
/// </summary>
public SmsStatusConsumer(
ISmsNotificationService smsNotificationsService,
IKafkaProducer producer,
IOptions<KafkaSettings> settings,
ILogger<SmsStatusConsumer> logger)
: base(settings, logger, settings.Value.SmsStatusUpdatedTopicName)
{
_smsNotificationsService = smsNotificationsService;
_producer = producer;
_retryTopicName = settings.Value.SmsStatusUpdatedTopicName;
_logger = logger;
}
/// <inheritdoc/>
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
return Task.Run(() => ConsumeMessage(ProcessStatus, RetryStatus, stoppingToken), stoppingToken);
}
private async Task ProcessStatus(string message)
{
bool succeeded = SmsSendOperationResult.TryParse(message, out SmsSendOperationResult result);
if (!succeeded)
{
_logger.LogError("// SmsStatusConsumer // ProcessStatus // Deserialization of message failed. {Message}", message);
return;
}
await _smsNotificationsService.UpdateSendStatus(result);
}
private async Task RetryStatus(string message)
{
await _producer.ProduceAsync(_retryTopicName, message!);
}
}