com.atlassian.bamboo.buildqueue.manager
Class AgentManagerImpl

java.lang.Object
  extended by com.atlassian.bamboo.buildqueue.manager.AgentManagerImpl
All Implemented Interfaces:
AgentManager, LocalAgentManager

public class AgentManagerImpl
extends Object
implements AgentManager, LocalAgentManager


Nested Class Summary
 
Nested classes/interfaces inherited from interface com.atlassian.bamboo.buildqueue.manager.AgentManager
AgentManager.DisabledAgentsInclusion, AgentManager.OfflineAgentsInclusion
 
Field Summary
protected  com.google.common.base.Supplier<ExecutableAgentsHelper> executableAgentsHelper
           
 
Fields inherited from interface com.atlassian.bamboo.buildqueue.manager.AgentManager
DEFAULT_AGENT_NAME
 
Constructor Summary
AgentManagerImpl(AgentDao agentDao, BuildAgentController buildAgentController, CapabilitySetManager capabilitySetManager, com.atlassian.event.api.EventPublisher eventPublisher, BambooLicenseManager bambooLicenseManager, ElasticImageConfigurationManager elasticImageConfigurationManager, ElasticInstanceManager elasticInstanceManager, BuildExecutionManager buildExecutionManager, ErrorUpdateHandler errorUpdateHandler, BuildLoggerManager buildLoggerManager, FeatureManager featureManager, AgentAssignmentService agentAssignmentService, DeploymentsInProgressService deploymentsInProgressService, DeploymentExecutionService deploymentExecutionService)
           
 
Method Summary
 void abandonBuild(BuildAgent remoteAgent, boolean agentStartingUp)
           
 boolean allowNewElasticAgent()
          Allow an elastic agent to register and come online? Based on licensing restrictions.
 boolean allowNewElasticAgents(int numOfAgentsToCreate)
          Allow a specified number of elastic agents to register and come online? Based on licensing restrictions.
 boolean allowNewLocalAgent()
          Allow a local agent to come online? Based on licensing restrictions.
 boolean allowNewRemoteAgent()
          Allow a remote agent to register and come online? Based on licensing restrictions.
 boolean allowNewRemoteAgents(int numOfAgentsToCreate)
          Allow a specified number of remote agents to register and come online? Based on licensing restrictions.
 boolean checkPipelinesAreDisabled()
           
 void createDefaultAgent()
          sets up a default local agent if no local agent exists
 void createLocalAgent(LocalAgentDefinition definition)
          Create a new local agent.
 void disableAllAgents()
          Disable all pipelines
 void enableAllAgents()
          Enable all pipelines
