X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fws%2Fjws2%2FSeqAnnotationServiceCalcWorker.java;h=3253f438efb0c4a0d059b153dc35e33e890054ad;hb=d043ce47fc710d3eb2629ba926a8a7417bd67d8c;hp=661bdf7228c31a05929d3fcffe1b06f694a01e6b;hpb=4ad3af6fae856880fea0a982e6f0dc65f3109567;p=jalview.git diff --git a/src/jalview/ws/jws2/SeqAnnotationServiceCalcWorker.java b/src/jalview/ws/jws2/SeqAnnotationServiceCalcWorker.java index 661bdf7..3253f43 100644 --- a/src/jalview/ws/jws2/SeqAnnotationServiceCalcWorker.java +++ b/src/jalview/ws/jws2/SeqAnnotationServiceCalcWorker.java @@ -26,7 +26,9 @@ import jalview.analysis.SeqsetUtils; import jalview.api.AlignViewportI; import jalview.api.AlignmentViewPanel; import jalview.api.FeatureColourI; +import jalview.api.PollableAlignCalcWorkerI; import jalview.bin.Cache; +import jalview.bin.Console; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.AlignmentI; import jalview.datamodel.AnnotatedCollectionI; @@ -64,7 +66,7 @@ import java.util.List; import java.util.Map; public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker - implements WSAnnotationCalcManagerI + implements WSAnnotationCalcManagerI, PollableAlignCalcWorkerI { protected ServiceWithParameters service; @@ -162,17 +164,11 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker return gapMap; } - public SeqAnnotationServiceCalcWorker(AlignViewportI alignViewport, - AlignmentViewPanel alignPanel) - { - super(alignViewport, alignPanel); - } - public SeqAnnotationServiceCalcWorker(ServiceWithParameters service, AlignFrame alignFrame, WsParamSetI preset, List paramset) { - this(alignFrame.getCurrentView(), alignFrame.alignPanel); + super(alignFrame.getCurrentView(), alignFrame.alignPanel); // TODO: both these fields needed ? this.alignFrame = alignFrame; this.guiProgress = alignFrame; @@ -185,7 +181,8 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker .getEndpoint(); } catch (ClassCastException cce) { - JvOptionPane.showMessageDialog(Desktop.desktop, + annotService = null; + JvOptionPane.showMessageDialog(Desktop.getInstance(), MessageManager.formatMessage( "label.service_called_is_not_an_annotation_service", new String[] @@ -194,6 +191,7 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker JvOptionPane.WARNING_MESSAGE); } + cancellable = CancellableI.class.isInstance(annotService); // configure submission flags proteinAllowed = service.isProteinService(); nucleotidesAllowed = service.isNucleotideService(); @@ -218,7 +216,8 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker return annotService != null; } - protected jalview.ws.api.SequenceAnnotationServiceI annotService = null; + protected SequenceAnnotationServiceI annotService; + protected final boolean cancellable; volatile JobId rslt = null; @@ -226,176 +225,123 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker private int min_valid_seqs; - @Override - public void run() + + private long progressId = -1; + JobStateSummary job = null; + WebserviceInfo info = null; + List seqs = null; + + @Override public void startUp() throws Throwable { - if (checkDone()) + if (alignViewport.isClosed()) { + abortAndDestroy(); return; } if (!hasService()) { - calcMan.workerComplete(this); return; } - long progressId = -1; - - int serverErrorsLeft = 3; - final boolean cancellable = CancellableI.class - .isAssignableFrom(annotService.getClass()); StringBuffer msg = new StringBuffer(); - JobStateSummary job = new JobStateSummary(); - WebserviceInfo info = new WebserviceInfo("foo", "bar", false); - try - { - List seqs = getInputSequences( - alignViewport.getAlignment(), - bySequence ? alignViewport.getSelectionGroup() : null); + job = new JobStateSummary(); + info = new WebserviceInfo("foo", "bar", false); - if (seqs == null || !checkValidInputSeqs(seqs)) - { - jalview.bin.Cache.log.debug( - "Sequences for analysis service were null or not valid"); - calcMan.workerComplete(this); - return; - } - - if (guiProgress != null) - { - guiProgress.setProgressBar(service.getActionText(), - progressId = System.currentTimeMillis()); - } - jalview.bin.Cache.log.debug("submitted " + seqs.size() - + " sequences to " + service.getActionText()); + seqs = getInputSequences( + alignViewport.getAlignment(), + bySequence ? alignViewport.getSelectionGroup() : null); - rslt = annotService.submitToService(seqs, getPreset(), - getArguments()); - if (rslt == null) - { - return; - } - // TODO: handle job submission error reporting here. - Cache.log.debug("Service " + service.getUri() + "\nSubmitted job ID: " - + rslt); - ; - // /// - // otherwise, construct WsJob and any UI handlers - running = new AnnotationWsJob(); - running.setJobHandle(rslt); - running.setSeqNames(seqNames); - running.setStartPos(start); - running.setSeqs(seqs); - job.updateJobPanelState(info, "", running); - if (guiProgress != null) - { - guiProgress.registerHandler(progressId, - new IProgressIndicatorHandler() - { + if (seqs == null || !checkValidInputSeqs(seqs)) + { + jalview.bin.Console.debug( + "Sequences for analysis service were null or not valid"); + return; + } - @Override - public boolean cancelActivity(long id) - { - ((CancellableI) annotService).cancel(running); - return true; - } - - @Override - public boolean canCancel() - { - return cancellable; - } - }); - } - - // /// - // and poll for updates until job finishes, fails or becomes stale - - boolean finished = false; - do - { - Cache.log.debug("Updating status for annotation service."); - annotService.updateStatus(running); - job.updateJobPanelState(info, "", running); - if (running.isSubjobComplete()) - { - Cache.log.debug( - "Finished polling analysis service job: status reported is " - + running.getState()); - finished = true; - } - else - { - Cache.log.debug("Status now " + running.getState()); - } + if (guiProgress != null) + { + guiProgress.setProgressBar(service.getActionText(), + progressId = System.currentTimeMillis()); + } + jalview.bin.Console.debug("submitted " + seqs.size() + + " sequences to " + service.getActionText()); - if (calcMan.isPending(this) && isInteractiveUpdate()) - { - Cache.log.debug("Analysis service job is stale. aborting."); - // job has become stale. - if (!finished) { - finished = true; - // cancel this job and yield to the new job - try - { - if (cancellable - && ((CancellableI) annotService).cancel(running)) - { - System.err.println("Cancelled job: " + rslt); - } - else + rslt = annotService.submitToService(seqs, getPreset(), + getArguments()); + if (rslt == null) + { + return; + } + // TODO: handle job submission error reporting here. + Console.debug("Service " + service.getUri() + "\nSubmitted job ID: " + + rslt); + // /// + // otherwise, construct WsJob and any UI handlers + running = new AnnotationWsJob(); + running.setJobHandle(rslt); + running.setSeqNames(seqNames); + running.setStartPos(start); + running.setSeqs(seqs); + job.updateJobPanelState(info, "", running); + if (guiProgress != null) + { + guiProgress.registerHandler(progressId, + new IProgressIndicatorHandler() { - System.err.println("FAILED TO CANCEL job: " + rslt); - } - - } catch (Exception x) - { - - } - } - rslt = running.getJobHandle(); - return; - } - // pull any stats - some services need to flush log output before - // results are available - Cache.log.debug("Updating progress log for annotation service."); + @Override + public boolean cancelActivity(long id) + { + calcMan.cancelWorker(SeqAnnotationServiceCalcWorker.this); + return true; + } - try - { - annotService.updateJobProgress(running); - } catch (Throwable thr) - { - Cache.log.debug("Ignoring exception during progress update.", - thr); - } - Cache.log.trace("Result of poll: " + running.getStatus()); + @Override + public boolean canCancel() + { + return cancellable; + } + }); + } + } + + @Override public boolean poll() throws Throwable + { + boolean finished = false; + + Console.debug("Updating status for annotation service."); + annotService.updateStatus(running); + job.updateJobPanelState(info, "", running); + if (running.isSubjobComplete()) + { + Console.debug( + "Finished polling analysis service job: status reported is " + + running.getState()); + finished = true; + } + else + { + Console.debug("Status now " + running.getState()); + } - if (!finished && !running.isFailed()) - { - try - { - Cache.log.debug("Analysis service job thread sleeping."); - Thread.sleep(200); - Cache.log.debug("Analysis service job thread woke."); - } catch (InterruptedException x) - { - } - ; - } - } while (!finished); + // pull any stats - some services need to flush log output before + // results are available + Console.debug("Updating progress log for annotation service."); - Cache.log.debug("Job poll loop exited. Job is " + running.getState()); - // TODO: need to poll/retry - if (serverErrorsLeft > 0) - { - try - { - Thread.sleep(200); - } catch (InterruptedException x) - { - } - } + try + { + annotService.updateJobProgress(running); + } catch (Throwable thr) + { + Console.debug("Ignoring exception during progress update.", + thr); + } + Console.debug("Result of poll: " + running.getStatus()); + + + if (finished) + { + Console.debug("Job poll loop exited. Job is " + running.getState()); if (running.isFinished()) { // expect there to be results to collect @@ -406,16 +352,16 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker // features automatically according to user preferences running.setFeatureRenderer( ((jalview.gui.AlignmentPanel) ap).cloneFeatureRenderer()); - Cache.log.debug("retrieving job results."); + Console.debug("retrieving job results."); final Map featureColours = new HashMap<>(); final Map featureFilters = new HashMap<>(); List returnedAnnot = annotService .getAnnotationResult(running.getJobHandle(), seqs, featureColours, featureFilters); - Cache.log.debug("Obtained " + (returnedAnnot == null ? "no rows" + Console.debug("Obtained " + (returnedAnnot == null ? "no rows" : ("" + returnedAnnot.size()))); - Cache.log.debug("There were " + featureColours.size() + Console.debug("There were " + featureColours.size() + " feature colours and " + featureFilters.size() + " filters defined."); @@ -424,11 +370,29 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker // sequence, excluding regions not annotated due to gapMap/column // visibility + // update calcId if it is not already set on returned annotation + if (returnedAnnot != null) + { + for (AlignmentAnnotation aa : returnedAnnot) + { + // assume that any CalcIds already set + if (getCalcId() != null && aa.getCalcId() == null + || "".equals(aa.getCalcId())) + { + aa.setCalcId(getCalcId()); + } + // autocalculated annotation are created by interactive alignment + // analysis services + aa.autoCalculated = service.isAlignmentAnalysis() + && service.isInteractiveUpdate(); + } + } + running.setAnnotation(returnedAnnot); if (running.hasResults()) { - jalview.bin.Cache.log.debug("Updating result annotation from Job " + jalview.bin.Console.debug("Updating result annotation from Job " + rslt + " at " + service.getUri()); updateResultAnnotation(true); if (running.isTransferSequenceFeatures()) @@ -438,7 +402,7 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker // regions // not annotated due to gapMap/column visibility - jalview.bin.Cache.log.debug( + jalview.bin.Console.debug( "Updating feature display settings and transferring features from Job " + rslt + " at " + service.getUri()); // TODO: consider merge rather than apply here @@ -476,61 +440,29 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker ap.adjustAnnotationHeight(); } } - Cache.log.debug("Annotation Service Worker thread finished."); - } -// TODO: use service specitic exception handlers -// catch (JobSubmissionException x) -// { -// -// System.err.println( -// "submission error with " + getServiceActionText() + " :"); -// x.printStackTrace(); -// calcMan.disableWorker(this); -// } catch (ResultNotAvailableException x) -// { -// System.err.println("collection error:\nJob ID: " + rslt); -// x.printStackTrace(); -// calcMan.disableWorker(this); -// -// } catch (OutOfMemoryError error) -// { -// calcMan.disableWorker(this); -// -// ap.raiseOOMWarning(getServiceActionText(), error); -// } - catch (Throwable x) - { - calcMan.disableWorker(this); + Console.debug("Annotation Service Worker thread finished."); - System.err - .println("Blacklisting worker due to unexpected exception:"); - x.printStackTrace(); - } finally + } + + return finished; + } + + @Override public void cancel() + { + cancelCurrentJob(); + } + + @Override public void done() + { + if (ap != null) { - - calcMan.workerComplete(this); - if (ap != null) - { - if (guiProgress != null && progressId != -1) - { - guiProgress.setProgressBar("", progressId); - } - // TODO: may not need to paintAlignment again ! - ap.paintAlignment(false, false); - } - if (msg.length() > 0) + if (guiProgress != null && progressId != -1) { - // TODO: stash message somewhere in annotation or alignment view. - // code below shows result in a text box popup - /* - * jalview.gui.CutAndPasteTransfer cap = new - * jalview.gui.CutAndPasteTransfer(); cap.setText(msg.toString()); - * jalview.gui.Desktop.addInternalFrame(cap, - * "Job Status for "+getServiceActionText(), 600, 400); - */ + guiProgress.removeProgressBar(progressId); } + // TODO: may not need to paintAlignment again ! + ap.paintAlignment(false, false); } - } /** @@ -563,7 +495,7 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker try { String id = running.getJobId(); - if (((CancellableI) annotService).cancel(running)) + if (cancellable && ((CancellableI) annotService).cancel(running)) { System.err.println("Cancelled job " + id); } @@ -819,7 +751,7 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker for (SequenceI sq : running.getSeqs()) { if (!sq.getFeatures().hasFeatures() - && (sq.getDBRefs() == null || sq.getDBRefs().length == 0)) + && (sq.getDBRefs() == null || sq.getDBRefs().size() == 0)) { continue; } @@ -871,43 +803,6 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker } } - /** - * notify manager that we have started, and wait for a free calculation slot - * - * @return true if slot is obtained and work still valid, false if another - * thread has done our work for us. - */ - protected boolean checkDone() - { - calcMan.notifyStart(this); - ap.paintAlignment(false, false); - while (!calcMan.notifyWorking(this)) - { - if (calcMan.isWorking(this)) - { - return true; - } - try - { - if (ap != null) - { - ap.paintAlignment(false, false); - } - - Thread.sleep(200); - } catch (Exception ex) - { - ex.printStackTrace(); - } - } - if (alignViewport.isClosed()) - { - abortAndDestroy(); - return true; - } - return false; - } - protected void updateOurAnnots(List ourAnnot) { List our = ourAnnots;