@Override protected void _terminate(TaskListener arg0) throws IOException, InterruptedException { //TODO: Check when this method is getting called and code accordingly LOGGER.log(Level.INFO, "AzureVMAgent: _terminate: called for agent {0}", getNodeName()); ProvisioningActivity activity = CloudStatistics.get().getActivityFor(this); if (activity != null) { activity.enterIfNotAlready(ProvisioningActivity.Phase.COMPLETED); } }
@Override public @Nonnull State dispose() throws Throwable { try { return inner.dispose(); } catch (Throwable ex) { CloudStatistics statistics = CloudStatistics.get(); ProvisioningActivity activity = statistics.getPotentiallyCompletedActivityFor(provisioningId); if (activity != null) { PhaseExecutionAttachment.ExceptionAttachment attachment = new PhaseExecutionAttachment.ExceptionAttachment( ProvisioningActivity.Status.WARN, ex ); statistics.attach(activity, ProvisioningActivity.Phase.COMPLETED, attachment); } throw ex; } }
private String getServerName() { CloudStatistics cs = CloudStatistics.get(); next_number: for (;;) { // Using static counter to ensure colliding template names (between clouds) will not cause a clash String nameCandidate = name + "-" + nodeCounter.getAndIncrement(); // Collide with existing node - quite likely from this cloud if (Jenkins.getInstance().getNode(nameCandidate) != null) continue; // Collide with node being provisioned (at this point this plugin does not assign final name before launch // is completed) or recently used name (just to avoid confusion). for (ProvisioningActivity provisioningActivity : cs.getActivities()) { if (nameCandidate.equals(provisioningActivity.getId().getNodeName())) { continue next_number; } } return nameCandidate; } }
public void cleanCloudStatistics() { Jenkins jenkins = Jenkins.getInstance(); Set<ProvisioningActivity.Id> plannedNodesSet = new HashSet<>(); for (NodeProvisioner.PlannedNode node : jenkins.unlabeledNodeProvisioner.getPendingLaunches()) { if (node instanceof TrackedItem) { plannedNodesSet.add(((TrackedItem) node).getId()); } } for (Label l : jenkins.getLabels()) { for (NodeProvisioner.PlannedNode node : l.nodeProvisioner.getPendingLaunches()) { if (node instanceof TrackedItem) { plannedNodesSet.add(((TrackedItem) node).getId()); } } } for (Node node : jenkins.getNodes()) { if (node instanceof TrackedItem) { plannedNodesSet.add(((TrackedItem) node).getId()); } } Collection<ProvisioningActivity> activities = CloudStatistics.get().getNotCompletedActivities(); for (ProvisioningActivity activity : activities) { if (activity.getCurrentPhase().equals(ProvisioningActivity.Phase.PROVISIONING) && !plannedNodesSet.contains(activity.getId())) { Exception e = new Exception(String.format("Node %s has lost. Mark as failure", activity.getId().toString())); CloudStatistics.ProvisioningListener.get().onFailure(activity.getId(), e); } } }
@Override protected void _terminate(TaskListener listener) { CloudStatistics cloudStatistics = CloudStatistics.get(); ProvisioningActivity activity = cloudStatistics.getActivityFor(this); if (activity != null) { activity.enterIfNotAlready(ProvisioningActivity.Phase.COMPLETED); // Attach what is likely a reason for the termination OfflineCause offlineCause = getFatalOfflineCause(); if (offlineCause != null) { PhaseExecutionAttachment attachment = new PhaseExecutionAttachment(ProvisioningActivity.Status.WARN, offlineCause.toString()); cloudStatistics.attach(activity, ProvisioningActivity.Phase.COMPLETED, attachment); } } // Wrap deletion disposables into statistics tracking disposables AsyncResourceDisposer.get().dispose( new RecordDisposal( new DestroyMachine(cloudName, nodeId), provisioningId ) ); }
@Override public boolean isOutOfScope(@Nonnull Server server) { if (Jenkins.getActiveInstance().getNode(specifier) != null) return false; // The node may be provisioned or deleted at the moment - do not interfere for (ProvisioningActivity pa : CloudStatistics.get().getActivities()) { if (specifier.equals(pa.getName())) { switch (pa.getCurrentPhase()) { case PROVISIONING: return false; // Node not yet created case LAUNCHING: case OPERATING: LOGGER.warning("Node does not exist for " + pa.getCurrentPhase() + " " + specifier); return false; case COMPLETED: return true; } assert false: "Unreachable"; } } Date created = server.getCreated(); if (created != null) { long now = System.currentTimeMillis(); long ageHours = (now - created.getTime()) / (1000 * 60 * 60); if (ageHours < 1) return false; // Make sure not to throw it away during launching } // Resolving activity by name is not reliable as they may not be assigned yet or the activity might already // be rotated when in completed state. In the latter case it should be treated as out of scope. return true; } }
/** * Destroy the node. * If stopOnTerminate is {@code true}, calls {@link ComputeService#suspendNode}, * otherwise {@link ComputeService#destroyNode}. */ @Override protected void _terminate(TaskListener listener) throws IOException, InterruptedException { final ComputeService compute = JCloudsCloud.getByName(cloudName).getCompute(); if (compute.getNodeMetadata(nodeId) != null && compute.getNodeMetadata(nodeId).getStatus().equals(NodeMetadata.Status.RUNNING)) { if (stopOnTerminate) { LOGGER.info("Suspending slave : " + getNodeName()); compute.suspendNode(nodeId); } else { LOGGER.info("Terminating slave : " + getNodeName()); compute.destroyNode(nodeId); } } else { LOGGER.info("Slave " + getNodeName() + " is already not running."); } ProvisioningActivity activity = CloudStatistics.get().getActivityFor(this); if (activity != null) { activity.enterIfNotAlready(ProvisioningActivity.Phase.COMPLETED); } }
ProvisioningActivity activity = CloudStatistics.get().getActivityFor(this); if (nonNull(activity)) { activity.enterIfNotAlready(ProvisioningActivity.Phase.COMPLETED);
ProvisioningActivity activity = CloudStatistics.get().getActivityFor(this); if (nonNull(activity)) { activity.enterIfNotAlready(ProvisioningActivity.Phase.COMPLETED);