4d3db586d49748151bb1a5dbe85074c940f6ea87
[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  * @author JimP
30  * 
31  */
32 public class JobStateSummary
33 {
34   /**
35    * number of jobs running
36    */
37   int running = 0;
38
39   /**
40    * number of jobs queued
41    */
42   int queuing = 0;
43
44   /**
45    * number of jobs finished
46    */
47   int finished = 0;
48
49   /**
50    * number of jobs failed
51    */
52   int error = 0;
53
54   /**
55    * number of jobs stopped due to server error
56    */
57   int serror = 0;
58
59   /**
60    * number of jobs cancelled
61    */
62   int cancelled = 0;
63
64   /**
65    * number of jobs finished with results
66    */
67   int results = 0;
68
69   /**
70    * processes an AWSJob's status and updates job status counters and WebService
71    * status displays
72    * 
73    * @param wsInfo
74    * @param OutputHeader
75    * @param j
76    */
77   public void updateJobPanelState(WebserviceInfo wsInfo,
78           String OutputHeader, AWsJob j)
79   {
80     if (j.cancelled)
81     {
82       cancelled++;
83       j.subjobComplete = true;
84       wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_CANCELLED_OK);
85       return;
86     }
87     if (j.submitted)
88     {
89       String progheader = "";
90       // Parse state of job[j]
91       if (j.isRunning())
92       {
93         running++;
94         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_RUNNING);
95       }
96       else if (j.isQueued())
97       {
98         queuing++;
99         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_QUEUING);
100       }
101       else if (j.isFinished())
102       {
103         finished++;
104         j.subjobComplete = true;
105         if (j.hasResults())
106         {
107           results++;
108         }
109         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_OK);
110       }
111       else if (j.isFailed())
112       {
113         progheader += "Job failed.\n";
114         j.subjobComplete = true;
115         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
116         error++;
117       }
118       else if (j.isServerError())
119       {
120         serror++;
121         j.subjobComplete = true;
122         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_SERVERERROR);
123       }
124       else if (j.isBroken())
125       {
126         progheader += "Job was broken.\n";
127         error++;
128         j.subjobComplete = true;
129         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
130       }
131       // and pass on any sub-job messages to the user
132       StringBuffer output = new StringBuffer();
133       if (OutputHeader != null)
134       {
135
136         output.append(OutputHeader);
137       }
138       if (progheader != null)
139       {
140         output.append(progheader);
141       }
142       if (j.hasStatus())
143       {
144         // Could try to squash OOMs here, but it usually doesn't help - there
145         // probably won't be
146         // enough memory to handle the results later on anyway.
147         // try {
148         String stat = j.getStatus();
149         if (stat != null)
150         {
151           output.append(stat);
152         }
153         // } catch (OutOfMemoryError e)
154         // {
155         // System.err.println("Out of memory when displaying status. Squashing error.");
156         // wsInfo.appendProgressText(j.jobnum,
157         // "..\n(Out of memory when displaying status)\n");
158         // }
159       }
160       wsInfo.setProgressText(j.jobnum, output.toString());
161     }
162     else
163     {
164       if (j.submitted && j.subjobComplete)
165       {
166         if (j.allowedServerExceptions == 0)
167         {
168           serror++;
169         }
170         else if (!j.hasResults())
171         {
172           error++;
173         }
174       }
175     }
176   }
177 }