1111// See the License for the specific language governing permissions and
1212// limitations under the License.
1313
14+ using Neuroglia . Data . Infrastructure . Services ;
15+
1416namespace Synapse . Operator . Services ;
1517
1618/// <summary>
@@ -21,7 +23,8 @@ namespace Synapse.Operator.Services;
2123/// <param name="controllerOptions">The service used to access the current <see cref="IOptions{TOptions}"/></param>
2224/// <param name="repository">The service used to manage <see cref="IResource"/>s</param>
2325/// <param name="operatorController">The service used to access the current <see cref="Resources.Operator"/></param>
24- public class WorkflowInstanceController ( IServiceProvider serviceProvider , ILoggerFactory loggerFactory , IOptions < ResourceControllerOptions < WorkflowInstance > > controllerOptions , IResourceRepository repository , IOperatorController operatorController )
26+ /// <param name="documents">The <see cref="IRepository"/> used to manage <see cref="Document"/>s</param>
27+ public class WorkflowInstanceController ( IServiceProvider serviceProvider , ILoggerFactory loggerFactory , IOptions < ResourceControllerOptions < WorkflowInstance > > controllerOptions , IResourceRepository repository , IOperatorController operatorController , IRepository < Document , string > documents )
2528 : ResourceController < WorkflowInstance > ( loggerFactory , controllerOptions , repository )
2629{
2730
@@ -35,6 +38,11 @@ public class WorkflowInstanceController(IServiceProvider serviceProvider, ILogge
3538 /// </summary>
3639 protected IResourceMonitor < Resources . Operator > Operator => operatorController . Operator ;
3740
41+ /// <summary>
42+ /// Gets the <see cref="IRepository"/> used to manage <see cref="Document"/>s
43+ /// </summary>
44+ protected IRepository < Document , string > Documents => documents ;
45+
3846 /// <summary>
3947 /// Gets a <see cref="ConcurrentDictionary{TKey, TValue}"/> that contains current <see cref="WorkflowInstanceHandler"/>es
4048 /// </summary>
@@ -139,24 +147,54 @@ public override async Task StopAsync(CancellationToken cancellationToken)
139147 /// <inheritdoc/>
140148 protected override async Task OnResourceCreatedAsync ( WorkflowInstance workflowInstance , CancellationToken cancellationToken = default )
141149 {
142- await base . OnResourceCreatedAsync ( workflowInstance , cancellationToken ) . ConfigureAwait ( false ) ;
143- if ( ! await this . TryClaimAsync ( workflowInstance , cancellationToken ) . ConfigureAwait ( false ) ) return ;
144- var handler = await this . CreateWorkflowInstanceHandlerAsync ( workflowInstance , cancellationToken ) . ConfigureAwait ( false ) ;
145- await handler . HandleAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
150+ try
151+ {
152+ await base . OnResourceCreatedAsync ( workflowInstance , cancellationToken ) . ConfigureAwait ( false ) ;
153+ if ( ! await this . TryClaimAsync ( workflowInstance , cancellationToken ) . ConfigureAwait ( false ) ) return ;
154+ var handler = await this . CreateWorkflowInstanceHandlerAsync ( workflowInstance , cancellationToken ) . ConfigureAwait ( false ) ;
155+ await handler . HandleAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
156+ }
157+ catch ( Exception ex )
158+ {
159+ this . Logger . LogError ( "An error occured while handling the creation of workflow instance '{workflowInstance}': {ex}" , workflowInstance . GetQualifiedName ( ) , ex ) ;
160+ }
146161 }
147162
148163 /// <inheritdoc/>
149164 protected override async Task OnResourceDeletedAsync ( WorkflowInstance workflowInstance , CancellationToken cancellationToken = default )
150165 {
151- await base . OnResourceDeletedAsync ( workflowInstance , cancellationToken ) . ConfigureAwait ( false ) ;
152- if ( this . Handlers . TryRemove ( workflowInstance . GetQualifiedName ( ) , out var process ) ) await process . DisposeAsync ( ) . ConfigureAwait ( false ) ;
153- var selectors = new LabelSelector [ ]
166+ try
154167 {
168+ await base . OnResourceDeletedAsync ( workflowInstance , cancellationToken ) . ConfigureAwait ( false ) ;
169+ if ( this . Handlers . TryRemove ( workflowInstance . GetQualifiedName ( ) , out var process ) ) await process . DisposeAsync ( ) . ConfigureAwait ( false ) ;
170+ var selectors = new LabelSelector [ ]
171+ {
155172 new ( SynapseDefaults . Resources . Labels . WorkflowInstance , LabelSelectionOperator . Equals , workflowInstance . GetQualifiedName ( ) )
156- } ;
157- await foreach ( var correlation in this . Repository . GetAllAsync < Correlation > ( null , selectors , cancellationToken : cancellationToken ) )
173+ } ;
174+ await foreach ( var correlation in this . Repository . GetAllAsync < Correlation > ( null , selectors , cancellationToken : cancellationToken ) )
175+ {
176+ await this . Repository . RemoveAsync < Correlation > ( correlation . GetName ( ) , correlation . GetNamespace ( ) , false , cancellationToken ) . ConfigureAwait ( false ) ;
177+ }
178+ if ( workflowInstance . Status != null )
179+ {
180+ var documentReferences = new List < string > ( ) ;
181+ if ( ! string . IsNullOrWhiteSpace ( workflowInstance . Status . ContextReference ) ) documentReferences . Add ( workflowInstance . Status . ContextReference ) ;
182+ if ( ! string . IsNullOrWhiteSpace ( workflowInstance . Status . OutputReference ) ) documentReferences . Add ( workflowInstance . Status . OutputReference ) ;
183+ if ( workflowInstance . Status . Tasks != null )
184+ {
185+ foreach ( var task in workflowInstance . Status . Tasks )
186+ {
187+ if ( ! string . IsNullOrWhiteSpace ( task . ContextReference ) ) documentReferences . Add ( task . ContextReference ) ;
188+ if ( ! string . IsNullOrWhiteSpace ( task . InputReference ) ) documentReferences . Add ( task . InputReference ) ;
189+ if ( ! string . IsNullOrWhiteSpace ( task . OutputReference ) ) documentReferences . Add ( task . OutputReference ) ;
190+ }
191+ }
192+ foreach ( var documentReference in documentReferences . Distinct ( ) ) await this . Documents . RemoveAsync ( documentReference , cancellationToken ) . ConfigureAwait ( false ) ;
193+ }
194+ }
195+ catch ( Exception ex )
158196 {
159- await this . Repository . RemoveAsync < Correlation > ( correlation . GetName ( ) , correlation . GetNamespace ( ) , false , cancellationToken ) . ConfigureAwait ( false ) ;
197+ this . Logger . LogError ( "An error occured while handling the deletion of workflow instance '{workflowInstance}': {ex}" , workflowInstance . GetQualifiedName ( ) , ex ) ;
160198 }
161199 }
162200
0 commit comments