JAL-1503 update version in GPL header
[jalview.git] / src / jalview / ws / JobStateSummary.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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.cancelled)
79     {
80         cancelled++;
81         j.subjobComplete=true;
82         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_CANCELLED_OK);
83         return;
84     } 
85     if (j.submitted)
86     {
87       String progheader = "";
88       // Parse state of job[j]
89       if (j.isRunning())
90       {
91         running++;
92         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_RUNNING);
93       }
94       else if (j.isQueued())
95       {
96         queuing++;
97         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_QUEUING);
98       }
99       else if (j.isFinished())
100       {
101         finished++;
102         j.subjobComplete = true;
103         if (j.hasResults())
104         {
105           results++;
106         }
107         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_OK);
108       }
109       else if (j.isFailed())
110       {
111         progheader += "Job failed.\n";
112         j.subjobComplete = true;
113         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
114         error++;
115       }
116       else if (j.isServerError())
117       {
118         serror++;
119         j.subjobComplete = true;
120         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_SERVERERROR);
121       }
122       else if (j.isBroken())
123       {
124         progheader += "Job was broken.\n";
125         error++;
126         j.subjobComplete = true;
127         wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
128       }
129       // and pass on any sub-job messages to the user
130       StringBuffer output = new StringBuffer();
131       if (OutputHeader != null)
132       {
133
134         output.append(OutputHeader);
135       }
136       if (progheader != null)
137       {
138         output.append(progheader);
139       }
140       if (j.hasStatus())
141       {
142         // Could try to squash OOMs here, but it usually doesn't help - there
143         // probably won't be
144         // enough memory to handle the results later on anyway.
145         // try {
146         String stat = j.getStatus();
147         if (stat != null)
148         {
149           output.append(stat);
150         }
151         // } catch (OutOfMemoryError e)
152         // {
153         // System.err.println("Out of memory when displaying status. Squashing error.");
154         // wsInfo.appendProgressText(j.jobnum,
155         // "..\n(Out of memory when displaying status)\n");
156         // }
157       }
158       wsInfo.setProgressText(j.jobnum, output.toString());
159     }
160     else
161     {
162       if (j.submitted && j.subjobComplete)
163       {
164         if (j.allowedServerExceptions == 0)
165         {
166           serror++;
167         }
168         else if (!j.hasResults())
169         {
170           error++;
171         }
172       }
173     }
174   }
175 }