1111// See the License for the specific language governing permissions and
1212// limitations under the License.
1313
14+ using Neuroglia . Data . Infrastructure . ResourceOriented ;
1415using Neuroglia . Data . Infrastructure . Services ;
1516
1617namespace Synapse . Operator . Services ;
@@ -93,9 +94,8 @@ protected virtual async Task<WorkflowInstanceHandler> CreateWorkflowInstanceHand
9394 protected virtual async Task < bool > TryClaimAsync ( WorkflowInstance resource , CancellationToken cancellationToken )
9495 {
9596 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 ;
9999 try
100100 {
101101 var originalResource = resource . Clone ( ) ;
@@ -120,9 +120,8 @@ protected virtual async Task<bool> TryClaimAsync(WorkflowInstance resource, Canc
120120 protected virtual async Task < bool > TryReleaseAsync ( WorkflowInstance resource , CancellationToken cancellationToken )
121121 {
122122 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 ;
126125 try
127126 {
128127 var originalResource = resource . Clone ( ) ;
@@ -215,6 +214,20 @@ protected override async Task OnResourceDeletedAsync(WorkflowInstance workflowIn
215214 /// <returns>A new awaitable <see cref="Task"/></returns>
216215 protected virtual Task OnResourceSelectorChangedAsync ( IDictionary < string , string > ? selector ) => this . ReconcileAsync ( this . CancellationTokenSource . Token ) ;
217216
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+
218231 /// <inheritdoc/>
219232 protected override async ValueTask DisposeAsync ( bool disposing )
220233 {
0 commit comments