private BundleResourceDeployment scheduleBundleResourceDeployment(Subject subject, BundleDeployment deployment,
Resource bundleTarget, boolean isCleanDeployment, boolean isRevert) throws Exception {
int bundleTargetResourceId = bundleTarget.getId();
AgentClient agentClient = agentManager.getAgentClient(subjectManager.getOverlord(), bundleTargetResourceId);
BundleAgentService bundleAgentService = agentClient.getBundleAgentService();
// The BundleResourceDeployment record must exist in the db before the agent request because the agent may try
// to add History to it during immediate deployments. So, create and persist it (requires a new trans).
BundleResourceDeployment resourceDeployment = bundleManager.createBundleResourceDeploymentInNewTrans(
subjectManager.getOverlord(), deployment.getId(), bundleTargetResourceId);
if (null != bundleTarget.getResourceType().getResourceTypeBundleConfiguration()) {
// Ask the agent to schedule the request. The agent should add history as needed.
try {
BundleScheduleRequest request = bundleManager.getScheduleRequest(subject, resourceDeployment.getId(),
isCleanDeployment, isRevert);
// add the deployment request history (in a new trans)
BundleResourceDeploymentHistory history = new BundleResourceDeploymentHistory(subject.getName(),
AUDIT_ACTION_DEPLOYMENT_REQUESTED, deployment.getName(), null,
BundleResourceDeploymentHistory.Status.SUCCESS, "Requested deployment time: "
+ request.getRequestedDeployTimeAsString(), null);
bundleManager.addBundleResourceDeploymentHistoryInNewTrans(subjectManager.getOverlord(),
resourceDeployment.getId(), history);
BundleScheduleResponse response = bundleAgentService.schedule(request);
if (!response.isSuccess()) {
// Handle Schedule Failures. This may include deployment failures for immediate deployment request
bundleManager.setBundleResourceDeploymentStatusInNewTransaction(subject,
resourceDeployment.getId(), BundleDeploymentStatus.FAILURE);