2632 |
08 Sep 06 |
nicklas |
1 |
/** |
2632 |
08 Sep 06 |
nicklas |
$Id$ |
2632 |
08 Sep 06 |
nicklas |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2006 Nicklas Nordborg |
2632 |
08 Sep 06 |
nicklas |
5 |
|
2632 |
08 Sep 06 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
2632 |
08 Sep 06 |
nicklas |
Available at http://base.thep.lu.se/ |
2632 |
08 Sep 06 |
nicklas |
8 |
|
2632 |
08 Sep 06 |
nicklas |
BASE is free software; you can redistribute it and/or |
2632 |
08 Sep 06 |
nicklas |
modify it under the terms of the GNU General Public License |
4479 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
2632 |
08 Sep 06 |
nicklas |
of the License, or (at your option) any later version. |
2632 |
08 Sep 06 |
nicklas |
13 |
|
2632 |
08 Sep 06 |
nicklas |
BASE is distributed in the hope that it will be useful, |
2632 |
08 Sep 06 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
2632 |
08 Sep 06 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2632 |
08 Sep 06 |
nicklas |
GNU General Public License for more details. |
2632 |
08 Sep 06 |
nicklas |
18 |
|
2632 |
08 Sep 06 |
nicklas |
You should have received a copy of the GNU General Public License |
4515 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
2632 |
08 Sep 06 |
nicklas |
21 |
*/ |
2632 |
08 Sep 06 |
nicklas |
22 |
package net.sf.basedb.util.jobagent; |
2632 |
08 Sep 06 |
nicklas |
23 |
|
2632 |
08 Sep 06 |
nicklas |
24 |
import java.util.HashSet; |
2634 |
12 Sep 06 |
nicklas |
25 |
import java.util.List; |
2634 |
12 Sep 06 |
nicklas |
26 |
import java.util.Map; |
2632 |
08 Sep 06 |
nicklas |
27 |
import java.util.Set; |
2632 |
08 Sep 06 |
nicklas |
28 |
|
2634 |
12 Sep 06 |
nicklas |
29 |
import net.sf.basedb.util.Values; |
2634 |
12 Sep 06 |
nicklas |
30 |
|
2632 |
08 Sep 06 |
nicklas |
31 |
/** |
2634 |
12 Sep 06 |
nicklas |
This class can hold information about the status of a job agent. |
2632 |
08 Sep 06 |
nicklas |
33 |
|
2634 |
12 Sep 06 |
nicklas |
@author nicklas |
2634 |
12 Sep 06 |
nicklas |
@version 2.0 |
2981 |
30 Nov 06 |
nicklas |
@see JobAgentConnection#getInfo(boolean) |
2634 |
12 Sep 06 |
nicklas |
@base.modified $Date$ |
2634 |
12 Sep 06 |
nicklas |
38 |
*/ |
2632 |
08 Sep 06 |
nicklas |
39 |
public class JobAgentInfo |
2632 |
08 Sep 06 |
nicklas |
40 |
{ |
2632 |
08 Sep 06 |
nicklas |
41 |
|
2632 |
08 Sep 06 |
nicklas |
42 |
private final long created; |
2634 |
12 Sep 06 |
nicklas |
43 |
private final Boolean paused; |
2632 |
08 Sep 06 |
nicklas |
44 |
private final Integer cpuUsage; |
2632 |
08 Sep 06 |
nicklas |
45 |
private final Long usedMemory; |
2632 |
08 Sep 06 |
nicklas |
46 |
private final Long totalMemory; |
4070 |
18 Dec 07 |
nicklas |
47 |
private final Set<JobInfo> jobs; |
2632 |
08 Sep 06 |
nicklas |
48 |
|
2634 |
12 Sep 06 |
nicklas |
49 |
/** |
2634 |
12 Sep 06 |
nicklas |
Create a new info object and set all values to null. |
2634 |
12 Sep 06 |
nicklas |
51 |
*/ |
2632 |
08 Sep 06 |
nicklas |
52 |
public JobAgentInfo() |
2632 |
08 Sep 06 |
nicklas |
53 |
{ |
2632 |
08 Sep 06 |
nicklas |
54 |
this.created = System.currentTimeMillis(); |
2632 |
08 Sep 06 |
nicklas |
55 |
this.jobs = null; |
2632 |
08 Sep 06 |
nicklas |
56 |
this.cpuUsage = null; |
2632 |
08 Sep 06 |
nicklas |
57 |
this.totalMemory = null; |
2632 |
08 Sep 06 |
nicklas |
58 |
this.usedMemory = null; |
2634 |
12 Sep 06 |
nicklas |
59 |
this.paused = null; |
2632 |
08 Sep 06 |
nicklas |
60 |
} |
2632 |
08 Sep 06 |
nicklas |
61 |
|
2634 |
12 Sep 06 |
nicklas |
62 |
/** |
2634 |
12 Sep 06 |
nicklas |
Create a new info object. |
2634 |
12 Sep 06 |
nicklas |
@param paused If the job agent is paused or running, or null if not know |
2634 |
12 Sep 06 |
nicklas |
@param cpuUsage The CPU usage in percent, or null if not known |
2634 |
12 Sep 06 |
nicklas |
@param usedMemory The used memory in bytes, or null if not known |
2634 |
12 Sep 06 |
nicklas |
@param totalMemory The total memory in bytes, or null if not known |
4070 |
18 Dec 07 |
nicklas |
@param jobs Information about jobs that are currently executing or null if not known |
2634 |
12 Sep 06 |
nicklas |
69 |
*/ |
4070 |
18 Dec 07 |
nicklas |
70 |
public JobAgentInfo(Boolean paused, Integer cpuUsage, Long usedMemory, Long totalMemory, Set<JobInfo> jobs) |
2632 |
08 Sep 06 |
nicklas |
71 |
{ |
2632 |
08 Sep 06 |
nicklas |
72 |
this.created = System.currentTimeMillis(); |
2634 |
12 Sep 06 |
nicklas |
73 |
this.paused = paused; |
2632 |
08 Sep 06 |
nicklas |
74 |
this.jobs = jobs; |
2632 |
08 Sep 06 |
nicklas |
75 |
this.cpuUsage = cpuUsage; |
2632 |
08 Sep 06 |
nicklas |
76 |
this.usedMemory = usedMemory; |
2632 |
08 Sep 06 |
nicklas |
77 |
this.totalMemory = totalMemory; |
2632 |
08 Sep 06 |
nicklas |
78 |
} |
2632 |
08 Sep 06 |
nicklas |
79 |
|
2634 |
12 Sep 06 |
nicklas |
80 |
/** |
2634 |
12 Sep 06 |
nicklas |
Create a new info object and parse the answer from a {@link JobAgentConnection} |
2634 |
12 Sep 06 |
nicklas |
to get the values. |
2634 |
12 Sep 06 |
nicklas |
@param answer The answer from <code>info</code> request to the job agent |
2634 |
12 Sep 06 |
nicklas |
84 |
*/ |
2632 |
08 Sep 06 |
nicklas |
85 |
public JobAgentInfo(String answer) |
2632 |
08 Sep 06 |
nicklas |
86 |
{ |
5447 |
19 Oct 10 |
nicklas |
87 |
Map<String, List<String>> headers = JobAgentConnection.parseAnswer(answer); |
2632 |
08 Sep 06 |
nicklas |
88 |
this.created = System.currentTimeMillis(); |
2634 |
12 Sep 06 |
nicklas |
89 |
String tempPaused = getValue(headers, "Status"); |
2634 |
12 Sep 06 |
nicklas |
90 |
this.paused = tempPaused == null ? null : "Paused".equals(tempPaused); |
2634 |
12 Sep 06 |
nicklas |
91 |
this.cpuUsage = Values.getInteger(getValue(headers, "Cpu"), null); |
2634 |
12 Sep 06 |
nicklas |
92 |
this.totalMemory = Values.getLong(getValue(headers, "Total memory"), null); |
2634 |
12 Sep 06 |
nicklas |
93 |
this.usedMemory = Values.getLong(getValue(headers, "Used memory"), null); |
4070 |
18 Dec 07 |
nicklas |
94 |
this.jobs = new HashSet<JobInfo>(); |
2634 |
12 Sep 06 |
nicklas |
95 |
List<String> jj = getValues(headers, "Job"); |
2634 |
12 Sep 06 |
nicklas |
96 |
if (jj != null) |
2632 |
08 Sep 06 |
nicklas |
97 |
{ |
2634 |
12 Sep 06 |
nicklas |
98 |
for (String id : jj) |
2632 |
08 Sep 06 |
nicklas |
99 |
{ |
2634 |
12 Sep 06 |
nicklas |
100 |
Integer jobId = Values.getInteger(id, null); |
4070 |
18 Dec 07 |
nicklas |
101 |
jobs.add(new JobInfo(jobId, headers)); |
2632 |
08 Sep 06 |
nicklas |
102 |
} |
2632 |
08 Sep 06 |
nicklas |
103 |
} |
2632 |
08 Sep 06 |
nicklas |
104 |
} |
2632 |
08 Sep 06 |
nicklas |
105 |
|
2634 |
12 Sep 06 |
nicklas |
106 |
/** |
2634 |
12 Sep 06 |
nicklas |
Get the age in milliseconds of this object. The age can be used by |
2634 |
12 Sep 06 |
nicklas |
client applications that caches the information to determine when it is |
2634 |
12 Sep 06 |
nicklas |
time to issue a new request. |
2634 |
12 Sep 06 |
nicklas |
@return The age in milliseconds |
2634 |
12 Sep 06 |
nicklas |
111 |
*/ |
2632 |
08 Sep 06 |
nicklas |
112 |
public long getAge() |
2632 |
08 Sep 06 |
nicklas |
113 |
{ |
2632 |
08 Sep 06 |
nicklas |
114 |
return System.currentTimeMillis() - created; |
2632 |
08 Sep 06 |
nicklas |
115 |
} |
2632 |
08 Sep 06 |
nicklas |
116 |
|
2634 |
12 Sep 06 |
nicklas |
117 |
/** |
2634 |
12 Sep 06 |
nicklas |
If the job agent is paused or running. |
2634 |
12 Sep 06 |
nicklas |
@return TRUE if the job agent is pasued, FALSE if it is running, or null |
2634 |
12 Sep 06 |
nicklas |
if not known |
2634 |
12 Sep 06 |
nicklas |
121 |
*/ |
2634 |
12 Sep 06 |
nicklas |
122 |
public Boolean isPaused() |
2634 |
12 Sep 06 |
nicklas |
123 |
{ |
2634 |
12 Sep 06 |
nicklas |
124 |
return paused; |
2634 |
12 Sep 06 |
nicklas |
125 |
} |
2634 |
12 Sep 06 |
nicklas |
126 |
|
2634 |
12 Sep 06 |
nicklas |
127 |
/** |
2634 |
12 Sep 06 |
nicklas |
Get the CPU usage in percent. |
2634 |
12 Sep 06 |
nicklas |
@return The CPU usage or null if not known |
2634 |
12 Sep 06 |
nicklas |
130 |
*/ |
2632 |
08 Sep 06 |
nicklas |
131 |
public Integer getCpuUsage() |
2632 |
08 Sep 06 |
nicklas |
132 |
{ |
2632 |
08 Sep 06 |
nicklas |
133 |
return cpuUsage; |
2632 |
08 Sep 06 |
nicklas |
134 |
} |
2632 |
08 Sep 06 |
nicklas |
135 |
|
2634 |
12 Sep 06 |
nicklas |
136 |
/** |
2634 |
12 Sep 06 |
nicklas |
Get the total memory on the job agent's server in bytes. |
2634 |
12 Sep 06 |
nicklas |
@return The total memory or null if not known |
2634 |
12 Sep 06 |
nicklas |
139 |
*/ |
2632 |
08 Sep 06 |
nicklas |
140 |
public Long getTotalMemory() |
2632 |
08 Sep 06 |
nicklas |
141 |
{ |
2632 |
08 Sep 06 |
nicklas |
142 |
return totalMemory; |
2632 |
08 Sep 06 |
nicklas |
143 |
} |
2632 |
08 Sep 06 |
nicklas |
144 |
|
2634 |
12 Sep 06 |
nicklas |
145 |
/** |
2634 |
12 Sep 06 |
nicklas |
Get the used memory on the job agent's server in bytes. |
2634 |
12 Sep 06 |
nicklas |
@return The used memory or null if not known |
2634 |
12 Sep 06 |
nicklas |
148 |
*/ |
2632 |
08 Sep 06 |
nicklas |
149 |
public Long getUsedMemory() |
2632 |
08 Sep 06 |
nicklas |
150 |
{ |
2632 |
08 Sep 06 |
nicklas |
151 |
return usedMemory; |
2632 |
08 Sep 06 |
nicklas |
152 |
} |
2632 |
08 Sep 06 |
nicklas |
153 |
|
2634 |
12 Sep 06 |
nicklas |
154 |
/** |
4070 |
18 Dec 07 |
nicklas |
Get detailed information about the running jobs. |
4070 |
18 Dec 07 |
nicklas |
@return A set with job information or null if not known |
4070 |
18 Dec 07 |
nicklas |
@since 2.6 |
4070 |
18 Dec 07 |
nicklas |
158 |
*/ |
4070 |
18 Dec 07 |
nicklas |
159 |
public Set<JobInfo> getJobInfo() |
4070 |
18 Dec 07 |
nicklas |
160 |
{ |
2632 |
08 Sep 06 |
nicklas |
161 |
return jobs; |
2632 |
08 Sep 06 |
nicklas |
162 |
} |
2632 |
08 Sep 06 |
nicklas |
163 |
|
2634 |
12 Sep 06 |
nicklas |
164 |
/** |
2634 |
12 Sep 06 |
nicklas |
Convert the information to a string that is suitable for sening as an |
2634 |
12 Sep 06 |
nicklas |
answer to a {@link JobAgentConnection} object. |
2634 |
12 Sep 06 |
nicklas |
167 |
*/ |
6127 |
14 Sep 12 |
nicklas |
168 |
@Override |
2632 |
08 Sep 06 |
nicklas |
169 |
public String toString() |
2632 |
08 Sep 06 |
nicklas |
170 |
{ |
2632 |
08 Sep 06 |
nicklas |
171 |
StringBuilder sb = new StringBuilder(); |
2634 |
12 Sep 06 |
nicklas |
172 |
if (paused != null) { |
2634 |
12 Sep 06 |
nicklas |
173 |
sb.append("Status:").append(paused ? "Paused\n" : "Running\n"); |
2634 |
12 Sep 06 |
nicklas |
174 |
} |
2632 |
08 Sep 06 |
nicklas |
175 |
if (cpuUsage != null) sb.append("Cpu:").append(cpuUsage).append("\n"); |
2632 |
08 Sep 06 |
nicklas |
176 |
if (totalMemory != null) sb.append("Total memory:").append(totalMemory).append("\n"); |
2632 |
08 Sep 06 |
nicklas |
177 |
if (usedMemory != null) sb.append("Used memory:").append(usedMemory).append("\n"); |
2632 |
08 Sep 06 |
nicklas |
178 |
if (jobs != null) |
2632 |
08 Sep 06 |
nicklas |
179 |
{ |
4070 |
18 Dec 07 |
nicklas |
180 |
for (JobInfo job : jobs) |
2632 |
08 Sep 06 |
nicklas |
181 |
{ |
4070 |
18 Dec 07 |
nicklas |
182 |
sb.append(job.toString()); |
2632 |
08 Sep 06 |
nicklas |
183 |
} |
2632 |
08 Sep 06 |
nicklas |
184 |
} |
2632 |
08 Sep 06 |
nicklas |
185 |
return sb.toString(); |
2632 |
08 Sep 06 |
nicklas |
186 |
} |
2634 |
12 Sep 06 |
nicklas |
187 |
|
2634 |
12 Sep 06 |
nicklas |
188 |
private String getValue(Map<String, List<String>> headers, String header) |
2634 |
12 Sep 06 |
nicklas |
189 |
{ |
2634 |
12 Sep 06 |
nicklas |
190 |
List<String> values = headers.get(header); |
2634 |
12 Sep 06 |
nicklas |
191 |
String value = null; |
2634 |
12 Sep 06 |
nicklas |
192 |
if (values != null && values.size() > 0) |
2634 |
12 Sep 06 |
nicklas |
193 |
{ |
2634 |
12 Sep 06 |
nicklas |
194 |
value = values.get(0); |
2634 |
12 Sep 06 |
nicklas |
195 |
} |
2634 |
12 Sep 06 |
nicklas |
196 |
return value; |
2634 |
12 Sep 06 |
nicklas |
197 |
} |
2634 |
12 Sep 06 |
nicklas |
198 |
|
2634 |
12 Sep 06 |
nicklas |
199 |
private List<String> getValues(Map<String, List<String>> headers, String header) |
2634 |
12 Sep 06 |
nicklas |
200 |
{ |
2634 |
12 Sep 06 |
nicklas |
201 |
return headers.get(header); |
2634 |
12 Sep 06 |
nicklas |
202 |
} |
2632 |
08 Sep 06 |
nicklas |
203 |
} |