-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
batchjob.cpp
223 lines (187 loc) · 6.77 KB
/
batchjob.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/******************************************************************************
This source file is part of the Avogadro project.
This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/
#include "batchjob.h"
#include "molequeuemanager.h"
#include <QtCore/QDebug>
#include <limits>
namespace Avogadro::MoleQueue {
// initialize statics
const BatchJob::BatchId BatchJob::InvalidBatchId = -1;
const BatchJob::RequestId BatchJob::InvalidRequestId = -1;
const BatchJob::ServerId BatchJob::InvalidServerId =
std::numeric_limits<BatchJob::ServerId>::max();
BatchJob::BatchJob(QObject* par) : QObject(par)
{
setup();
}
BatchJob::BatchJob(const QString& scriptFilePath, QObject* par)
: QObject(par), m_inputGenerator(scriptFilePath)
{
setup();
}
BatchJob::~BatchJob()
{
}
BatchJob::BatchId BatchJob::submitNextJob(const Core::Molecule& mol)
{
// Is everything configured?
if (!m_inputGenerator.isValid() || m_inputGeneratorOptions.empty() ||
m_moleQueueOptions.empty()) {
return InvalidBatchId;
}
// Verify that molequeue is running:
MoleQueueManager& mqManager = MoleQueueManager::instance();
if (!mqManager.connectIfNeeded())
return InvalidBatchId;
// Generate the input:
if (!m_inputGenerator.generateInput(m_inputGeneratorOptions, mol)) {
if (!m_inputGenerator.errorList().isEmpty()) {
qWarning() << "BatchJob::submitNextJob() error:\n\t"
<< m_inputGenerator.errorList().join("\n\t");
}
return InvalidBatchId;
}
// Warnings are non-fatal -- just print them for now:
if (!m_inputGenerator.warningList().isEmpty()) {
qWarning() << "BatchJob::submitNextJob() warning:\n\t"
<< m_inputGenerator.warningList().join("\n\t");
}
BatchId bId = m_jobObjects.size();
// Create the job object:
JobObject job;
job.fromJson(m_moleQueueOptions);
job.setDescription(
tr("Batch Job #%L1 (%2)").arg(bId + 1).arg(job.description()));
// Main input file:
const QString mainFileName = m_inputGenerator.mainFileName();
job.setInputFile(mainFileName, m_inputGenerator.fileContents(mainFileName));
// Any additional input files:
QStringList fileNames = m_inputGenerator.fileNames();
fileNames.removeOne(mainFileName);
foreach (const QString& fn, fileNames)
job.appendAdditionalInputFile(fn, m_inputGenerator.fileContents(fn));
// Submit the job
RequestId rId = mqManager.client().submitJob(job);
// Was submission successful?
if (rId < 0)
return InvalidBatchId;
// Register the job and assign the ID.
m_jobObjects.push_back(job);
m_states.push_back(None);
m_requests.insert(rId, Request(Request::SubmitJob, bId));
return bId;
}
bool BatchJob::lookupJob(BatchId bId)
{
ServerId sId = serverId(static_cast<BatchId>(bId));
if (sId == InvalidServerId)
return false;
// Verify that molequeue is running:
MoleQueueManager& mqManager = MoleQueueManager::instance();
if (!mqManager.connectIfNeeded())
return false;
Client& client = mqManager.client();
RequestId rId = client.lookupJob(sId);
m_requests.insert(rId, Request(Request::LookupJob, bId));
return true;
}
void BatchJob::handleSubmissionReply(int rId, unsigned int sId)
{
Request req = m_requests.value(rId);
if (req.isValid()) {
m_requests.remove(rId);
if (req.batchId >= m_jobObjects.size()) {
qWarning() << "BatchJob::handleSubmissionReply(): batchID out of range.";
return;
}
m_jobObjects[req.batchId].setValue("moleQueueId",
QVariant(static_cast<ServerId>(sId)));
m_serverIds.insert(sId, req.batchId);
// Request full job details:
lookupJob(req.batchId);
}
}
void BatchJob::handleJobStateChange(unsigned int sId, const QString&,
const QString&)
{
BatchId bId = m_serverIds.value(static_cast<ServerId>(sId), InvalidBatchId);
if (bId == InvalidBatchId)
return;
// Update full job details:
lookupJob(bId);
}
void BatchJob::handleLookupJobReply(int rId, const QJsonObject& jobInfo)
{
Request req = m_requests.value(rId);
if (req.isValid()) {
m_requests.remove(rId);
if (req.batchId >= m_jobObjects.size()) {
qWarning() << "BatchJob::handleSubmissionReply(): batchID out of range.";
return;
}
JobObject& job(m_jobObjects[req.batchId]);
job.fromJson(jobInfo);
JobState oldState = m_states[req.batchId];
JobState newState = stringToState(job.value("jobState").toString());
m_states[req.batchId] = newState;
emit jobUpdated(req.batchId, true);
if (!isTerminal(oldState) && isTerminal(newState))
emit jobCompleted(req.batchId, newState);
}
}
void BatchJob::handleErrorResponse(int requestId, int errorCode,
const QString& errorMessage,
const QJsonValue& errorData)
{
qDebug() << "Error rcv'd: {"
<< "requestId:" << requestId << "errorCode:" << errorCode
<< "errorMessage:" << errorMessage << "errorData:" << errorData
<< "}";
Request req = m_requests.value(requestId);
if (!req.isValid())
return;
m_requests.remove(requestId);
if (req.batchId < m_jobObjects.size())
return;
switch (req.type) {
case Request::SubmitJob:
// The job was rejected:
qDebug() << "Batch job" << req.batchId << "was rejected by MoleQueue.";
m_states[req.batchId] = Rejected;
m_jobObjects[req.batchId].fromJson(QJsonObject());
break;
case Request::LookupJob:
qDebug() << "Batch job" << req.batchId << "failed to update.";
emit jobUpdated(req.batchId, false);
break;
default:
case Request::InvalidType:
break;
}
}
void BatchJob::setup()
{
static bool metaTypesRegistered = false;
if (!metaTypesRegistered) {
qRegisterMetaType<BatchId>("Avogadro::QtGui::BatchJob::BatchId");
qRegisterMetaType<BatchId>("BatchId");
qRegisterMetaType<ServerId>("Avogadro::QtGui::BatchJob::ServerId");
qRegisterMetaType<ServerId>("ServerId");
qRegisterMetaType<RequestId>("Avogadro::QtGui::BatchJob::RequestId");
qRegisterMetaType<RequestId>("RequestId");
metaTypesRegistered = true;
}
MoleQueueManager& mqManager = MoleQueueManager::instance();
Client& client = mqManager.client();
connect(&client, SIGNAL(submitJobResponse(int, uint)),
SLOT(handleSubmissionReply(int, uint)));
connect(&client, SIGNAL(lookupJobResponse(int, QJsonObject)),
SLOT(handleLookupJobReply(int, QJsonObject)));
connect(&client, SIGNAL(jobStateChanged(uint, QString, QString)),
SLOT(handleJobStateChange(uint, QString, QString)));
connect(&client, SIGNAL(errorReceived(int, int, QString, QJsonValue)),
SLOT(handleErrorResponse(int, int, QString, QJsonValue)));
}
} // namespace Avogadro