VI Java API: monitoring task completion

In a previous post, I’ve shown how to create a snapshot. It is done by creating a background task:

Task task = vm.createSnapshot_Task("benohead1", "Benohead's first snapshot", false /* memory */, true /* quiesce */);

The call to createSnapshot_Task immediately returns with a Task object. Using this Task instance, you can monitor what’s the progress of the task. It is done:

  • Getting a TaskInfo instance from the task object
  • Checking for the current state

You can do it in a loop like this:

TaskInfoState state;
do {
	Thread.sleep(1000);
	state = task.getTaskInfo().getState();
	System.out.println("State="+state);
}
while (state != TaskInfoState.error && state != TaskInfoState.success);

This will loop until the task succeeds or fails. During each iteration in the loop, it will sleep for 1 second and print the current state of the task.

Additionally, you could also print the current progress of the task:

TaskInfoState state;
do {
	Thread.sleep(1000);
	state = task.getTaskInfo().getState();
	Integer progress = task.getTaskInfo().getProgress();
	if (state == TaskInfoState.success) {
		progress = 100;
	}
	else if (progress == null) {
		progress = 0;
	}
	System.out.println("State="+state+"("+progress+"%)");
}
while (state != TaskInfoState.error && state != TaskInfoState.success);

If the task hasn’t yet done anything task.getTaskInfo().getProgress() will return null. And if the task is already finished, it will also return 0.

If you want to check all recent tasks (basically the lower part of the screen in the vSphere client) it’s pretty easy.
You can get the list of recent tasks from the ServiceInstance:

Task[] tasks = si.getTaskManager().getRecentTasks();

And get for each task some info e.g.:

for (int i = 0; i < tasks.length; i++) {
	TaskInfo info = tasks[i].getTaskInfo();
	String name = info.getName();
	String target = info.getEntityName();
	TaskInfoState state = info.getState();
	Integer progress = info.getProgress();
	if (state == TaskInfoState.success) {
		progress = 100;
	} else if (progress == null) {
		progress = 0;
	}
	LocalizableMessage desc = info.getDescription();
	String description = desc != null ? desc.getMessage() : "";
	TaskReason reason = info.getReason();
	String initiatedBy = "";
	if (reason != null) {
		if (reason instanceof TaskReasonUser) {
			initiatedBy = ((TaskReasonUser) reason).getUserName();
		} else if (reason instanceof TaskReasonSystem) {
			initiatedBy = "System";
		} else if (reason instanceof TaskReasonSchedule) {
			initiatedBy = ((TaskReasonSchedule) reason).getName();
		} else if (reason instanceof TaskReasonAlarm) {
			initiatedBy = ((TaskReasonAlarm) reason).getAlarmName();
		}
	}
	Calendar queueT = info.getQueueTime();
	String queueTime = queueT != null ? queueT.getTime().toString() : "";
	Calendar startT = info.getStartTime();
	String startTime = startT != null ? startT.getTime().toString() : "";
	Calendar completeT = info.getCompleteTime();
	String completeTime = completeT != null ? completeT.getTime().toString() : "";
	System.out.println(name + "t" + target + "t" + state + " " + progress + "%t" + description + "t" + initiatedBy + "t" + queueTime
			+ "t" + startTime + "t" + completeTime);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>