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.

Get Available Resource's count #4591

chrisnak ·

Hello everyone,

Lately I'm working a lot with resources so I have another resource related question.
In my current scenario I want to get the available resource count to perform some actions.
I have figure out a way to do so with the following code:

1. import com.pmease.quickbuild.entitymanager.ResourceManager;
2. import com.pmease.quickbuild.rest.resources.ResourceResource;
3. 
4. def resource = ResourceManager.instance.get( "ResourceName");
5. def resource_id = resource.getId();
6. // null checks
7. 
8. ResourceResource r_resource = new ResourceResource();
9. def available_resource_obj = r_resource.getAvailable( resource_id );

So I'm utilizing the ResourceResource class to get the available resources count and it works if I start a build. The problem arises when the scheduler starts the build. On which case I get this error message:

ERROR - Step 'master>Test_Get_Available_Resource' is failed: null

So I have the following questsions;

a. Is this caused because I'm using the rest classes? If yes, is something wrong with related to QB's config?
b. Could I get the available resources with some other way that's not too complicated ?

  • solved #2
  • replies 2
  • views 269
  • stars 0
robinshen ADMIN ·

The restful resource is not assumed to be called inside groovy script. However you may copy the logic of that method directly into your groovy script, ie, below Java code:

Map<String, Integer> nodeResources = new HashMap<>();

List<BuildRequest> requests = BuildEngine.instance.getBuildRequests(null);
for (GridNode node: Grid.instance.getAllNodes()) {
	int total = ((NodeResourceType)resource.getType()).getCount(resource.getName(), node);

	if (total != 0) {
		int used = 0;
		for (BuildRequest request: requests) {
			Build build = request.getBuild();
			synchronized (request) {
				for (Step step: build.getSteps()) {
					if (step.isRunning() && step.getNodeAddress().equals(node.getAddress())) { 
						Integer stepUsed = step.getResources().get(resource.getName());
						if (stepUsed != null)
							used += stepUsed;
					}
				}
			}
		}
		int available = total - used;
		if (available > 0)
			nodeResources.put(node.getAddress(), available);
	}
}
return nodeResources;
chrisnak ·

Thanks for your quick reply Robin.
I created a helper function with the provided code so it can be reused.
Works as intended!