param ( [parameter(Mandatory=$false, HelpMessage="Name of the workflow for which Failed cmd execution reservations needs to be cleaned off.")] [string]$workflowName ) if ($PSVersionTable.PSVersion.Major -le '2') { throw("Minimun PowerShell version required is 3.0") } $REGISTRY = "HKLM:\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\NA_WFA_SRV\Parameters\Java" $httpPort = (Get-ItemProperty $REGISTRY |select -ExpandProperty Options|where {$_ -match "-Dhttp.port"}).split("=")[1] if($workflowName) { $url1 = "http://localhost:" + $httpPort + "/rest/workflows?name=" + $workflowName } else { $url1= 'http://localhost:' + $httpPort + '/rest/workflows' } $cd=Get-WfaCredentials -Host "localhost" if(!$cd) { throw("Credentials for localhost not found") } Get-WfaLogger -Info -Message 'Started looking for the workflows' $workflowList = Invoke-RestMethod -Method get -Uri $url1 -Credential $cd -TimeoutSec 300 foreach ( $wf in $workflowList.collection.workflow ) { $workflow = $wf.name $uuid = $wf.uuid $myWorkflowName = Get-WfaRestParameter "workflowName" if($myWorkflowName -eq $workflow) { #This is itself. Ignore the self workflow continue } $uri_workflow= "http://localhost:" + $httpPort + "/rest/search?term='" + $workflow + "'&context=EXECUTION_STATUS" $result = Invoke-RestMethod -Method get -Uri $uri_workflow -Credential $cd foreach($jobId in $result.collection.searchResult.id) { $Url2= "http://localhost:" + $httpPort + "/rest/workflows/"+ $uuid + "/jobs/" + $jobId try { $job = Invoke-RestMethod -Method get -Uri $Url2 -Credential $cd $status=$job.job.jobStatus.jobStatus } catch { #This job ID didn't belong to this workflow. Search returns other matching jobs too. Skip it. continue } if($status -eq 'FAILED') { #Clean the Job's reservation of the failed commands. $Url3="http://localhost:" + $httpPort +"/rest/workflows/"+ $uuid + "/jobs/" + $jobId +"/reservation" #Invoke REST api. Invoke-RestMethod -Method Delete -Uri $Url3 -Credential $cd Get-WfaLogger -Info -Messag "Reservation cleaned successfully for workflow: $workflow, job Id: $jobId" } } } #end foreach wf