-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminAnnouncementProjectionProcessManager.cs
More file actions
235 lines (206 loc) · 8.23 KB
/
Copy pathAdminAnnouncementProjectionProcessManager.cs
File metadata and controls
235 lines (206 loc) · 8.23 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using Admin.Application.Authorization;
using Admin.Application.Consumers;
using BuildingBlocks.Application.Dispatching;
using BuildingBlocks.Application.Modules;
using BuildingBlocks.Application.ProcessManagers;
using BuildingBlocks.Application.Results;
using Admin.Application.Realtime;
using Admin.PublicContracts.Queries;
using NodaTime;
using DomainClock = BuildingBlocks.Domain.Time.IClock;
namespace Admin.Application.ProcessManagers;
public sealed record AdminAnnouncementProjectionProcessState(
Guid AnnouncementId,
string Title,
string Body,
DateTimeOffset PublishedUtc,
string PublishedByActorId,
string SourceModuleKey,
string? SourceReference,
string Step,
bool ProjectionStored,
string? FailureCode);
public interface IAdminAnnouncementProjectionProcessManagerStore
{
ValueTask<ProcessManagerCheckpoint<AdminAnnouncementProjectionProcessState>?> LoadAsync(
Guid announcementId,
CancellationToken cancellationToken);
ValueTask<IReadOnlyCollection<ProcessManagerCheckpoint<AdminAnnouncementProjectionProcessState>>> ListRecoverableAsync(
Instant updatedBefore,
int limit,
CancellationToken cancellationToken);
ValueTask<ProcessManagerCheckpoint<AdminAnnouncementProjectionProcessState>> SaveAsync(
ProcessManagerCheckpoint<AdminAnnouncementProjectionProcessState> checkpoint,
CancellationToken cancellationToken);
}
public interface IAdminAnnouncementProjectionProcessManager
{
Task HandleAsync(AdminAnnouncementProjection announcement, CancellationToken cancellationToken);
}
public sealed record RecoverAdminAnnouncementProjectionCommand(AdminAnnouncementProjection Projection)
: ICommand, IModuleScoped
{
public string ModuleKey => AdminModuleInfo.ModuleKey;
}
internal sealed class RecoverAdminAnnouncementProjectionCommandHandler : ICommandHandler<RecoverAdminAnnouncementProjectionCommand>
{
private readonly IAdminAnnouncementProjectionProcessManager _processManager;
public RecoverAdminAnnouncementProjectionCommandHandler(IAdminAnnouncementProjectionProcessManager processManager)
{
_processManager = processManager ?? throw new ArgumentNullException(nameof(processManager));
}
public async Task<Result> Handle(RecoverAdminAnnouncementProjectionCommand command, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(command);
ArgumentNullException.ThrowIfNull(command.Projection);
await _processManager.HandleAsync(command.Projection, cancellationToken);
return Result.Success();
}
}
public sealed class AdminAnnouncementProjectionProcessManager : IAdminAnnouncementProjectionProcessManager
{
public const string ProcessManagerName = "AdminAnnouncementProjectionProcessManager";
private static readonly Error ProjectionFailed = new(
"admin.announcement_projection_failed",
"Admin announcement projection process failed.",
ErrorKind.Failure);
private readonly DomainClock _clock;
private readonly IAdminAnnouncementInbox _inbox;
private readonly IAdminAnnouncementRealtimeNotifier _realtimeNotifier;
private readonly IAdminAnnouncementProjectionProcessManagerStore _store;
public AdminAnnouncementProjectionProcessManager(
DomainClock clock,
IAdminAnnouncementInbox inbox,
IAdminAnnouncementRealtimeNotifier realtimeNotifier,
IAdminAnnouncementProjectionProcessManagerStore store)
{
_clock = clock ?? throw new ArgumentNullException(nameof(clock));
_inbox = inbox ?? throw new ArgumentNullException(nameof(inbox));
_realtimeNotifier = realtimeNotifier ?? throw new ArgumentNullException(nameof(realtimeNotifier));
_store = store ?? throw new ArgumentNullException(nameof(store));
}
public async Task HandleAsync(AdminAnnouncementProjection announcement, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(announcement);
var checkpoint = await _store.LoadAsync(announcement.AnnouncementId, cancellationToken);
if (checkpoint is { LifecycleState: ProcessManagerLifecycleState.Completed })
{
return;
}
checkpoint ??= await _store.SaveAsync(
new ProcessManagerCheckpoint<AdminAnnouncementProjectionProcessState>(
AdminModuleInfo.ModuleKey,
ProcessManagerName,
CreateProcessId(announcement.AnnouncementId),
ProcessManagerLifecycleState.Running,
Version: 0,
UpdatedAt: _clock.GetCurrentInstant(),
State: CreateState(
announcement,
step: "project-announcement",
projectionStored: false,
failureCode: null)),
cancellationToken);
try
{
await _inbox.StoreAsync(CreateProjection(checkpoint.State), cancellationToken);
var completedAt = _clock.GetCurrentInstant();
var completedState = checkpoint.State with
{
Step = "projection-stored",
ProjectionStored = true,
FailureCode = null
};
await _store.SaveAsync(
checkpoint with
{
LifecycleState = ProcessManagerLifecycleState.Completed,
UpdatedAt = completedAt,
CompletedAt = completedAt,
Failure = null,
State = completedState
},
cancellationToken);
try
{
await _realtimeNotifier.PublishProjectedAsync(
AdminAnnouncementProjectedNotification.From(CreateReadModel(completedState)),
cancellationToken);
}
catch
{
}
}
catch (Exception exception)
{
var failedAt = _clock.GetCurrentInstant();
try
{
await _store.SaveAsync(
checkpoint with
{
LifecycleState = ProcessManagerLifecycleState.Failed,
UpdatedAt = failedAt,
CompletedAt = null,
Failure = ProjectionFailed with { Message = exception.Message },
State = checkpoint.State with
{
Step = "projection-failed",
FailureCode = ProjectionFailed.Code
}
},
cancellationToken);
}
catch
{
}
throw;
}
}
private static string CreateProcessId(Guid announcementId)
{
return announcementId.ToString("N");
}
private static AdminAnnouncementProjection CreateProjection(AdminAnnouncementProjectionProcessState state)
{
return new AdminAnnouncementProjection(
state.AnnouncementId,
state.Title,
state.Body,
state.PublishedUtc,
state.PublishedByActorId,
state.SourceModuleKey,
state.SourceReference);
}
private static AdminAnnouncementProjectionProcessState CreateState(
AdminAnnouncementProjection announcement,
string step,
bool projectionStored,
string? failureCode)
{
return new AdminAnnouncementProjectionProcessState(
announcement.AnnouncementId,
announcement.Title,
announcement.Body,
announcement.PublishedUtc,
announcement.PublishedByActorId,
announcement.SourceModuleKey,
announcement.SourceReference,
step,
projectionStored,
failureCode);
}
private static AdminAnnouncementReadModel CreateReadModel(AdminAnnouncementProjectionProcessState state)
{
return new AdminAnnouncementReadModel(
state.AnnouncementId,
state.Title,
state.Body,
state.PublishedUtc,
state.PublishedByActorId)
{
SourceModuleKey = state.SourceModuleKey,
SourceReference = state.SourceReference
};
}
}