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 . ResourceOriented ;
14
15
using Neuroglia . Data . Infrastructure . Services ;
15
16
16
17
namespace Synapse . Operator . Services ;
@@ -93,9 +94,8 @@ protected virtual async Task<WorkflowInstanceHandler> CreateWorkflowInstanceHand
93
94
protected virtual async Task < bool > TryClaimAsync ( WorkflowInstance resource , CancellationToken cancellationToken )
94
95
{
95
96
ArgumentNullException . ThrowIfNull ( resource ) ;
96
- if ( resource . Metadata . Labels != null && resource . Metadata . Labels . TryGetValue ( SynapseDefaults . Resources . Labels . Operator , out var operatorQualifiedName ) ) return operatorQualifiedName == this . Operator . Resource . GetQualifiedName ( ) ;
97
- if ( this . Workflows . TryGetValue ( this . GetResourceCacheKey ( resource . Spec . Definition . Name , resource . Spec . Definition . Namespace ) , out var workflow ) && workflow != null
98
- && workflow . Metadata . Labels != null && workflow . Metadata . Labels . TryGetValue ( SynapseDefaults . Resources . Labels . Operator , out operatorQualifiedName ) ) return operatorQualifiedName == this . Operator . Resource . GetQualifiedName ( ) ;
97
+ var isClaimable = this . IsWorkflowInstanceClaimable ( resource ) ;
98
+ if ( isClaimable . HasValue ) return isClaimable . Value ;
99
99
try
100
100
{
101
101
var originalResource = resource . Clone ( ) ;
@@ -120,9 +120,8 @@ protected virtual async Task<bool> TryClaimAsync(WorkflowInstance resource, Canc
120
120
protected virtual async Task < bool > TryReleaseAsync ( WorkflowInstance resource , CancellationToken cancellationToken )
121
121
{
122
122
ArgumentNullException . ThrowIfNull ( resource ) ;
123
- if ( resource . Metadata . Labels != null && resource . Metadata . Labels . TryGetValue ( SynapseDefaults . Resources . Labels . Operator , out var operatorQualifiedName ) ) return operatorQualifiedName == this . Operator . Resource . GetQualifiedName ( ) ;
124
- if ( this . Workflows . TryGetValue ( this . GetResourceCacheKey ( resource . Spec . Definition . Name , resource . Spec . Definition . Namespace ) , out var workflow ) && workflow != null
125
- && workflow . Metadata . Labels != null && workflow . Metadata . Labels . TryGetValue ( SynapseDefaults . Resources . Labels . Operator , out operatorQualifiedName ) ) return operatorQualifiedName == this . Operator . Resource . GetQualifiedName ( ) ;
123
+ var isClaimable = this . IsWorkflowInstanceClaimable ( resource ) ;
124
+ if ( isClaimable . HasValue ) return isClaimable . Value ;
126
125
try
127
126
{
128
127
var originalResource = resource . Clone ( ) ;
@@ -215,6 +214,20 @@ protected override async Task OnResourceDeletedAsync(WorkflowInstance workflowIn
215
214
/// <returns>A new awaitable <see cref="Task"/></returns>
216
215
protected virtual Task OnResourceSelectorChangedAsync ( IDictionary < string , string > ? selector ) => this . ReconcileAsync ( this . CancellationTokenSource . Token ) ;
217
216
217
+ /// <summary>
218
+ /// Determines whether or not the specified <see cref="WorkflowInstance"/> can be claimed by the current <see cref="Resources.Operator"/>
219
+ /// </summary>
220
+ /// <param name="workflowInstance">The <see cref="WorkflowInstance"/> to check</param>
221
+ /// <returns>A boolean indicating whether or not the specified <see cref="WorkflowInstance"/> can be claimed by the current <see cref="Resources.Operator"/></returns>
222
+ protected virtual bool ? IsWorkflowInstanceClaimable ( WorkflowInstance workflowInstance )
223
+ {
224
+ ArgumentNullException . ThrowIfNull ( workflowInstance ) ;
225
+ if ( workflowInstance . Metadata . Labels != null && workflowInstance . Metadata . Labels . TryGetValue ( SynapseDefaults . Resources . Labels . Operator , out var operatorQualifiedName ) ) return operatorQualifiedName == this . Operator . Resource . GetQualifiedName ( ) ;
226
+ if ( this . Workflows . TryGetValue ( this . GetResourceCacheKey ( workflowInstance . Spec . Definition . Name , workflowInstance . Spec . Definition . Namespace ) , out var workflow ) && workflow != null
227
+ && workflow . Metadata . Labels != null && workflow . Metadata . Labels . TryGetValue ( SynapseDefaults . Resources . Labels . Operator , out operatorQualifiedName ) ) return operatorQualifiedName == this . Operator . Resource . GetQualifiedName ( ) ;
228
+ return null ;
229
+ }
230
+
218
231
/// <inheritdoc/>
219
232
protected override async ValueTask DisposeAsync ( bool disposing )
220
233
{
0 commit comments