use of com.vmware.vim25.mo.Task in project CloudStack-archive by CloudStack-extras.
the class VirtualMachineMO method createSnapshot.
public boolean createSnapshot(String snapshotName, String snapshotDescription, boolean dumpMemory, boolean quiesce) throws Exception {
ManagedObjectReference morTask = _context.getService().createSnapshot_Task(_mor, snapshotName, snapshotDescription, dumpMemory, quiesce);
String result = _context.getServiceUtil().waitForTask(morTask);
if (result.equals("sucess")) {
_context.waitForTaskProgressDone(morTask);
ManagedObjectReference morSnapshot = null;
// We still need to wait until the object appear in vCenter
long startTick = System.currentTimeMillis();
while (System.currentTimeMillis() - startTick < 10000) {
morSnapshot = getSnapshotMor(snapshotName);
if (morSnapshot != null) {
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
if (morSnapshot == null)
s_logger.error("We've been waiting for over 10 seconds for snapshot MOR to be appearing in vCenter after CreateSnapshot task is done, but it is still not there?!");
return true;
} else {
s_logger.error("VMware createSnapshot_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
}
return false;
}
use of com.vmware.vim25.mo.Task in project coprhd-controller by CoprHD.
the class VcenterApiClient method removeCluster.
public void removeCluster(String datacenterName, String clusterNameOrMoRef) throws VcenterSystemException, VcenterObjectNotFoundException, VcenterObjectConnectionException {
try {
_log.info("Request to remove cluster in datacenter " + datacenterName + " cluster " + clusterNameOrMoRef);
try {
ClusterComputeResource clusterComputeResource = (ClusterComputeResource) createManagedEntityMap(datacenterName, clusterNameOrMoRef, null, false).get("ClusterComputeResource");
String clusterName = clusterComputeResource.getName();
_log.info("Attempt to delete cluster " + clusterName);
// Remove
Integer clusterOperationTimeout = Integer.parseInt(propertyInfo.getProperty("vcenter_operation_timeout"));
VcenterTaskMonitor taskMonitor = new VcenterTaskMonitor(clusterOperationTimeout);
Task deleteClusterTask = clusterComputeResource.destroy_Task();
// call blocks
VcenterTaskMonitor.TaskStatus taskStatus = taskMonitor.monitor(deleteClusterTask);
if (taskStatus == VcenterTaskMonitor.TaskStatus.SUCCESS) {
_log.info("Delete cluster " + clusterName + " task succeeded");
} else if (taskStatus == VcenterTaskMonitor.TaskStatus.ERROR) {
String errorMessage = "Delete cluster " + clusterName + " task failed - " + taskMonitor.errorDescription;
_log.error(errorMessage);
throw new VcenterSystemException(errorMessage);
} else if (taskStatus == VcenterTaskMonitor.TaskStatus.TIMED_OUT) {
_log.error("Delete cluster " + clusterName + " task timed out at " + taskMonitor.progressPercent);
throw new VcenterSystemException("Delete cluster " + clusterName + " task timed out at " + taskMonitor.progressPercent);
} else {
// Should not execute - Just here in case someone ever added a new state so we catch it
_log.error("Unknown task status encountered tracking delete cluster " + taskStatus);
throw new VcenterSystemException("Unknown task status encountered tracking delete cluster " + taskStatus);
}
} catch (VcenterObjectNotFoundException e) {
_log.info("Cluster not found thus no delete necessary");
}
} catch (VcenterSystemException | VcenterObjectNotFoundException | VcenterObjectConnectionException e) {
throw e;
} catch (Exception e) {
_log.error("Exception removing cluster: " + e);
throw new VcenterSystemException(e.getLocalizedMessage());
}
}
use of com.vmware.vim25.mo.Task in project coprhd-controller by CoprHD.
the class VcenterTaskMonitor method monitor.
/*
* Call will block until terminal state or timeout is met
*/
public TaskStatus monitor(Task task) throws Exception {
TaskInfo taskInfo = null;
try {
_log.info("Monitor task " + task);
while (count < statefulTimeout) {
count += 1;
// MUST DO THIS IN LOOP SINCE THIS FETCHES LATEST INFO FROM VCENTER
taskInfo = task.getTaskInfo();
// _log.debug("${taskInfo.getName()} task progress is " + taskInfo.getProgress())
if (taskInfo.getState() == TaskInfoState.error) {
if (taskInfo.getError() != null) {
errorDescription = taskInfo.getError().getLocalizedMessage();
} else {
errorDescription = "No description provided";
}
_log.error("error description " + errorDescription);
return TaskStatus.ERROR;
} else if (taskInfo.getState() == TaskInfoState.success) {
return TaskStatus.SUCCESS;
} else {
if (taskInfo.getProgress() != null) {
progressPercent = taskInfo.getProgress();
} else {
progressPercent = 0;
}
}
Thread.sleep(1000);
}
_log.error(taskInfo.getName() + " task timed out at state " + taskInfo.getState());
return TaskStatus.TIMED_OUT;
} catch (Exception ex) {
_log.error("Error occurred in task monitor ", ex);
_log.info("task " + task);
_log.info("taskInfo " + taskInfo);
throw new Exception("Error occurred in task monitor " + ex);
}
}
use of com.vmware.vim25.mo.Task in project coprhd-controller by CoprHD.
the class VcenterTaskMonitor method waitForTask.
// Used to wait for a number of tasks within a set of time
public void waitForTask(Task task) throws Exception {
TaskInfo taskInfo = null;
try {
_log.info("Monitor task " + task);
// accessing it.taskInfo refreshes
taskInfo = task.getTaskInfo();
while ((taskInfo.state == TaskInfoState.running || taskInfo.state == TaskInfoState.queued) && count < statefulTimeout) {
_log.info("Wait for " + taskInfo.getState() + " task " + taskInfo.getName() + " " + taskInfo.getDescription() + " to reach terminal state");
// check state every second, wait up until afterCompletionWait
Thread.sleep(1000);
taskInfo = task.getTaskInfo();
count = count + 1;
}
} catch (Exception ex) {
_log.error("Error occurred in task waitForTask ", ex);
_log.info("task " + task);
_log.info("taskInfo " + taskInfo);
throw new Exception("Error occurred in task monitor waitForTask " + ex);
}
}
use of com.vmware.vim25.mo.Task in project coprhd-controller by CoprHD.
the class ComputeSystemControllerImpl method setStorageIOControl.
/**
* Sets the Storage I/O control on a datastore
*
* @param vcenter
* vcenter API for the vcenter
* @param datastore
* the datastore to set storage I/O control
* @param enabled
* if true, enables storage I/O control, otherwise disables storage I/O control
*/
public void setStorageIOControl(VCenterAPI vcenter, Datastore datastore, boolean enabled) {
StorageResourceManager manager = vcenter.getStorageResourceManager();
StorageIORMConfigSpec spec = new StorageIORMConfigSpec();
spec.setEnabled(enabled);
Task task = null;
try {
_log.info("Setting Storage I/O to " + enabled + " on datastore " + datastore.getName());
task = manager.configureDatastoreIORM_Task(datastore, spec);
boolean cancel = false;
long maxTime = System.currentTimeMillis() + (60 * 1000);
while (!isComplete(task)) {
Thread.sleep(5000);
if (System.currentTimeMillis() > maxTime) {
cancel = true;
break;
}
}
if (cancel) {
cancelTask(task);
}
} catch (Exception e) {
_log.error("Error setting storage i/o control");
if (e instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
cancelTaskNoException(task);
}
}
Aggregations