<T> T
executeIfIdle(Callable<T> callable)
          Executes a given Callable only if no builds are in progress.
 Map<Long,PipelineDefinition> findAllAgentsForAgentIds(Collection<Long> agentIds)
          Returns a Map of PipelineDefinitions keyed by their id
 Collection<BuildAgent> getActiveAndEnabledAgents()
          Returns agents that are enabled and online
 BuildAgent getAgent(long agentId)
           
 long getAgentCount(Class<? extends PipelineDefinition> agentDefinitionClass)
          Retrieves the amount of agents of given type stored in the database
 PipelineDefinition getAgentDefinition(Long agentId)
           
 PipelineDefinition getAgentDefinitionByName(String name)
          Retrieve a pipelineDefinition by name.
 List<PipelineDefinition> getAgentDefinitionsWithNameLike(String likeName)
          Return a list of agents whose name is like the provided string.
 List<BuildAgent> getAllAgents()
           
 List<ElasticAgentDefinition> getAllElasticAgentDefinitions(Collection<String> instanceIds)
          Retrieve elastic agent definitions from db.
 List<ElasticAgentDefinition> getAllElasticAgentDefinitions(int firstResult, int maxResults)
          Retrieve most recently terminated elastic agent definitions from db, ordered by termination time, descending.
 List<LocalBuildAgent> getAllLocalAgents()
          Returns all LocalBuildAgent objects
 List<BuildAgent> getAllNonElasticAgents()
           
 List<PipelineDefinition> getAllPersistedAgentDefinitions()
          Retrieves a list of all agent definitions stored in the database.
 List<BuildAgent> getAllRemoteAgents()
          Gets all remote agents in the system.
 List<BuildAgent> getAllRemoteAgents(boolean onlineOnly)
          Get remote agents.
 Collection<BuildAgent> getBusyBuildAgents()
          Gets a collection agents that are currently building or cancelling a build
 Collection<BuildAgent> getExecutableAgents(RequirementSet requirementSet, AgentManager.DisabledAgentsInclusion includeDisabled, AgentManager.OfflineAgentsInclusion includeOffline)
          Gets all agents which can run a given RequirementSet
 Collection<BuildAgent> getExecutableAgents(RequirementSet requirementSet, boolean includeDisabled)
          Gets all agents which can run a given RequirementSet
 ExecutableAgentsMatrix getExecutableAgentsMatrix(RequirementSet requirementSet, AgentManager.DisabledAgentsInclusion includeDisabled, AgentManager.OfflineAgentsInclusion includeOffline)
          Returns a matrix of how many agents match each requirement in the set.
 ExecutableAgentsMatrix getExecutableAgentsMatrix(RequirementSet requirementSet, boolean includeDisabled)
          Returns a matrix of how many agents match each requirement in the set.
 Collection<ElasticImageConfiguration> getExecutableImages(RequirementSet requirementSet)
          Retrieves a list of all images which satisfy the given requirements set.
 Collection<ElasticImageConfiguration> getExecutableImages(RequirementSet requirementSet, boolean includeDisabled)
          Retrieves a list of all images which satisfy the given requirements set.
 LocalBuildAgent getLocalAgent(long pipelineId)
          Returns the pipeline that matches the passed id
 List<BuildAgent> getOnlineElasticAgents()
          Retrieve all online elastic build agents.
 void initAgents()
          Inits the local agents Must be ran before any other methods.
 void removeAgent(long agentId)
          Removes the passed agent
 void saveAnyPipeline(PipelineDefinition pipelineDefinition)
          Stores a pipeline PipelineDefinition of any type (elastic, local, remote) AgentConfigurationUpdateEvent is fired after the pipeline is saved
 void saveAnyPipeline(PipelineDefinition pipelineDefinition, AgentConfigurationUpdatedEventFactory eventFactory)
          Stores a pipeline PipelineDefinition of any type (elastic, local, remote)
 void saveElasticPipeline(ElasticAgentDefinition elasticDefinition)
          Stores the elastic PipelineDefinition
 void savePipeline(PipelineDefinition pipelineDefinition)
          Stores the PipelineDefinition.
 void savePipeline(PipelineDefinition pipelineDefinition, AgentConfigurationUpdatedEventFactory eventFactory)
          Stores the PipelineDefinition.
 void startLocalAgents()
          Starts all current LocalBuildAgent objects
 void stopAgent(BuildAgent agent)
          Performs any other operations required when an agent goes offline, such as updating the status and shutdown time.
 void stopLocalAgents()
          Stops all LocalBuildAgent
 boolean updateAgentStatusIfResultReturnPossibleAfterServerRestart(long agentId, ResultKey resultKey, String displayName, long deploymentResultId)
          Checks if agent is capable of 'surviving' a server restart, i.e.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

executableAgentsHelper

protected com.google.common.base.Supplier<ExecutableAgentsHelper> executableAgentsHelper
Constructor Detail

AgentManagerImpl

