Your browser was unable to load all of the resources. They may have been blocked by your firewall, proxy or browser configuration.
Press Ctrl+F5 or Ctrl+Shift+R to have your browser try again.

Select agent by script #4133

CaoFangyuan ·

Hi Robin,

We want to achieve one thing is to select agent by running script, the script will return the agents are available and then quick build choose one from these agents. I know that if I select "on any build agent", quick build will select the one which is free to run, but our special case is even the agent is free now but others people may book that agent during a certain time. If quick build trigger the build on that agent which may cause problem for the one who book this agent.
So our script will get the information which includes which agent is free and also whether it got booked. Then select one agent from the result. Can this achieve in quick build?

  • solved #11
  • replies 9
  • views 688
  • stars 0
robinshen ADMIN ·

The node selection setting allows you to specify a groovy script to run. It accepts the "node" variable, and if the script returns true for the node, the node is considered to be one of the candidates. You may call method node.hasResource("resource name") in script to determine if a node has specified resource.

CaoFangyuan ·

Hi Robin,

From my understanding, in my case, the groovy script needs to communicate with a database and get status of each agent. So I need to create a resource in "Grid", and in that resource I need to write the script to get the agent status, and if the status is available then return True, then set "on node with specific resource" in the Node selection and select my resource. am I right?

robinshen ADMIN ·

You mentioned that you want to run job on free agent not being booked by others. Can you please let me know how you consider a build agent is free? Does it mean that there is no other builds running on this agent? Also how you are booking a build agent and where is the book info stored? With these info, I might be able to propose a more appropriate solution.

CaoFangyuan ·

Hi Robin,

So all information will store in our database including two tables like this. First, we will analyze the top table, first the remote session should be None which means no body is using this agent right now. And then we check the JHI status, if the status is Idle, then it is available. Final thing is to check the below table to see whether other person booked this agent, if it is booked today, then it couldn't be the candidates. So in one word, to determine whether a agent is free we need to check three things:1. remote by others? 2. Use or idle? 3. booked by others today? Only the agent which meet the remote session is none, idle status and no body book today can be seen as the candidates which means in this case only the agent 4 is free.
Capture.PNG

robinshen ADMIN ·

So it is totally not relevant to QB resource. You may edit node selection setting of the QB step and tell it to run a groovy script, and in your groovy script, connect to database and check if a given node is available, a psuedo script:

groovy:
def agentRecord = queryAgentRecord(node.hostName);
if (agentRecord != null && agentRecord.remoteSession == null && agentRecord.jhiStatus == idle) {
def bookRecord = queryBookRecord(node.hostName);
return bookRecord != null;
} else {
return false;
}

CaoFangyuan ·

Hi Robin,

I have developed the groovy script on my own computer and it runs well. But in quick build UI, I select the node selection as "on node with specific script evaluating to true", after I copy the script to quick build and run, it shows the error. Do you know the reason why the error occurs? BTW, why the println statement in my script info didn't show in the quick build web log, because in my local computer it can show.

error.PNG
CaoFangyuan ·

Hi Robin,

I have fixed that error, it's the problem of my script. Now I have face one problem which is when there is no available agent to run the build, the build will fail with the error "cannot find ant node matching specified criteria". So in this case it will stop the build right now without run. I wonder is there any setting can repeat that build in a certain periodical time if my groovy script doesn't find the free agent rather than stop it immediately. So once we have the available agent we can still build that task. Thanks!

robinshen ADMIN ·

In normal cases QB will fail the build immediately if there is no agent matching your node selection script. Only if grid resource is used, QB put the step into waiting state in such case. So you may define a faked grid resource, with available count on server as a large enough number (10000 for instance), then modify your script something like below:

groovy:
node.hasResource("the faked resource name"); // Calling hasResource to cheat QB of reource usage
// other logic follows
...
CaoFangyuan ·

Thanks Robin, it works.