// Wait for idling to complete.
if (!idleWait()) {
return; // Monitor signaled worker should cancel.
}
TaskDescription taskDesc = service.requestTask();
UUID jobId = taskDesc.getJobId();
int taskId = taskDesc.getTaskId();
if (jobId != null) { // server has a task to perform.
idleEnd(); // Signal that idling is complete.
currentJobId = jobId;
currentTaskId = taskId;
activeWorkers.add(this);
this.monitor.notifyStatusChanged("Obtaining task worker...");
TaskWorker worker;
try {
worker = getTaskWorker(jobId);
} catch (DelegationException e) {
worker = null;
} catch (ClassNotFoundException e) {
service.reportException(jobId, 0, e);
idle(EXCEPTION_IDLE_SECONDS, EXCEPTION_IDLE_MESSAGE);
worker = null;
}
if (worker == null) {
this.monitor.notifyStatusChanged("Could not obtain worker...");
this.monitor.notifyCancelled();
return;
}
this.monitor.notifyStatusChanged("Performing task...");
ClassLoader loader = worker.getClass().getClassLoader();
Object results;
try {
Object task = taskDesc.getTask().deserialize(loader);
results = worker.performTask(task, monitor);
} catch (DelegationException e) {
results = null;
} catch (Exception e) {
service.reportException(jobId, taskId, e);
idle(EXCEPTION_IDLE_SECONDS, EXCEPTION_IDLE_MESSAGE);
results = null;
}
if (results != null && !monitor.isCancelPending()) {
this.monitor.notifyStatusChanged("Submitting task results...");
service.submitTaskResults(jobId, taskId, new Serialized<Object>(results));
}
} else { // server has no tasks to perform.
if (idleBegin()) {
try {
int seconds = (Integer) taskDesc.getTask().deserialize();
this.idle(seconds);
} catch (ClassNotFoundException e) {
throw new UnexpectedException(e);
}
}