always pass full web service exception details to the log
[jalview.git] / src / jalview / ws / AWSThread.java
1 package jalview.ws;
2
3 import jalview.bin.Cache;
4 import jalview.datamodel.AlignedCodonFrame;
5 import jalview.datamodel.Alignment;
6 import jalview.datamodel.AlignmentI;
7 import jalview.datamodel.AlignmentView;
8 import jalview.datamodel.SequenceI;
9 import jalview.gui.AlignFrame;
10 import jalview.gui.WebserviceInfo;
11 import jalview.gui.FeatureRenderer.FeatureRendererSettings;
12
13 public abstract class AWSThread extends Thread
14 {
15
16   /**
17    * view that this job was associated with
18    */
19   protected AlignmentI currentView = null;
20   /**
21    * feature settings from view that job was associated with
22    */
23   protected FeatureRendererSettings featureSettings = null;
24   /**
25    * metadata about this web service
26    */
27   protected WebserviceInfo wsInfo = null;
28   /**
29    * original input data for this job
30    */
31   protected AlignmentView input = null;
32   /**
33    * dataset sequence relationships to be propagated onto new results
34    */
35   protected AlignedCodonFrame[] codonframe = null;
36   /**
37    * are there jobs still running in this thread.
38    */
39   protected boolean jobComplete = false;
40   /**
41    * one or more jobs being managed by this thread.
42    */
43   protected AWsJob jobs[] = null;
44   /**
45    * full name of service
46    */
47   protected String WebServiceName = null;
48   protected char defGapChar = '-';
49   /**
50    * header prepended to all output from job
51    */
52   protected String OutputHeader;
53
54   /**
55    * only used when reporting a web service out of memory error - the job ID will be concatenated to the URL
56    */
57   protected String WsUrl = null;
58
59   /**
60    * generic web service job/subjob poll loop 
61    */
62   public void run()
63   {
64     JobStateSummary jstate = null;
65     if (jobs == null)
66     {
67       jobComplete = true;
68     }
69     while (!jobComplete)
70     {
71       jstate = new JobStateSummary();
72       for (int j = 0; j < jobs.length; j++)
73       {
74
75         if (!jobs[j].submitted && jobs[j].hasValidInput())
76         {
77           StartJob(jobs[j]);
78         }
79
80         if (jobs[j].submitted && !jobs[j].subjobComplete)
81         {
82           try
83           {
84             pollJob(jobs[j]);
85             if (!jobs[j].hasResponse())
86             {
87               throw (new Exception(
88                       "Timed out when communicating with server\nTry again later.\n"));
89             }
90             jalview.bin.Cache.log.debug("Job " + j + " Result state "
91                     + jobs[j].getState() + "(ServerError="
92                     + jobs[j].isServerError() + ")");
93           } catch (Exception ex)
94           {
95             // Deal with Transaction exceptions
96             wsInfo.appendProgressText(jobs[j].jobnum, "\n" + WebServiceName
97                     + " Server exception!\n" + ex.getMessage());
98             // always output the exception's stack trace to the log
99             Cache.log.warn(WebServiceName + " job(" + jobs[j].jobnum
100                     + ") Server exception.");
101             ex.printStackTrace();
102
103             if (jobs[j].allowedServerExceptions > 0)
104             {
105               jobs[j].allowedServerExceptions--;
106               Cache.log.debug("Sleeping after a server exception.");
107               try
108               {
109                 Thread.sleep(5000);
110               } catch (InterruptedException ex1)
111               {
112               }
113             }
114             else
115             {
116               Cache.log.warn("Dropping job " + j + " " + jobs[j].jobId);
117               jobs[j].subjobComplete = true;
118               wsInfo.setStatus(jobs[j].jobnum,
119                       WebserviceInfo.STATE_STOPPED_SERVERERROR);
120             }
121           } catch (OutOfMemoryError er)
122           {
123             jobComplete = true;
124             jobs[j].subjobComplete = true;
125             jobs[j].clearResponse(); // may contain out of date result data
126             wsInfo.setStatus(jobs[j].jobnum,
127                     WebserviceInfo.STATE_STOPPED_ERROR);
128             Cache.log.error("Out of memory when retrieving Job " + j
129                     + " id:" + WsUrl + "/" + jobs[j].jobId, er);
130             new jalview.gui.OOMWarning("retrieving result for "
131                     + WebServiceName, er);
132             System.gc();
133           }
134         }
135         jstate.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
136       }
137       // Decide on overall state based on collected jobs[] states
138       if (jstate.running > 0)
139       {
140         wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
141       }
142       else if (jstate.queuing > 0)
143       {
144         wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
145       }
146       else
147       {
148         jobComplete = true;
149         if (jstate.finished > 0)
150         {
151           wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
152         }
153         else if (jstate.error > 0)
154         {
155           wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
156         }
157         else if (jstate.serror > 0)
158         {
159           wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
160         }
161       }
162       if (!jobComplete)
163       {
164         try
165         {
166           Thread.sleep(5000);
167         } catch (InterruptedException e)
168         {
169           Cache.log
170                   .debug("Interrupted sleep waiting for next job poll.", e);
171         }
172         // System.out.println("I'm alive "+alTitle);
173       }
174     }
175     if (jobComplete && jobs != null)
176     {
177       parseResult(); // tidy up and make results available to user
178     }
179     else
180     {
181       Cache.log
182               .debug("WebServiceJob poll loop finished with no jobs created.");
183       wsInfo.setFinishedNoResults();
184     }
185   }
186
187
188   public AWSThread()
189   {
190     super();
191   }
192
193   public AWSThread(Runnable target)
194   {
195     super(target);
196   }
197
198   public AWSThread(String name)
199   {
200     super(name);
201   }
202
203   public AWSThread(ThreadGroup group, Runnable target)
204   {
205     super(group, target);
206   }
207
208   public AWSThread(ThreadGroup group, String name)
209   {
210     super(group, name);
211   }
212
213   public AWSThread(Runnable target, String name)
214   {
215     super(target, name);
216   }
217
218   public AWSThread(ThreadGroup group, Runnable target, String name)
219   {
220     super(group, target, name);
221   }
222
223   /**
224    * query web service for status of job. on return, job.result must not be null
225    * - if it is then it will be assumed that the job status query timed out and
226    * a server exception will be logged.
227    * 
228    * @param job
229    * @throws Exception
230    *           will be logged as a server exception for this job
231    */
232   public abstract void pollJob(AWsJob job) throws Exception;
233
234   /**
235    * submit job to web service
236    * 
237    * @param job
238    */
239   public abstract void StartJob(AWsJob job);
240
241   /**
242    * process the set of AWsJob objects into a set of results, and tidy up.
243    */
244   public abstract void parseResult();
245
246   /**
247    * helper function to conserve dataset references to sequence objects returned
248    * from web services 1. Propagates AlCodonFrame data from
249    * <code>codonframe</code> to <code>al</code>
250    * TODO: refactor to datamodel
251    * @param al
252    */
253   public void propagateDatasetMappings(Alignment al)
254   {
255     if (codonframe != null)
256     {
257       SequenceI[] alignment = al.getSequencesArray();
258       for (int sq = 0; sq < alignment.length; sq++)
259       {
260         for (int i = 0; i < codonframe.length; i++)
261         {
262           if (codonframe[i] != null
263                   && codonframe[i].involvesSequence(alignment[sq]))
264           {
265             al.addCodonFrame(codonframe[i]);
266             codonframe[i] = null;
267             break;
268           }
269         }
270       }
271     }
272   }
273
274   public AWSThread(ThreadGroup group, Runnable target, String name,
275           long stackSize)
276   {
277     super(group, target, name, stackSize);
278   }
279
280   /**
281    * 
282    * @return gap character to use for any alignment generation
283    */
284   public char getGapChar()
285   {
286     return defGapChar;
287   }
288
289   /**
290    * 
291    * @param alignFrame
292    *          reference for copying mappings across
293    * @param wsInfo
294    *          gui attachment point
295    * @param input
296    *          input data for the calculation
297    * @param webServiceName
298    *          name of service
299    * @param wsUrl
300    *          url of the service being invoked
301    */
302   public AWSThread(AlignFrame alignFrame, WebserviceInfo wsinfo,
303           AlignmentView input, String webServiceName, String wsUrl)
304   {
305     this(alignFrame, wsinfo, input, wsUrl);
306     WebServiceName = webServiceName;
307   }
308
309   /**
310    * 
311    * @param alframe
312    *          - reference for copying mappings and display styles across
313    * @param wsinfo2
314    *          - gui attachment point
315    * @param alview
316    *          - input data for the calculation
317    * @param wsurl2
318    *          - url of the service being invoked
319    */
320   public AWSThread(AlignFrame alframe, WebserviceInfo wsinfo2,
321           AlignmentView alview, String wsurl2)
322   {
323     super();
324     // this.alignFrame = alframe;
325     currentView = alframe.getCurrentView().getAlignment();
326     featureSettings = alframe.getFeatureRenderer().getSettings();
327     defGapChar = alframe.getViewport().getGapCharacter();
328     this.wsInfo = wsinfo2;
329     this.input = alview;
330     WsUrl = wsurl2;
331     if (alframe != null)
332     {
333       AlignedCodonFrame[] cf = alframe.getViewport().getAlignment()
334               .getCodonFrames();
335       if (cf != null)
336       {
337         codonframe = new AlignedCodonFrame[cf.length];
338         System.arraycopy(cf, 0, codonframe, 0, cf.length);
339       }
340     }
341   }
342 }