2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.bin.Cache;
24 import jalview.datamodel.AlignedCodonFrame;
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.AlignmentView;
28 import jalview.datamodel.SequenceI;
29 import jalview.gui.AlignFrame;
30 import jalview.gui.WebserviceInfo;
31 import jalview.util.MessageManager;
32 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Timer;
37 import java.util.TimerTask;
39 import static java.lang.String.format;
41 public abstract class AWSThread
44 private final Timer timer = new Timer();
47 * view that this job was associated with
49 protected AlignmentI currentView = null;
52 * feature settings from view that job was associated with
54 protected FeatureRendererSettings featureSettings = null;
57 * metadata about this web service
59 protected WebserviceInfo wsInfo = null;
62 * original input data for this job
64 protected AlignmentView input = null;
67 * dataset sequence relationships to be propagated onto new results
69 protected List<AlignedCodonFrame> codonframe = null;
72 * are there jobs still running in this thread.
74 * fixme: initialize with an empty array?
76 protected boolean jobComplete = false;
79 * one or more jobs being managed by this thread.
81 protected AWsJob[] jobs = null;
84 * full name of service
86 protected String WebServiceName = null;
88 protected char defGapChar = '-';
91 * header prepended to all output from job
93 protected String OutputHeader;
96 * only used when reporting a web service out of memory error - the job ID
97 * will be concatenated to the URL
99 protected String WsUrl = null;
102 * The AlignFrame from which the service was requested.
104 private AlignFrame alignFrame;
112 "WebServiceJob poll loop finished with no jobs created.");
113 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
114 wsInfo.appendProgressText(
115 MessageManager.getString("info.no_jobs_ran"));
116 wsInfo.setFinishedNoResults();
119 TimerTask task = new TimerTask() {
123 JobStateSummary jstate = new JobStateSummary();
124 for (final AWsJob job : jobs)
126 if (!job.submitted && job.hasValidInput())
130 Cache.log.debug(format(
131 "Job %s is %ssubmitted", job, job.submitted ? "" : "not "));
132 if (job.submitted && !job.subjobComplete)
134 Cache.log.debug(format(
135 "Polling Job %s Result state was:%s(ServerError=%b)",
136 job, job.getState(), job.isServerError()));
140 if (!job.hasResponse())
141 throw new Exception("Timed out when communicating with server. Try again later.");
143 Cache.log.debug(format("Job %s Result state:%s(ServerError=%b)",
144 job, job.getState(), job.isServerError()));
145 } catch (Exception exc)
147 // Deal with Transaction exceptions
148 wsInfo.appendProgressText(job.jobnum, MessageManager
149 .formatMessage("info.server_exception", WebServiceName,
151 // always output the exception's stack trace to the log
152 Cache.log.warn(format("%s job(%s) Server exception.",
153 WebServiceName, job.jobnum));
154 exc.printStackTrace();
156 if (job.allowedServerExceptions > 0)
158 job.allowedServerExceptions--;
162 Cache.log.warn(format("Dropping job %s %s", job, job.jobId));
163 job.subjobComplete = true;
164 wsInfo.setStatus(job.jobnum, WebserviceInfo.STATE_STOPPED_SERVERERROR);
166 } catch (OutOfMemoryError oomerror)
169 job.subjobComplete = true;
171 wsInfo.setStatus(job.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
172 Cache.log.error(format("Out of memory when retrieving Job %s id:%s/%s",
173 job, WsUrl, job.jobId), oomerror);
174 new jalview.gui.OOMWarning("retrieving result for " + WebServiceName, oomerror);
178 jstate.updateJobPanelState(wsInfo, OutputHeader, job);
180 // Decide on overall state based on collected jobs[] states
181 updateGlobalStatus(jstate);
184 // jobs should never be null at this point
185 parseResult(); // tidy up and make results available to user
190 timer.schedule(task, 0, 5000);
193 protected void updateGlobalStatus(JobStateSummary jstate)
195 if (jstate.running > 0)
197 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
199 else if (jstate.queuing > 0)
201 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
206 if (jstate.finished > 0)
208 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
210 else if (jstate.error > 0)
212 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
214 else if (jstate.serror > 0)
216 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
221 public void interrupt()
227 * query web service for status of job. on return, job.result must not be null
228 * - if it is then it will be assumed that the job status query timed out and
229 * a server exception will be logged.
233 * will be logged as a server exception for this job
235 public abstract void pollJob(AWsJob job) throws Exception;
238 * submit job to web service
242 public abstract void StartJob(AWsJob job);
245 * process the set of AWsJob objects into a set of results, and tidy up.
247 public abstract void parseResult();
250 * helper function to conserve dataset references to sequence objects returned
251 * from web services 1. Propagates AlCodonFrame data from
252 * <code>codonframe</code> to <code>al</code> TODO: refactor to datamodel
256 public void propagateDatasetMappings(Alignment al)
258 if (codonframe != null)
260 SequenceI[] alignment = al.getSequencesArray();
261 for (int sq = 0; sq < alignment.length; sq++)
263 for (AlignedCodonFrame acf : codonframe)
265 final SequenceI seq = alignment[sq];
266 if (acf != null && acf.involvesSequence(seq))
268 al.addCodonFrame(acf);
278 * @return gap character to use for any alignment generation
280 public char getGapChar()
288 * reference for copying mappings across
290 * gui attachment point
292 * input data for the calculation
293 * @param webServiceName
296 * url of the service being invoked
298 public AWSThread(AlignFrame alignFrame, WebserviceInfo wsinfo,
299 AlignmentView input, String webServiceName, String wsUrl)
301 this(alignFrame, wsinfo, input, wsUrl);
302 WebServiceName = webServiceName;
306 * Extracts additional info from alignment view's context.
309 * - reference for copying mappings and display styles across
311 * - gui attachment point - may be null
313 * - input data for the calculation
315 * - url of the service being invoked
317 public AWSThread(AlignFrame alframe, WebserviceInfo wsinfo2,
318 AlignmentView alview, String wsurl2)
321 this.alignFrame = alframe;
322 currentView = alframe.getCurrentView().getAlignment();
323 featureSettings = alframe.getFeatureRenderer().getSettings();
324 defGapChar = alframe.getViewport().getGapCharacter();
325 this.wsInfo = wsinfo2;
330 List<AlignedCodonFrame> cf = alframe.getViewport().getAlignment()
334 codonframe = new ArrayList<>();
335 codonframe.addAll(cf);
340 protected AlignFrame getRequestingAlignFrame()
342 return this.alignFrame;