JAL-3070 TODO
[jalview.git] / src / jalview / ws / JobStateSummary.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.ws;
22
23 import jalview.gui.WebserviceInfo;
24
25 /**
26  * bookkeeper class for the WebServiceInfo GUI, maintaining records of web
27  * service jobs handled by the window and reflecting any status updates.
28  * 
29  * TODO: separate from the GUI cleanly since it also holds logic for
30  * interpreting failure states
31  * 
32  * @author JimP
33  * 
34  */
35 public class JobStateSummary
36 {
37   /**
38    * number of jobs running
39    */
40   int running = 0;
41
42   /**
43    * number of jobs queued
44    */
45   int queuing = 0;
46
47   /**
48    * number of jobs finished
49    */
50   int finished = 0;
51
52   /**
53    * number of jobs failed
54    */
55   int error = 0;
56
57   /**
58    * number of jobs stopped due to server error
59    */
60   int serror = 0;
61
62   /**
63    * number of jobs cancelled
64    */
65   int cancelled = 0;
66
67   /**
68    * number of jobs finished with results
69    */
70   int results = 0;
71
72   /**
73    * processes an AWSJob's status and updates job status counters and WebService
74    * status displays
75    * 
76    * @param wsInfo
77    * @param OutputHeader
78    * @param j
79    */
80   public void updateJobPanelState(WebserviceInfo wsInfo,
81           String OutputHeader, AWsJob j)
82   {
83     if (j.cancelled)
84     {
85       cancelled++;
86       j.subjobComplete = true;
87       wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_CANCELLED_OK);
88       return;
89     }
90     if (j.submitted)
91     {
92       String progheader = "";
93       // Parse state of job[j]
94       if (j.isRunning())
95       {
96         running++;
97         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_RUNNING);
98       }
99       else if (j.isQueued())
100       {
101         queuing++;
102         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_QUEUING);
103       }
104       else if (j.isFinished())
105       {
106         finished++;
107         j.subjobComplete = true;
108         if (j.hasResults())
109         {
110           results++;
111         }
112         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_OK);
113       }
114       else if (j.isFailed())
115       {
116         progheader += "Job failed.\n";
117         j.subjobComplete = true;
118         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
119         error++;
120       }
121       else if (j.isServerError())
122       {
123         serror++;
124         j.subjobComplete = true;
125         wsInfo.setStatus(j.jobnum,
126                 WebserviceInfo.STATE_STOPPED_SERVERERROR);
127       }
128       else if (j.isBroken())
129       {
130         progheader += "Job was broken.\n";
131         error++;
132         j.subjobComplete = true;
133         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
134       }
135       // and pass on any sub-job messages to the user
136       StringBuffer output = new StringBuffer();
137       if (OutputHeader != null)
138       {
139
140         output.append(OutputHeader);
141       }
142       if (progheader != null)
143       {
144         output.append(progheader);
145       }
146       if (j.hasStatus())
147       {
148         // Could try to squash OOMs here, but it usually doesn't help - there
149         // probably won't be
150         // enough memory to handle the results later on anyway.
151         // try {
152         String stat = j.getStatus();
153         if (stat != null)
154         {
155           output.append(stat);
156         }
157         // } catch (OutOfMemoryError e)
158         // {
159         // System.err.println("Out of memory when displaying status. Squashing
160         // error.");
161         // wsInfo.appendProgressText(j.jobnum,
162         // "..\n(Out of memory when displaying status)\n");
163         // }
164       }
165       wsInfo.setProgressText(j.jobnum, output.toString());
166     }
167     else
168     {
169       if (j.submitted && j.subjobComplete)
170       {
171         if (j.allowedServerExceptions == 0)
172         {
173           serror++;
174         }
175         else if (!j.hasResults())
176         {
177           error++;
178         }
179       }
180     }
181   }
182 }