JAL-1432 updated copyright notices
[jalview.git] / src / jalview / ws / JobStateSummary.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.ws;
20
21 import jalview.gui.WebserviceInfo;
22
23 /**
24  * bookkeeper class for the WebServiceInfo GUI, maintaining records of web
25  * service jobs handled by the window and reflecting any status updates.
26  * 
27  * @author JimP
28  * 
29  */
30 public class JobStateSummary
31 {
32   /**
33    * number of jobs running
34    */
35   int running = 0;
36
37   /**
38    * number of jobs queued
39    */
40   int queuing = 0;
41
42   /**
43    * number of jobs finished
44    */
45   int finished = 0;
46
47   /**
48    * number of jobs failed
49    */
50   int error = 0;
51
52   /**
53    * number of jobs stopped due to server error
54    */
55   int serror = 0;
56
57   /**
58    * number of jobs cancelled
59    */
60   int cancelled = 0;
61
62   /**
63    * number of jobs finished with results
64    */
65   int results = 0;
66
67   /**
68    * processes an AWSJob's status and updates job status counters and WebService
69    * status displays
70    * 
71    * @param wsInfo
72    * @param OutputHeader
73    * @param j
74    */
75   public void updateJobPanelState(WebserviceInfo wsInfo,
76           String OutputHeader, AWsJob j)
77   {
78     if (j.submitted)
79     {
80       String progheader = "";
81       // Parse state of job[j]
82       if (j.isRunning())
83       {
84         running++;
85         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_RUNNING);
86       }
87       else if (j.isQueued())
88       {
89         queuing++;
90         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_QUEUING);
91       }
92       else if (j.isFinished())
93       {
94         finished++;
95         j.subjobComplete = true;
96         if (j.hasResults())
97         {
98           results++;
99         }
100         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_OK);
101       }
102       else if (j.isFailed())
103       {
104         progheader += "Job failed.\n";
105         j.subjobComplete = true;
106         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
107         error++;
108       }
109       else if (j.isServerError())
110       {
111         serror++;
112         j.subjobComplete = true;
113         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_SERVERERROR);
114       }
115       else if (j.isBroken())
116       {
117         progheader += "Job was broken.\n";
118         error++;
119         j.subjobComplete = true;
120         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
121       }
122       // and pass on any sub-job messages to the user
123       StringBuffer output = new StringBuffer();
124       if (OutputHeader != null)
125       {
126
127         output.append(OutputHeader);
128       }
129       if (progheader != null)
130       {
131         output.append(progheader);
132       }
133       if (j.hasStatus())
134       {
135         // Could try to squash OOMs here, but it usually doesn't help - there
136         // probably won't be
137         // enough memory to handle the results later on anyway.
138         // try {
139         String stat = j.getStatus();
140         if (stat != null)
141         {
142           output.append(stat);
143         }
144         // } catch (OutOfMemoryError e)
145         // {
146         // System.err.println("Out of memory when displaying status. Squashing error.");
147         // wsInfo.appendProgressText(j.jobnum,
148         // "..\n(Out of memory when displaying status)\n");
149         // }
150       }
151       wsInfo.setProgressText(j.jobnum, output.toString());
152     }
153     else
154     {
155       if (j.submitted && j.subjobComplete)
156       {
157         if (j.allowedServerExceptions == 0)
158         {
159           serror++;
160         }
161         else if (!j.hasResults())
162         {
163           error++;
164         }
165       }
166     }
167   }
168 }