public AgentManagerImpl(AgentDao agentDao,
                        BuildAgentController buildAgentController,
                        CapabilitySetManager capabilitySetManager,
                        com.atlassian.event.api.EventPublisher eventPublisher,
                        BambooLicenseManager bambooLicenseManager,
                        ElasticImageConfigurationManager elasticImageConfigurationManager,
                        ElasticInstanceManager elasticInstanceManager,
                        BuildExecutionManager buildExecutionManager,
                        ErrorUpdateHandler errorUpdateHandler,
                        BuildLoggerManager buildLoggerManager,
                        FeatureManager featureManager,
                        AgentAssignmentService agentAssignmentService,
                        DeploymentsInProgressService deploymentsInProgressService,
                        DeploymentExecutionService deploymentExecutionService)
Method Detail

createDefaultAgent

public void createDefaultAgent()
Description copied from interface: AgentManager
sets up a default local agent if no local agent exists

Specified by:
createDefaultAgent in interface AgentManager

initAgents

public void initAgents()
Description copied from interface: AgentManager
Inits the local agents Must be ran before any other methods.

Specified by:
initAgents in interface AgentManager

startLocalAgents

public void startLocalAgents()
Description copied from interface: AgentManager
Starts all current LocalBuildAgent objects

Specified by:
startLocalAgents in interface AgentManager

stopLocalAgents

public void stopLocalAgents()
                     throws InterruptedException
Description copied from interface: AgentManager
Stops all LocalBuildAgent

Specified by:
stopLocalAgents in interface AgentManager
Throws:
InterruptedException

getAllLocalAgents

@NotNull
public List<LocalBuildAgent> getAllLocalAgents()
Description copied from interface: AgentManager
Returns all LocalBuildAgent objects

Specified by:
getAllLocalAgents in interface AgentManager
Returns:
A copied collection of LocalBuildAgent objects, or an empty collection if none exists.

getOnlineElasticAgents

@NotNull
public List<BuildAgent> getOnlineElasticAgents()
Description copied from interface: AgentManager
Retrieve all online elastic build agents.

Specified by:
getOnlineElasticAgents in interface AgentManager
Returns:

getAllElasticAgentDefinitions

@NotNull
public List<ElasticAgentDefinition> getAllElasticAgentDefinitions(int firstResult,
                                                                          int maxResults)
Description copied from interface: AgentManager
Retrieve most recently terminated elastic agent definitions from db, ordered by termination time, descending.

Specified by:
getAllElasticAgentDefinitions in interface AgentManager
Returns:
list of ElasticAgents

getAgentCount

public long getAgentCount(Class<? extends PipelineDefinition> agentDefinitionClass)
Description copied from interface: AgentManager
Retrieves the amount of agents of given type stored in the database

Specified by:
getAgentCount in interface AgentManager
Parameters:
agentDefinitionClass - agent type
Returns:
amount of agents

getAllElasticAgentDefinitions

@NotNull
public List<ElasticAgentDefinition> getAllElasticAgentDefinitions(@NotNull
                                                                          Collection<String> instanceIds)
Description copied from interface: AgentManager
Retrieve elastic agent definitions from db.

Specified by:
getAllElasticAgentDefinitions in interface AgentManager
Parameters:
instanceIds - the instances ids on which the agents were running
Returns:
list of ElasticAgents

getAllRemoteAgents

@NotNull
public List<BuildAgent> getAllRemoteAgents(boolean onlineOnly)
Description copied from interface: AgentManager
Get remote agents.

Specified by:
getAllRemoteAgents in interface AgentManager
Parameters:
onlineOnly - - only return those agents which are online currently.
Returns:

getAllRemoteAgents

@NotNull
public List<BuildAgent> getAllRemoteAgents()
Description copied from interface: AgentManager
Gets all remote agents in the system. They can be offline.

Specified by:
getAllRemoteAgents in interface AgentManager
Returns:
all remote agents.

getAllAgents

@NotNull
public List<BuildAgent> getAllAgents()
Specified by:
getAllAgents in interface AgentManager

getAllNonElasticAgents

@NotNull
public List<BuildAgent> getAllNonElasticAgents()
Specified by:
getAllNonElasticAgents in interface AgentManager

getBusyBuildAgents

