-
Notifications
You must be signed in to change notification settings - Fork 122
/
QueryAllRemoteJobs.cpp
163 lines (136 loc) · 6.39 KB
/
QueryAllRemoteJobs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidRemoteAlgorithms/QueryAllRemoteJobs.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/FacilityInfo.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/NullValidator.h"
#include "MantidKernel/RemoteJobManager.h"
#include "MantidKernel/SimpleJSON.h"
#include "boost/make_shared.hpp"
namespace Mantid {
namespace RemoteAlgorithms {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(QueryAllRemoteJobs)
using namespace Mantid::Kernel;
using namespace Mantid::API;
// A reference to the logger is provided by the base class, it is called g_log.
void QueryAllRemoteJobs::init() {
// Unlike most algorithms, this one doesn't deal with workspaces....
auto nullValidator = std::make_shared<NullValidator>();
// Compute Resources
std::vector<std::string> computes = Mantid::Kernel::ConfigService::Instance().getFacility().computeResources();
declareProperty("ComputeResource", "", std::make_shared<StringListValidator>(computes),
"The name of the remote computer to query", Direction::Input);
// Mantid can't store arbitrary structs in its properties, so we're going to
// declare several
// array properties for different pieces of data. Values from the same array
// index are for
// the same job.
declareProperty(std::make_unique<ArrayProperty<std::string>>("JobId", nullValidator, Direction::Output),
"ID string for the job");
declareProperty(std::make_unique<ArrayProperty<std::string>>("JobStatusString", nullValidator, Direction::Output),
"Description of the job's current status (Queued, Running, "
"Complete, etc..)");
declareProperty(std::make_unique<ArrayProperty<std::string>>("JobName", nullValidator, Direction::Output),
"Name of the job (specified when the job was submitted)");
declareProperty(std::make_unique<ArrayProperty<std::string>>("ScriptName", nullValidator, Direction::Output),
"The name of the python script that was executed");
declareProperty(std::make_unique<ArrayProperty<std::string>>("TransID", nullValidator, Direction::Output),
"The ID of the transaction that owns the job");
// Times for job submit, job start and job complete (may be empty depending
// on the server-side implementation)
declareProperty(std::make_unique<ArrayProperty<std::string>>("SubmitDate", nullValidator, Direction::Output),
"The date & time the job was submitted");
declareProperty(std::make_unique<ArrayProperty<std::string>>("StartDate", nullValidator, Direction::Output),
"The date & time the job actually started executing");
declareProperty(std::make_unique<ArrayProperty<std::string>>("CompletionDate", nullValidator, Direction::Output),
"The date & time the job finished");
}
void QueryAllRemoteJobs::exec() {
std::shared_ptr<RemoteJobManager> jobManager =
Mantid::Kernel::ConfigService::Instance().getFacility().getRemoteJobManager(getPropertyValue("ComputeResource"));
// jobManager is a std::shared_ptr...
if (!jobManager) {
// Requested compute resource doesn't exist
// TODO: should we create our own exception class for this??
throw(std::runtime_error(
std::string("Unable to create a compute resource named " + getPropertyValue("ComputeResource"))));
}
std::istream &respStream = jobManager->httpGet("/query");
JSONObject resp;
try {
initFromStream(resp, respStream);
} catch (JSONParseException &) {
// Nobody else knows what a JSONParseException is, so rethrow as a
// runtime_error
throw(std::runtime_error("Error parsing data returned from the server. "
"This probably indicates a server-side error of "
"some kind."));
}
if (jobManager->lastStatus() == Poco::Net::HTTPResponse::HTTP_OK) {
std::vector<std::string> jobIds;
std::vector<std::string> jobStatusStrs;
std::vector<std::string> jobNames;
std::vector<std::string> scriptNames;
std::vector<std::string> transIds;
std::vector<std::string> submitDates;
std::vector<std::string> startDates;
std::vector<std::string> completionDates;
JSONObject::const_iterator it = resp.begin();
while (it != resp.end()) {
jobIds.emplace_back((*it).first);
JSONObject jobData;
(*it).second.getValue(jobData);
std::string value;
jobData["JobStatus"].getValue(value);
jobStatusStrs.emplace_back(value);
jobData["JobName"].getValue(value);
jobNames.emplace_back(value);
jobData["ScriptName"].getValue(value);
scriptNames.emplace_back(value);
jobData["TransID"].getValue(value);
transIds.emplace_back(value);
// The time stuff is actually an optional extension. We could check the
// info
// URL and see if the server implements it, but it's easier to just look
// in
// the output and see if the values are there...
if (jobData.find("SubmitDate") != jobData.end()) {
jobData["SubmitDate"].getValue(value);
submitDates.emplace_back(value);
jobData["StartDate"].getValue(value);
startDates.emplace_back(value);
jobData["CompletionDate"].getValue(value);
completionDates.emplace_back(value);
} else {
// push back empty strings just so all the array properties have the
// same
// number of elements
submitDates.emplace_back("");
startDates.emplace_back("");
completionDates.emplace_back("");
}
++it;
}
setProperty("JobId", jobIds);
setProperty("JobStatusString", jobStatusStrs);
setProperty("JobName", jobNames);
setProperty("ScriptName", scriptNames);
setProperty("TransID", transIds);
setProperty("SubmitDate", submitDates);
setProperty("StartDate", startDates);
setProperty("CompletionDate", completionDates);
} else {
std::string errMsg;
resp["Err_Msg"].getValue(errMsg);
throw(std::runtime_error(errMsg));
}
}
} // end namespace RemoteAlgorithms
} // end namespace Mantid