Reputation: 21
I have the following resources defined with specific labels.
Resource:Windows1
Labels:win10,64bit,firefox
Resource:Windows2
Labels:win10,32bit,chrome
Resource:Linux1
Labels:opensuse15.1,32bit,chrome
Resource:Linux2
Labels:opensuse15.1,64bit,chrome
Resource:Linux3
Labels:centos7,64bit,firefox
And have created the following pipeline inside a Jenkins project:
import org.jenkins.plugins.lockableresources.LockableResourcesManager as manager
pipeline {
agent none
stages {
stage('Build') {
agent none
steps {
script {
def myResources = manager.get().getResources()
def String myLabels = manager.get().getAllLabels()
def notLocked = myResources.find{rName->
manager.get().fromName(rName).with{ r-> !r.isLocked() && !r.isQueued() && myLabels.contains("opensuse15.1") && myLabels.contains("chrome") && myLabels.contains("64bit")}
}
if(notLocked){
lock(notLocked){
}
}
}
}
}
}
}
When trying to select a resource based on specific labels i am getting this error:
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: org.jenkins.plugins.lockableresources.LockableResourcesManager.fromName() is applicable for argument types: (org.jenkins.plugins.lockableresources.LockableResource) values: [Windows1]
Possible solutions: fromName(java.lang.String)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
...
has anyone encountered a similar situation and could help me out?
Upvotes: 1
Views: 2127
Reputation: 3261
for pickup the qualified free resource randomly can be also handle as below:
import org.jenkins.plugins.lockableresources.*
LockableResourcesManager manager = org.jenkins.plugins.lockableresources.LockableResourcesManager.get()
List criteria = [ '64bit', 'firefox' ]
List all = manager.resources.findAll { r ->
!r.locked &&
!r.reserved &&
criteria.every{ c -> r.labels.split(',').collect{ it.toLowerCase() }.contains(c.toLowerCase()) }
}
Collections.shuffle(all)
println (all?.first() ?: '')
btw, you can try add/remove test labels by:
import org.jenkins.plugins.lockableresources.*
LockableResourcesManager manager = org.jenkins.plugins.lockableresources.LockableResourcesManager.get()
// add
manager.createResourceWithLabel('marslo-test-1', 'windows,64bit,firefox')
manager.createResourceWithLabel('marslo-test-2', 'CentOS,32bit,chrome')
manager.createResourceWithLabel('marslo-test-3', 'RHEL,64bit,firefox')
// remove
(1..3).each {
manager.resources.remove( manager.fromName("marslo-test-${it}") )
}
Upvotes: 0
Reputation: 21
The code that works for me is:
pipeline {
agent none
stages {
stage('Build') {
agent none
steps {
script {
def OS = "win10"
def bit = "64bit"
def browser = ""
def manager = org.jenkins.plugins.lockableresources.LockableResourcesManager
def myResources = manager.get().resources
def myLabels = manager.get().allLabels
def checkLocked = myResources.find { r ->
!r.isLocked() && !r.isQueued() && r.labels.contains(OS) && r.labels.contains(bit) && r.labels.contains(browser)}
if(checkLocked){
def myResource = checkLocked.toString()
lock(myResource){
println "Resource was succesfully locked"
sleep 30
}
} else {
println "Sorry!!! No resource is available in this moment"
}
}
}
}
}
}
Upvotes: 1
Reputation: 4319
It appears your code assumes the wrong type for rName
: its not a String, it's a LockableResource
.
Therefore the whole manager.get().fromName(rName).with{
is not needed. Try:
def notLocked = myResources.find { r ->
!r.isLocked() && !r.isQueued()
&& myLabels.contains("opensuse15.1")
&& myLabels.contains("chrome")
&& myLabels.contains("64bit")
}
Upvotes: 0