62f172d749a8a2092252341f172b676e5e410ef0
[jalview.git] / src / jalview / ws / JobStateSummary.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3  * Copyright (C) 2010 J Procter, AM Waterhouse, 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       wsInfo.setProgressText(j.jobnum, OutputHeader);
123       wsInfo.appendProgressText(j.jobnum, progheader);
124       if (j.hasStatus())
125       {
126         // Could try to squash OOMs here, but it usually doesn't help - there
127         // probably won't be
128         // enough memory to handle the results later on anyway.
129         // try {
130         wsInfo.appendProgressText(j.jobnum, j.getStatus());
131         // } catch (OutOfMemoryError e)
132         // {
133         // System.err.println("Out of memory when displaying status. Squashing error.");
134         // wsInfo.appendProgressText(j.jobnum,
135         // "..\n(Out of memory when displaying status)\n");
136         // }
137       }
138     }
139     else
140     {
141       if (j.submitted && j.subjobComplete)
142       {
143         if (j.allowedServerExceptions == 0)
144         {
145           serror++;
146         }
147         else if (!j.hasResults())
148         {
149           error++;
150         }
151       }
152     }
153   }
154 }