@NotNull
public Collection<BuildAgent> getBusyBuildAgents()
Description copied from interface: AgentManager
Gets a collection agents that are currently building or cancelling a build

Specified by:
getBusyBuildAgents in interface AgentManager
Returns:
Collection of BuildAgent

getActiveAndEnabledAgents

@NotNull
public Collection<BuildAgent> getActiveAndEnabledAgents()
Description copied from interface: AgentManager
Returns agents that are enabled and online

Specified by:
getActiveAndEnabledAgents in interface AgentManager
Returns:
Collection of BuildAgent

getExecutableAgents

@NotNull
public Collection<BuildAgent> getExecutableAgents(RequirementSet requirementSet,
                                                          boolean includeDisabled)
Description copied from interface: AgentManager
Gets all agents which can run a given RequirementSet

Specified by:
getExecutableAgents in interface AgentManager
Returns:

getExecutableAgents

@NotNull
public Collection<BuildAgent> getExecutableAgents(RequirementSet requirementSet,
                                                          AgentManager.DisabledAgentsInclusion includeDisabled,
                                                          AgentManager.OfflineAgentsInclusion includeOffline)
Description copied from interface: AgentManager
Gets all agents which can run a given RequirementSet

Specified by:
getExecutableAgents in interface AgentManager
Returns:

getExecutableAgentsMatrix

@NotNull
public ExecutableAgentsMatrix getExecutableAgentsMatrix(@NotNull
                                                                RequirementSet requirementSet,
                                                                boolean includeDisabled)
Description copied from interface: AgentManager
Returns a matrix of how many agents match each requirement in the set. Also adds the total number of agents that matches the set

Specified by:
getExecutableAgentsMatrix in interface AgentManager

getExecutableAgentsMatrix

@NotNull
public ExecutableAgentsMatrix getExecutableAgentsMatrix(@NotNull
                                                                RequirementSet requirementSet,
                                                                AgentManager.DisabledAgentsInclusion includeDisabled,
                                                                AgentManager.OfflineAgentsInclusion includeOffline)
Description copied from interface: AgentManager
Returns a matrix of how many agents match each requirement in the set. Also adds the total number of agents that matches the set

Specified by:
getExecutableAgentsMatrix in interface AgentManager

getExecutableImages

@NotNull
public Collection<ElasticImageConfiguration> getExecutableImages(@NotNull
                                                                         RequirementSet requirementSet,
                                                                         boolean includeDisabled)
Description copied from interface: AgentManager
Retrieves a list of all images which satisfy the given requirements set.

Specified by:
getExecutableImages in interface AgentManager
Parameters:
requirementSet - to check
includeDisabled - whether to include images configurations which are currently disabled
Returns:
Any configured ElasticImageConfiguration which can build the requirement set (currently this can be 1 or none)

getExecutableImages

@NotNull
public Collection<ElasticImageConfiguration> getExecutableImages(@NotNull
                                                                         RequirementSet requirementSet)
Description copied from interface: AgentManager
Retrieves a list of all images which satisfy the given requirements set.

Specified by:
getExecutableImages in interface AgentManager
Parameters:
requirementSet - to check
Returns:
Any configured ElasticImageConfiguration which can build the requirement set (currently this can be 1 or none)

createLocalAgent

public void createLocalAgent(LocalAgentDefinition definition)
                      throws BambooLicenseException
Description copied from interface: AgentManager
Create a new local agent.

Specified by:
createLocalAgent in interface AgentManager
Throws:
BambooLicenseException

savePipeline

public void savePipeline(@NotNull
                         PipelineDefinition pipelineDefinition)
Description copied from interface: AgentManager
Stores the PipelineDefinition. Updates any caches so calls to AgentManager.getLocalAgent(long) will return the LocalBuildAgent returns with the new definition

Specified by:
savePipeline in interface AgentManager
Parameters:
pipelineDefinition - - Must not be null

savePipeline

