11
11
// See the License for the specific language governing permissions and
12
12
// limitations under the License.
13
13
14
+ using Neuroglia . Data . Infrastructure . Services ;
15
+
14
16
namespace Synapse . Operator . Services ;
15
17
16
18
/// <summary>
@@ -21,7 +23,8 @@ namespace Synapse.Operator.Services;
21
23
/// <param name="controllerOptions">The service used to access the current <see cref="IOptions{TOptions}"/></param>
22
24
/// <param name="repository">The service used to manage <see cref="IResource"/>s</param>
23
25
/// <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 )
25
28
: ResourceController < WorkflowInstance > ( loggerFactory , controllerOptions , repository )
26
29
{
27
30
@@ -35,6 +38,11 @@ public class WorkflowInstanceController(IServiceProvider serviceProvider, ILogge
35
38
/// </summary>
36
39
protected IResourceMonitor < Resources . Operator > Operator => operatorController . Operator ;
37
40
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
+
38
46
/// <summary>
39
47
/// Gets a <see cref="ConcurrentDictionary{TKey, TValue}"/> that contains current <see cref="WorkflowInstanceHandler"/>es
40
48
/// </summary>
@@ -139,24 +147,54 @@ public override async Task StopAsync(CancellationToken cancellationToken)
139
147
/// <inheritdoc/>
140
148
protected override async Task OnResourceCreatedAsync ( WorkflowInstance workflowInstance , CancellationToken cancellationToken = default )
141
149
{
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
+ }
146
161
}
147
162
148
163
/// <inheritdoc/>
149
164
protected override async Task OnResourceDeletedAsync ( WorkflowInstance workflowInstance , CancellationToken cancellationToken = default )
150
165
{
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
154
167
{
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
+ {
155
172
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 )
158
196
{
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 ) ;
160
198
}
161
199
}
162
200
0 commit comments