package jalview.ws2.operations; import static java.lang.String.format; import java.io.IOException; import java.util.HashMap; import java.util.Map; import jalview.bin.Cache; import jalview.util.MathUtils; import jalview.ws2.WSJob; import jalview.ws2.WSJobStatus; public abstract class AbstractWorker implements WebServiceWorkerI { protected long uid = MathUtils.getUID(); public long getUID() { return uid; } protected WebServiceWorkerListenersList listeners = new WebServiceWorkerListenersList(this); public void addListener(WebServiceWorkerListener listener) { listeners.addListener(listener); } private Map exceptionCount = new HashMap<>(); protected static final int MAX_RETRY = 5; public boolean poll() { boolean done = true; for (WSJob job : getJobs()) { if (!job.getStatus().isDone() && !job.getStatus().isFailed()) { Cache.log.debug(format("Polling job %s.", job)); try { getOperation().getWebService().updateProgress(job); exceptionCount.remove(job.getUid()); } catch (IOException e) { Cache.log.error(format("Polling job %s failed.", job), e); listeners.firePollException(job, e); int count = exceptionCount.getOrDefault(job.getUid(), MAX_RETRY); if (--count <= 0) { job.setStatus(WSJobStatus.SERVER_ERROR); Cache.log.warn(format( "Attempts limit exceeded. Droping job %s.", job)); } exceptionCount.put(job.getUid(), count); } catch (OutOfMemoryError e) { job.setStatus(WSJobStatus.BROKEN); Cache.log.error( format("Out of memory when retrieving job %s", job), e); } Cache.log.debug( format("Job %s status is %s", job, job.getStatus())); } done &= job.getStatus().isDone() || job.getStatus().isFailed(); } return done; } }