public void savePipeline(@NotNull
                         PipelineDefinition pipelineDefinition,
                         @Nullable
                         AgentConfigurationUpdatedEventFactory eventFactory)
Description copied from interface: AgentManager
Stores the PipelineDefinition. Updates any caches so calls to AgentManager.getLocalAgent(long) will return the LocalBuildAgent returns with the new definition

Specified by:
savePipeline in interface AgentManager
Parameters:
pipelineDefinition - - Must not be null
eventFactory - event factory used to create event sent after the pipeline is saved

saveElasticPipeline

public void saveElasticPipeline(@NotNull
                                ElasticAgentDefinition elasticDefinition)
Description copied from interface: AgentManager
Stores the elastic PipelineDefinition

Specified by:
saveElasticPipeline in interface AgentManager
Parameters:
elasticDefinition - - Must not be null

getAllPersistedAgentDefinitions

public List<PipelineDefinition> getAllPersistedAgentDefinitions()
Description copied from interface: AgentManager
Retrieves a list of all agent definitions stored in the database.

Specified by:
getAllPersistedAgentDefinitions in interface AgentManager
Returns:
List

abandonBuild

public void abandonBuild(@NotNull
                         BuildAgent remoteAgent,
                         boolean agentStartingUp)
Specified by:
abandonBuild in interface AgentManager

stopAgent

public void stopAgent(@NotNull
                      BuildAgent agent)
Description copied from interface: AgentManager
Performs any other operations required when an agent goes offline, such as updating the status and shutdown time.

Specified by:
stopAgent in interface AgentManager
Parameters:
agent - to be stopped

getLocalAgent

public LocalBuildAgent getLocalAgent(long pipelineId)
Description copied from interface: AgentManager
Returns the pipeline that matches the passed id

Specified by:
getLocalAgent in interface AgentManager
Returns:
Matching LocalBuildAgent object. Null if pipeline does not exist

getAgent

@Nullable
public BuildAgent getAgent(long agentId)
Specified by:
getAgent in interface AgentManager

getAgentDefinition

@Nullable
public PipelineDefinition getAgentDefinition(@NotNull
                                                      Long agentId)
Specified by:
getAgentDefinition in interface AgentManager

findAllAgentsForAgentIds

public Map<Long,PipelineDefinition> findAllAgentsForAgentIds(@NotNull
                                                             Collection<Long> agentIds)
Description copied from interface: AgentManager
Returns a Map of PipelineDefinitions keyed by their id

Specified by:
findAllAgentsForAgentIds in interface AgentManager
Returns:
agentMap

getAgentDefinitionByName

@Nullable
public PipelineDefinition getAgentDefinitionByName(@NotNull
                                                            String name)
Description copied from interface: AgentManager
Retrieve a pipelineDefinition by name. Names should be unique.

Specified by:
getAgentDefinitionByName in interface AgentManager
Parameters:
name - of the agent to find
Returns:
the agent that has the given name.

getAgentDefinitionsWithNameLike

@NotNull
public List<PipelineDefinition> getAgentDefinitionsWithNameLike(@NotNull
                                                                        String likeName)
Description copied from interface: AgentManager
Return a list of agents whose name is like the provided string. Uses hibernate's "like" so % to mark wild card.

Specified by:
getAgentDefinitionsWithNameLike in interface AgentManager
Parameters:
likeName - the string to match
Returns:
List of Pipeline Definitions

saveAnyPipeline

public void saveAnyPipeline(@NotNull
                            PipelineDefinition pipelineDefinition)
Description copied from interface: AgentManager
Stores a pipeline PipelineDefinition of any type (elastic, local, remote) AgentConfigurationUpdateEvent is fired after the pipeline is saved

Specified by:
saveAnyPipeline in interface AgentManager
Parameters:
pipelineDefinition - - Must not be null

saveAnyPipeline

public void saveAnyPipeline(@NotNull
                            PipelineDefinition pipelineDefinition,
                            @Nullable
                            AgentConfigurationUpdatedEventFactory eventFactory)
