Search in sources :

Example 1 with RequestCanceled

use of com.vmware.vim25.RequestCanceled in project cloudstack by apache.

the class VmwareClient method cancelTask.

public void cancelTask(ManagedObjectReference task) throws Exception {
    TaskInfo info = (TaskInfo) (getDynamicProperty(task, "info"));
    if (info == null) {
        s_logger.warn("Unable to get the task info, so couldn't cancel the task");
        return;
    }
    String taskName = StringUtils.isNotBlank(info.getName()) ? info.getName() : "Unknown";
    taskName += "(" + info.getKey() + ")";
    String entityName = StringUtils.isNotBlank(info.getEntityName()) ? info.getEntityName() : "";
    if (info.getState().equals(TaskInfoState.SUCCESS)) {
        s_logger.debug(taskName + " task successfully completed for the entity " + entityName + ", can't cancel it");
        return;
    }
    if (info.getState().equals(TaskInfoState.ERROR)) {
        s_logger.debug(taskName + " task execution failed for the entity " + entityName + ", can't cancel it");
        return;
    }
    s_logger.debug(taskName + " task pending for the entity " + entityName + ", trying to cancel");
    if (!info.isCancelable()) {
        s_logger.warn(taskName + " task will continue to run on vCenter because it can't be cancelled");
        return;
    }
    s_logger.debug("Cancelling task " + taskName + " of the entity " + entityName);
    getService().cancelTask(task);
    // Since task cancellation is asynchronous, wait for the task to be cancelled
    Object[] result = waitForValues(task, new String[] { "info.state", "info.error" }, new String[] { "state" }, new Object[][] { new Object[] { TaskInfoState.SUCCESS, TaskInfoState.ERROR } });
    if (result != null && result.length == 2) {
        // result for 2 properties: info.state, info.error
        if (result[0].equals(TaskInfoState.SUCCESS)) {
            s_logger.warn("Failed to cancel" + taskName + " task of the entity " + entityName + ", the task successfully completed");
        }
        if (result[1] instanceof LocalizedMethodFault) {
            MethodFault fault = ((LocalizedMethodFault) result[1]).getFault();
            if (fault instanceof RequestCanceled) {
                s_logger.debug(taskName + " task of the entity " + entityName + " was successfully cancelled");
            }
        } else {
            s_logger.warn("Couldn't cancel " + taskName + " task of the entity " + entityName + " due to " + ((LocalizedMethodFault) result[1]).getLocalizedMessage());
        }
    }
}
Also used : TaskInfo(com.vmware.vim25.TaskInfo) LocalizedMethodFault(com.vmware.vim25.LocalizedMethodFault) MethodFault(com.vmware.vim25.MethodFault) LocalizedMethodFault(com.vmware.vim25.LocalizedMethodFault) RequestCanceled(com.vmware.vim25.RequestCanceled)

Aggregations

LocalizedMethodFault (com.vmware.vim25.LocalizedMethodFault)1 MethodFault (com.vmware.vim25.MethodFault)1 RequestCanceled (com.vmware.vim25.RequestCanceled)1 TaskInfo (com.vmware.vim25.TaskInfo)1