-
Notifications
You must be signed in to change notification settings - Fork 1
/
EntityMonitorServiceBase.cs
70 lines (61 loc) · 2.26 KB
/
EntityMonitorServiceBase.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
60
61
62
63
64
65
66
67
68
69
70
using System.Text.Json;
using Akka.Actor;
using Common.Messaging.Redis;
using Tracing.Worker.Services;
using StackExchange.Redis;
namespace Tracing.Worker.BackgroundServices;
public abstract class EntityMonitorServiceBase<TSupervisor, TMonitor, TStartedMessage> : BackgroundService
where TSupervisor : ActorBase
{
protected abstract string ActorCollectionName { get; }
protected abstract string MessageType { get; }
private ActorSystemService _actorSystem;
private IActorRef _supervisor;
private readonly ConnectionMultiplexer _redis;
private ISubscriber _subscriber;
private readonly IConfiguration _config;
private readonly ILogger _logger;
public EntityMonitorServiceBase(RedisClient redisClient, ActorSystemService actorSystem, IConfiguration config, ILogger logger)
{
_actorSystem = actorSystem;
_redis = redisClient.ConnectionMultiplexer;
_config = config;
_logger = logger;
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
var name = GetName();
var enabled = _config.GetValue<bool>($"TracingSample:{name}:Enabled");
if (enabled)
{
_supervisor = _actorSystem.ActorOf(Props.Create<TSupervisor>(), ActorCollectionName);
_subscriber = _redis.GetSubscriber();
_subscriber.Subscribe(MessageType, (channel, value) =>
{
_logger.LogDebug($"Received message on channel: {channel}");
_logger.LogTrace(value);
var message = JsonSerializer.Deserialize<TStartedMessage>(value);
_supervisor.Tell(message);
});
_logger.LogInformation($"Listening on channel: {MessageType}");
}
else
{
_logger.LogInformation($"Monitor: {name} not enabled in config; will not collect metrics");
}
return Task.CompletedTask;
}
public override Task StopAsync(CancellationToken cancellationToken)
{
if (_subscriber != null)
{
_logger.LogInformation("Unsubscribing from Redis");
_subscriber.Unsubscribe(MessageType);
}
return Task.CompletedTask;
}
private static string GetName()
{
return typeof(TMonitor).Name;
}
}