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.",ex);
101
102             if (jobs[j].allowedServerExceptions > 0)
103             {
104               jobs[j].allowedServerExceptions--;
105               Cache.log.debug("Sleeping after a server exception.");
106               try
107               {
108                 Thread.sleep(5000);
109               } catch (InterruptedException ex1)
110               {
111               }
112             }
113             else
114             {
115               Cache.log.warn("Dropping job " + j + " " + jobs[j].jobId);
116               jobs[j].subjobComplete = true;
117               wsInfo.setStatus(jobs[j].jobnum,
118                       WebserviceInfo.STATE_STOPPED_SERVERERROR);
119             }
120           } catch (OutOfMemoryError er)
121           {
122             jobComplete = true;
123             jobs[j].subjobComplete = true;
124             jobs[j].clearResponse(); // may contain out of date result data
125             wsInfo.setStatus(jobs[j].jobnum,
126                     WebserviceInfo.STATE_STOPPED_ERROR);
127             Cache.log.error("Out of memory when retrieving Job " + j
128                     + " id:" + WsUrl + "/" + jobs[j].jobId, er);
129             new jalview.gui.OOMWarning("retrieving result for "
130                     + WebServiceName, er);
131             System.gc();
132           }
133         }
134         jstate.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
135       }
136       // Decide on overall state based on collected jobs[] states
137       if (jstate.running > 0)
138       {
139         wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
140       }
141       else if (jstate.queuing > 0)
142       {
143         wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
144       }
145       else
146       {
147         jobComplete = true;
148         if (jstate.finished > 0)
149         {
150           wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
151         }
152         else if (jstate.error > 0)
153         {
154           wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
155         }
156         else if (jstate.serror > 0)
157         {
158           wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
159         }
160       }
161       if (!jobComplete)
162       {
163         try
164         {
165           Thread.sleep(5000);
166         } catch (InterruptedException e)
167         {
168           Cache.log
169                   .debug("Interrupted sleep waiting for next job poll.", e);
170         }
171         // System.out.println("I'm alive "+alTitle);
172       }
173     }
174     if (jobComplete && jobs != null)
175     {
176       parseResult(); // tidy up and make results available to user
177     }
178     else
179     {
180       Cache.log
181               .debug("WebServiceJob poll loop finished with no jobs created.");
182       wsInfo.setFinishedNoResults();
183     }
184   }
185
186
187   public AWSThread()
188   {
189     super();
190   }
191
192   public AWSThread(Runnable target)
193   {
194     super(target);
195   }
196
197   public AWSThread(String name)
198   {
199     super(name);
200   }
201
202   public AWSThread(ThreadGroup group, Runnable target)
203   {
204     super(group, target);
205   }
206
207   public AWSThread(ThreadGroup group, String name)
208   {
209     super(group, name);
210   }
211
212   public AWSThread(Runnable target, String name)
213   {
214     super(target, name);
215   }
216
217   public AWSThread(ThreadGroup group, Runnable target, String name)
218   {
219     super(group, target, name);
220   }
221
222   /**
223    * query web service for status of job. on return, job.result must not be null
224    * - if it is then it will be assumed that the job status query timed out and
225    * a server exception will be logged.
226    * 
227    * @param job
228    * @throws Exception
229    *           will be logged as a server exception for this job
230    */
231   public abstract void pollJob(AWsJob job) throws Exception;
232
233   /**
234    * submit job to web service
235    * 
236    * @param job
237    */
238   public abstract void StartJob(AWsJob job);
239
240   /**
241    * process the set of AWsJob objects into a set of results, and tidy up.
242    */
243   public abstract void parseResult();
244
245   /**
246    * helper function to conserve dataset references to sequence objects returned
247    * from web services 1. Propagates AlCodonFrame data from
248    * <code>codonframe</code> to <code>al</code>
249    * TODO: refactor to datamodel
250    * @param al
251    */
252   public void propagateDatasetMappings(Alignment al)
253   {
254     if (codonframe != null)
255     {
256       SequenceI[] alignment = al.getSequencesArray();
257       for (int sq = 0; sq < alignment.length; sq++)
258       {
259         for (int i = 0; i < codonframe.length; i++)
260         {
261           if (codonframe[i] != null
262                   && codonframe[i].involvesSequence(alignment[sq]))
263           {
264             al.addCodonFrame(codonframe[i]);
265             codonframe[i] = null;
266             break;
267           }
268         }
269       }
270     }
271   }
272
273   public AWSThread(ThreadGroup group, Runnable target, String name,
274           long stackSize)
275   {
276     super(group, target, name, stackSize);
277   }
278
279   /**
280    * 
281    * @return gap character to use for any alignment generation
282    */
283   public char getGapChar()
284   {
285     return defGapChar;
286   }
287
288   /**
289    * 
290    * @param alignFrame
291    *          reference for copying mappings across
292    * @param wsInfo
293    *          gui attachment point
294    * @param input
295    *          input data for the calculation
296    * @param webServiceName
297    *          name of service
298    * @param wsUrl
299    *          url of the service being invoked
300    */
301   public AWSThread(AlignFrame alignFrame, WebserviceInfo wsinfo,
302           AlignmentView input, String webServiceName, String wsUrl)
303   {
304     this(alignFrame, wsinfo, input, wsUrl);
305     WebServiceName = webServiceName;
306   }
307
308   /**
309    * 
310    * @param alframe
311    *          - reference for copying mappings and display styles across
312    * @param wsinfo2
313    *          - gui attachment point
314    * @param alview
315    *          - input data for the calculation
316    * @param wsurl2
317    *          - url of the service being invoked
318    */
319   public AWSThread(AlignFrame alframe, WebserviceInfo wsinfo2,
320           AlignmentView alview, String wsurl2)
321   {
322     super();
323     // this.alignFrame = alframe;
324     currentView = alframe.getCurrentView().getAlignment();
325     featureSettings = alframe.getFeatureRenderer().getSettings();
326     defGapChar = alframe.getViewport().getGapCharacter();
327     this.wsInfo = wsinfo2;
328     this.input = alview;
329     WsUrl = wsurl2;
330     if (alframe != null)
331     {
332       AlignedCodonFrame[] cf = alframe.getViewport().getAlignment()
333               .getCodonFrames();
334       if (cf != null)
335       {
336         codonframe = new AlignedCodonFrame[cf.length];
337         System.arraycopy(cf, 0, codonframe, 0, cf.length);
338       }
339     }
340   }
341 }