Description copied from interface: AgentManager
Stores a pipeline PipelineDefinition of any type (elastic, local, remote)

Specified by:
saveAnyPipeline in interface AgentManager
Parameters:
pipelineDefinition - Must not be null
eventFactory - event factory used to create event sent after the pipeline is saved

disableAllAgents

public void disableAllAgents()
Description copied from interface: AgentManager
Disable all pipelines

Specified by:
disableAllAgents in interface AgentManager

enableAllAgents

public void enableAllAgents()
Description copied from interface: AgentManager
Enable all pipelines

Specified by:
enableAllAgents in interface AgentManager

checkPipelinesAreDisabled

public boolean checkPipelinesAreDisabled()
Specified by:
checkPipelinesAreDisabled in interface AgentManager
Returns:
whether or not all pipelines are disabled

executeIfIdle

public <T> T executeIfIdle(Callable<T> callable)
                throws Exception
Description copied from interface: AgentManager

Executes a given Callable only if no builds are in progress. Builds are prevented from starting while the Callable executes.

Specified by:
executeIfIdle in interface AgentManager
Parameters:
callable - the Callable to be executed
Returns:
the result of callable
Throws:
BambooBusyException - if builds are in progress
Exception - if thrown by callable

removeAgent

public void removeAgent(long agentId)
                 throws TimeoutException
Description copied from interface: AgentManager
Removes the passed agent

Specified by:
removeAgent in interface AgentManager
Throws:
TimeoutException

allowNewRemoteAgent

public boolean allowNewRemoteAgent()
Description copied from interface: AgentManager
Allow a remote agent to register and come online? Based on licensing restrictions. The method takes into consideration the number of agents "reserved" for pending elastic instances.

Specified by:
allowNewRemoteAgent in interface AgentManager
Returns:
true if a new agent is allowed, otherise false

allowNewRemoteAgents

public boolean allowNewRemoteAgents(int numOfAgentsToCreate)
Description copied from interface: AgentManager
Allow a specified number of remote agents to register and come online? Based on licensing restrictions. The method takes into consideration the number of agents "reserved" for pending elastic instances.

Specified by:
allowNewRemoteAgents in interface AgentManager
Returns:
true if a specified number agents is allowed, otherise false

allowNewElasticAgent

public boolean allowNewElasticAgent()
Description copied from interface: AgentManager
Allow an elastic agent to register and come online? Based on licensing restrictions.

Specified by:
allowNewElasticAgent in interface AgentManager
Returns:
true if a new agent is allowed, otherise false

allowNewElasticAgents

public boolean allowNewElasticAgents(int numOfAgentsToCreate)
Description copied from interface: AgentManager
Allow a specified number of elastic agents to register and come online? Based on licensing restrictions.

Specified by:
allowNewElasticAgents in interface AgentManager
Returns:
true if a specified number agents is allowed, otherise false

allowNewLocalAgent

public boolean allowNewLocalAgent()
Description copied from interface: AgentManager
Allow a local agent to come online? Based on licensing restrictions.

Specified by:
allowNewLocalAgent in interface AgentManager
Returns:
true if a new local agent is allowed, otherwise false

updateAgentStatusIfResultReturnPossibleAfterServerRestart

public boolean updateAgentStatusIfResultReturnPossibleAfterServerRestart(long agentId,
                                                                         ResultKey resultKey,
                                                                         String displayName,
                                                                         long deploymentResultId)
Description copied from interface: AgentManager
Checks if agent is capable of 'surviving' a server restart, i.e. if agent exists and is configured to reconnect after server restart, allowing it to return build result produced while the server was down. Sets agent status to 'building' if it is so.

Specified by:
updateAgentStatusIfResultReturnPossibleAfterServerRestart in interface AgentManager
Returns:
true iff agents is capable of surviving a server restart.


Copyright © 2015 Atlassian Software Systems Pty Ltd. All rights reserved.