-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPostgreSqlProducerService.cs
183 lines (162 loc) · 7.83 KB
/
PostgreSqlProducerService.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System.Diagnostics;
using System.Text;
using System.Text.Json;
using AsyncMonolith.Consumers;
using AsyncMonolith.Producers;
using AsyncMonolith.Scheduling;
using AsyncMonolith.Utilities;
using Microsoft.EntityFrameworkCore;
using Npgsql;
namespace AsyncMonolith.PostgreSql;
/// <summary>
/// Represents a service for producing messages to a PostgreSQL database.
/// </summary>
/// <typeparam name="T">The type of the DbContext.</typeparam>
public sealed class PostgreSqlProducerService<T> : IProducerService where T : DbContext
{
private readonly ConsumerRegistry _consumerRegistry;
private readonly T _dbContext;
private readonly IAsyncMonolithIdGenerator _idGenerator;
private readonly TimeProvider _timeProvider;
/// <summary>
/// Initializes a new instance of the <see cref="PostgreSqlProducerService{T}"/> class.
/// </summary>
/// <param name="timeProvider">The time provider.</param>
/// <param name="consumerRegistry">The consumer registry.</param>
/// <param name="dbContext">The DbContext.</param>
/// <param name="idGenerator">The ID generator.</param>
public PostgreSqlProducerService(TimeProvider timeProvider, ConsumerRegistry consumerRegistry, T dbContext,
IAsyncMonolithIdGenerator idGenerator)
{
_timeProvider = timeProvider;
_consumerRegistry = consumerRegistry;
_dbContext = dbContext;
_idGenerator = idGenerator;
}
/// <summary>
/// Produces a single message to the database.
/// </summary>
/// <typeparam name="TK">The type of the message.</typeparam>
/// <param name="message">The message to produce.</param>
/// <param name="availableAfter">The time when the message should be available for consumption.</param>
/// <param name="insertId">The insert ID for the message.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public async Task Produce<TK>(TK message, long? availableAfter = null, string? insertId = null,
CancellationToken cancellationToken = default)
where TK : IConsumerPayload
{
var currentTime = _timeProvider.GetUtcNow().ToUnixTimeSeconds();
availableAfter ??= currentTime;
var payload = JsonSerializer.Serialize(message);
var traceId = Activity.Current?.TraceId.ToString();
var spanId = Activity.Current?.SpanId.ToString();
var payloadType = typeof(TK).Name;
insertId ??= _idGenerator.GenerateId();
var sqlBuilder = new StringBuilder();
var parameters = new List<NpgsqlParameter>
{
new("@created_at", currentTime),
new("@available_after", availableAfter),
new("@payload_type", payloadType),
new("@payload", payload),
new("@insert_id", insertId),
new("@trace_id", string.IsNullOrEmpty(traceId) ? DBNull.Value : traceId),
new("@span_id", string.IsNullOrEmpty(spanId) ? DBNull.Value : spanId)
};
var consumerTypes = _consumerRegistry.ResolvePayloadConsumerTypes(payloadType);
for (var index = 0; index < consumerTypes.Count; index++)
{
if (sqlBuilder.Length > 0)
{
sqlBuilder.Append(", ");
}
sqlBuilder.Append(
$@"(@id_{index}, @created_at, @available_after, 0, @consumer_type_{index}, @payload_type, @payload, @insert_id, @trace_id, @span_id)");
parameters.Add(new NpgsqlParameter($"@id_{index}", _idGenerator.GenerateId()));
parameters.Add(new NpgsqlParameter($"@consumer_type_{index}", consumerTypes[index]));
}
var sql = $@"
INSERT INTO consumer_messages (id, created_at, available_after, attempts, consumer_type, payload_type, payload, insert_id, trace_id, span_id)
VALUES {sqlBuilder}
ON CONFLICT (insert_id, consumer_type) DO NOTHING;";
await _dbContext.Database.ExecuteSqlRawAsync(sql, parameters, cancellationToken);
}
/// <summary>
/// Produces a list of messages to the database.
/// </summary>
/// <typeparam name="TK">The type of the messages.</typeparam>
/// <param name="messages">The messages to produce.</param>
/// <param name="availableAfter">The time when the messages should be available for consumption.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public async Task ProduceList<TK>(List<TK> messages, long? availableAfter = null,
CancellationToken cancellationToken = default) where TK : IConsumerPayload
{
var currentTime = _timeProvider.GetUtcNow().ToUnixTimeSeconds();
availableAfter ??= currentTime;
var traceId = Activity.Current?.TraceId.ToString();
var spanId = Activity.Current?.SpanId.ToString();
var sqlBuilder = new StringBuilder();
var parameters = new List<NpgsqlParameter>
{
new("@created_at", currentTime),
new("@available_after", availableAfter),
new("@trace_id", string.IsNullOrEmpty(traceId) ? DBNull.Value : traceId),
new("@span_id", string.IsNullOrEmpty(spanId) ? DBNull.Value : spanId)
};
var payloadType = typeof(TK).Name;
var consumerTypes = _consumerRegistry.ResolvePayloadConsumerTypes(payloadType);
for (var i = 0; i < messages.Count; i++)
{
var message = messages[i];
var insertId = _idGenerator.GenerateId();
var payload = JsonSerializer.Serialize(message);
parameters.Add(new NpgsqlParameter($"@insert_id_{i}", insertId));
parameters.Add(new NpgsqlParameter($"@payload_type_{i}", payloadType));
parameters.Add(new NpgsqlParameter($"@payload_{i}", payload));
for (var index = 0; index < consumerTypes.Count; index++)
{
if (sqlBuilder.Length > 0)
{
sqlBuilder.Append(", ");
}
sqlBuilder.Append(
$@"(@id_{i}_{index}, @created_at, @available_after, 0, @consumer_type_{i}_{index}, @payload_type_{i}, @payload_{i}, @insert_id_{i}, @trace_id, @span_id)");
parameters.Add(new NpgsqlParameter($"@id_{i}_{index}", _idGenerator.GenerateId()));
parameters.Add(new NpgsqlParameter($"@consumer_type_{i}_{index}", consumerTypes[index]));
}
}
var sql = $@"
INSERT INTO consumer_messages (id, created_at, available_after, attempts, consumer_type, payload_type, payload, insert_id, trace_id, span_id)
VALUES {sqlBuilder}
ON CONFLICT (insert_id, consumer_type) DO NOTHING;";
await _dbContext.Database.ExecuteSqlRawAsync(sql, parameters, cancellationToken);
}
/// <summary>
/// Produces a scheduled message to the database.
/// </summary>
/// <param name="message">The scheduled message to produce.</param>
public void Produce(ScheduledMessage message)
{
var currentTime = _timeProvider.GetUtcNow().ToUnixTimeSeconds();
var set = _dbContext.Set<ConsumerMessage>();
var insertId = _idGenerator.GenerateId();
foreach (var consumerId in _consumerRegistry.ResolvePayloadConsumerTypes(message.PayloadType))
{
set.Add(new ConsumerMessage
{
Id = _idGenerator.GenerateId(),
CreatedAt = currentTime,
AvailableAfter = currentTime,
ConsumerType = consumerId,
PayloadType = message.PayloadType,
Payload = message.Payload,
Attempts = 0,
InsertId = insertId,
TraceId = null,
SpanId = null
});
}
}
}