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