Debian Codegraph @

avogadro/molequeue/batchjob.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "batchjob.h"
7 #include "molequeuemanager.h"
8
9 #include <QtCore/QDebug>
10
11 #include <limits>
12
13 namespace Avogadro::MoleQueue {
14
15 // initialize statics
16 const BatchJob::BatchId BatchJob::InvalidBatchId = -1;
17 const BatchJob::RequestId BatchJob::InvalidRequestId = -1;
18 const BatchJob::ServerId BatchJob::InvalidServerId =
19 std::numeric_limits<BatchJob::ServerId>::max();
20
21 BatchJob::BatchJob(QObject* par) : QObject(par)
22 {
23 setup();
24 }
25
26 BatchJob::BatchJob(const QString& scriptFilePath, QObject* par)
27 : QObject(par), m_inputGenerator(scriptFilePath)
28 {
29 setup();
30 }
31
32 BatchJob::BatchId BatchJob::submitNextJob(const Core::Molecule& mol)
33 {
34 // Is everything configured?
35 if (!m_inputGenerator.isValid() || m_inputGeneratorOptions.empty() ||
36 m_moleQueueOptions.empty()) {
37 return InvalidBatchId;
38 }
39
40 // Verify that molequeue is running:
41 MoleQueueManager& mqManager = MoleQueueManager::instance();
42 if (!mqManager.connectIfNeeded())
43 return InvalidBatchId;
44
45 // Generate the input:
46 if (!m_inputGenerator.generateInput(m_inputGeneratorOptions, mol)) {
47 if (!m_inputGenerator.errorList().isEmpty()) {
48 qWarning() << "BatchJob::submitNextJob() error:\n\t"
49 << m_inputGenerator.errorList().join("\n\t");
50 }
51 return InvalidBatchId;
52 }
53
54 // Warnings are non-fatal -- just print them for now:
55 if (!m_inputGenerator.warningList().isEmpty()) {
56 qWarning() << "BatchJob::submitNextJob() warning:\n\t"
57 << m_inputGenerator.warningList().join("\n\t");
58 }
59
60 BatchId bId = m_jobObjects.size();
61
62 // Create the job object:
63 JobObject job;
64 job.fromJson(m_moleQueueOptions);
65 job.setDescription(
66 tr("Batch Job #%L1 (%2)").arg(bId + 1).arg(job.description()));
67
68 // Main input file:
69 const QString mainFileName = m_inputGenerator.mainFileName();
70 job.setInputFile(mainFileName, m_inputGenerator.fileContents(mainFileName));
71
72 // Any additional input files:
73 QStringList fileNames = m_inputGenerator.fileNames();
74 fileNames.removeOne(mainFileName);
75 foreach (const QString& fn, fileNames)
76 job.appendAdditionalInputFile(fn, m_inputGenerator.fileContents(fn));
77
78 // Submit the job
79 RequestId rId = mqManager.client().submitJob(job);
80
81 // Was submission successful?
82 if (rId < 0)
83 return InvalidBatchId;
84
85 // Register the job and assign the ID.
86 m_jobObjects.push_back(job);
87 m_states.push_back(None);
88 m_requests.insert(rId, Request(Request::SubmitJob, bId));
89
90 return bId;
91 }
92
93 bool BatchJob::lookupJob(BatchId bId)
94 {
95 ServerId sId = serverId(static_cast<BatchId>(bId));
96 if (sId == InvalidServerId)
97 return false;
98
99 // Verify that molequeue is running:
100 MoleQueueManager& mqManager = MoleQueueManager::instance();
101 if (!mqManager.connectIfNeeded())
102 return false;
103
104 Client& client = mqManager.client();
105 RequestId rId = client.lookupJob(sId);
106 m_requests.insert(rId, Request(Request::LookupJob, bId));
107 return true;
108 }
109
110 void BatchJob::handleSubmissionReply(int rId, unsigned int sId)
111 {
112 Request req = m_requests.value(rId);
113 if (req.isValid()) {
114 m_requests.remove(rId);
115 if (req.batchId >= m_jobObjects.size()) {
116 qWarning() << "BatchJob::handleSubmissionReply(): batchID out of range.";
117 return;
118 }
119 m_jobObjects[req.batchId].setValue("moleQueueId",
120 QVariant(static_cast<ServerId>(sId)));
121 m_serverIds.insert(sId, req.batchId);
122 // Request full job details:
123 lookupJob(req.batchId);
124 }
125 }
126
127 void BatchJob::handleJobStateChange(unsigned int sId, const QString&,
128 const QString&)
129 {
130 BatchId bId = m_serverIds.value(static_cast<ServerId>(sId), InvalidBatchId);
131 if (bId == InvalidBatchId)
132 return;
133 // Update full job details:
134 lookupJob(bId);
135 }
136
137 void BatchJob::handleLookupJobReply(int rId, const QJsonObject& jobInfo)
138 {
139 Request req = m_requests.value(rId);
140 if (req.isValid()) {
141 m_requests.remove(rId);
142 if (req.batchId >= m_jobObjects.size()) {
143 qWarning() << "BatchJob::handleSubmissionReply(): batchID out of range.";
144 return;
145 }
146 JobObject& job(m_jobObjects[req.batchId]);
147 job.fromJson(jobInfo);
148
149 JobState oldState = m_states[req.batchId];
150 JobState newState = stringToState(job.value("jobState").toString());
151 m_states[req.batchId] = newState;
152 emit jobUpdated(req.batchId, true);
153 if (!isTerminal(oldState) && isTerminal(newState))
154 emit jobCompleted(req.batchId, newState);
155 }
156 }
157
158 void BatchJob::handleErrorResponse(int requestId, int errorCode,
159 const QString& errorMessage,
160 const QJsonValue& errorData)
161 {
162 qDebug() << "Error rcv'd: {"
163 << "requestId:" << requestId << "errorCode:" << errorCode
164 << "errorMessage:" << errorMessage << "errorData:" << errorData
165 << "}";
166
167 Request req = m_requests.value(requestId);
168
169 if (!req.isValid())
170 return;
171
172 m_requests.remove(requestId);
173
174 if (req.batchId < m_jobObjects.size())
175 return;
176
177 switch (req.type) {
178 case Request::SubmitJob:
179 // The job was rejected:
180 qDebug() << "Batch job" << req.batchId << "was rejected by MoleQueue.";
181 m_states[req.batchId] = Rejected;
182 m_jobObjects[req.batchId].fromJson(QJsonObject());
183 break;
184 case Request::LookupJob:
185 qDebug() << "Batch job" << req.batchId << "failed to update.";
186 emit jobUpdated(req.batchId, false);
187 break;
188 default:
189 case Request::InvalidType:
190 break;
191 }
192 }
193
194 void BatchJob::setup()
195 {
196 static bool metaTypesRegistered = false;
197 if (!metaTypesRegistered) {
198 qRegisterMetaType<BatchId>("Avogadro::QtGui::BatchJob::BatchId");
199 qRegisterMetaType<BatchId>("BatchId");
200 qRegisterMetaType<ServerId>("Avogadro::QtGui::BatchJob::ServerId");
201 qRegisterMetaType<ServerId>("ServerId");
202 qRegisterMetaType<RequestId>("Avogadro::QtGui::BatchJob::RequestId");
203 qRegisterMetaType<RequestId>("RequestId");
204 metaTypesRegistered = true;
205 }
206
207 MoleQueueManager& mqManager = MoleQueueManager::instance();
208 Client& client = mqManager.client();
209 connect(&client, SIGNAL(submitJobResponse(int, uint)),
210 SLOT(handleSubmissionReply(int, uint)));
211 connect(&client, SIGNAL(lookupJobResponse(int, QJsonObject)),
212 SLOT(handleLookupJobReply(int, QJsonObject)));
213 connect(&client, SIGNAL(jobStateChanged(uint, QString, QString)),
214 SLOT(handleJobStateChange(uint, QString, QString)));
215 connect(&client, SIGNAL(errorReceived(int, int, QString, QJsonValue)),
216 SLOT(handleErrorResponse(int, int, QString, QJsonValue)));
217 }
218
219 } // namespace Avogadro::MoleQueue