@@ -8,6 +8,8 @@ namespace OrganisationRegistry.ElasticSearch.Projections;
88using System . Threading . Tasks ;
99using Client ;
1010using Configuration ;
11+ using ElasticSearch . Bodies ;
12+ using ElasticSearch . Organisations ;
1113using Infrastructure ;
1214using Infrastructure . Change ;
1315using Microsoft . Extensions . Logging ;
@@ -128,21 +130,22 @@ public async Task Run()
128130 _metrics . NumberOfEnvelopesHandledGauge = envelopes . Count ;
129131 _metrics . NumberOfEnvelopesHandledCounter = envelopes . Count ;
130132 }
131- catch ( ElasticsearchOrganisationNotFoundException organisationNotFoundException )
133+ catch ( ElasticsearchAggregateNotFoundException organisationNotFoundException )
132134 {
133135 await using var organisationRegistryContext = _contextFactory . Create ( ) ;
136+
134137 organisationRegistryContext . OrganisationsToRebuild . Add (
135138 new OrganisationToRebuild
136139 {
137- OrganisationId = Guid . Parse ( organisationNotFoundException . OrganisationId )
140+ OrganisationId = Guid . Parse ( organisationNotFoundException . AggregateId )
138141 } ) ;
139142 await organisationRegistryContext . SaveChangesAsync ( ) ;
140143 _logger . LogWarning (
141144 0 ,
142145 organisationNotFoundException ,
143146 "[{ProjectionName}] Could not find {OrganisationId} in ES while processing envelope #{EnvelopeNumber}, adding it to organisations to rebuild" ,
144147 ProjectionName ,
145- organisationNotFoundException . OrganisationId ,
148+ organisationNotFoundException . AggregateId ,
146149 newLastProcessedEventNumber ) ;
147150 throw ;
148151 }
@@ -168,26 +171,7 @@ private async Task ProcessChange(IElasticChange? changeSetChange, Dictionary<Gui
168171 }
169172 case ElasticPerDocumentChange < T > perDocumentChange :
170173 {
171- foreach ( var documentChange in perDocumentChange . Changes )
172- {
173- T ? document ;
174-
175- if ( ! documentCache . ContainsKey ( documentChange . Key ) )
176- {
177- document = ( await _elastic . TryGetAsync ( async ( ) =>
178- ( await _elastic . WriteClient . GetAsync < T > ( documentChange . Key ) )
179- . ThrowOnFailure ( ) ) )
180- . Source ;
181-
182- documentCache . Add ( documentChange . Key , document ) ;
183- }
184- else
185- {
186- document = documentCache [ documentChange . Key ] ;
187- }
188-
189- await documentChange . Value ( document ) ;
190- }
174+ await HandlePerDocumentChange ( documentCache , perDocumentChange ) ;
191175
192176 break ;
193177 }
@@ -206,6 +190,76 @@ await _elastic.TryGetAsync(async () =>
206190 }
207191 }
208192
193+ private async Task HandlePerDocumentChange ( Dictionary < Guid , T > documentCache , ElasticPerDocumentChange < T > perDocumentChange )
194+ {
195+ try
196+ {
197+ foreach ( var documentChange in perDocumentChange . Changes )
198+ {
199+ T ? document ;
200+
201+ if ( ! documentCache . ContainsKey ( documentChange . Key ) )
202+ {
203+ document = ( await _elastic . TryGetAsync ( async ( ) =>
204+ ( await _elastic . WriteClient . GetAsync < T > ( documentChange . Key ) )
205+ . ThrowOnFailure ( ) ) )
206+ . Source ;
207+
208+ documentCache . Add ( documentChange . Key , document ) ;
209+ }
210+ else
211+ {
212+ document = documentCache [ documentChange . Key ] ;
213+ }
214+
215+ await documentChange . Value ( document ) ;
216+ }
217+ }
218+ catch ( ElasticsearchPerDocumentChangeException e )
219+ {
220+ await using var organisationRegistryContext = _contextFactory . Create ( ) ;
221+
222+ switch ( perDocumentChange )
223+ {
224+ case ElasticPerDocumentChange < OrganisationDocument > :
225+ organisationRegistryContext . OrganisationsToRebuild . Add (
226+ new OrganisationToRebuild
227+ {
228+ OrganisationId = e . AggregateId ,
229+ } ) ;
230+ await organisationRegistryContext . SaveChangesAsync ( ) ;
231+ _logger . LogWarning (
232+ 0 ,
233+ e ,
234+ "[{ProjectionName}] Error occured for {AggregateId} in ES while processing envelope #{EnvelopeNumber}, adding it to entities to rebuild" ,
235+ ProjectionName ,
236+ e . AggregateId ,
237+ e . EnvelopeNumber ) ;
238+
239+ break ;
240+
241+ case ElasticPerDocumentChange < BodyDocument > :
242+ organisationRegistryContext . BodiesToRebuild . Add (
243+ new BodyToRebuild ( )
244+ {
245+ BodyId = e . AggregateId ,
246+ } ) ;
247+ await organisationRegistryContext . SaveChangesAsync ( ) ;
248+ _logger . LogWarning (
249+ 0 ,
250+ e ,
251+ "[{ProjectionName}] Error occured for {AggregateId} in ES while processing envelope #{EnvelopeNumber}, adding it to entities to rebuild" ,
252+ ProjectionName ,
253+ e . AggregateId ,
254+ e . EnvelopeNumber ) ;
255+
256+ break ;
257+ }
258+
259+ throw ;
260+ }
261+ }
262+
209263 private async Task FlushDocuments ( Dictionary < Guid , T > documentCache )
210264 {
211265 if ( documentCache . Any ( ) )
0 commit comments