Debian Codegraph @

avogadro/molequeue/batchjob.h

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 #ifndef AVOGADRO_QTGUI_BATCHJOB_H
7 #define AVOGADRO_QTGUI_BATCHJOB_H
8
9 #include <QtCore/QObject>
10
11 #include "inputgenerator.h"
12
13 #include <avogadro/core/avogadrocore.h>
14
15 #include "client/jobobject.h"
16
17 #include <QtCore/QJsonObject>
18 #include <QtCore/QMap>
19 #include <QtCore/QVector>
20
21 namespace Avogadro {
22 namespace Core {
23 class Molecule;
24 } // end namespace Core
25
26 namespace MoleQueue {
27
28 /**
29 * @brief The BatchJob class manages a collection of jobs that are configured
30 * using the same InputGenerator and MoleQueue options. For use with
31 * InputGeneratorDialog::configureBatchJob(BatchJob&).
32 */
33 class AVOGADROMOLEQUEUE_EXPORT BatchJob : public QObject
34 {
35 Q_OBJECT
36 public:
37 /**
38 * Job status. Same as those defined in molequeueglobal.h. The 'Rejected'
39 * state is added to identify jobs that rejected by molequeue prior to having
40 * a MoleQueue id (ServerId) set.
41 */
42 enum JobState
43 {
44 Rejected = -2,
45 Unknown = -1,
46 None = 0,
47 Accepted,
48 QueuedLocal,
49 Submitted,
50 QueuedRemote,
51 RunningLocal,
52 RunningRemote,
53 Finished,
54 Canceled,
55 Error
56 };
57
58 /**
59 * Type used to identify a job within this batch. Unique to this object.
60 */
61 using BatchId = int;
62 static const BatchId InvalidBatchId;
63
64 /**
65 * Type used to identify requests sent to the MoleQueue server.
66 */
67 using RequestId = int;
68 static const RequestId InvalidRequestId;
69
70 /**
71 * Type used by MoleQueue to identify jobs. Unique across the connected
72 * MoleQueue server.
73 */
74 using ServerId = unsigned int;
75 static const ServerId InvalidServerId;
76
77 /**
78 * Construct a new BatchJob object. If provided, ese scriptFilePath to setup
79 * the input generator.
80 */
81 explicit BatchJob(QObject* parent = nullptr);
82 explicit BatchJob(const QString& scriptFilePath, QObject* parent = nullptr);
83 ~BatchJob() override = default;
84
85 /**
86 * Options for the input generator.
87 * @{
88 */
89 void setInputGeneratorOptions(const QJsonObject& opts);
90 QJsonObject inputGeneratorOptions() const;
91 /**@}*/
92
93 /**
94 * Options for MoleQueue.
95 * @{
96 */
97 void setMoleQueueOptions(const QJsonObject& opts);
98 QJsonObject moleQueueOptions() const;
99 JobObject moleQueueJobTemplate() const;
100 /**@}*/
101
102 /**
103 * The internal InputGenerator.
104 * @{
105 */
106 const InputGenerator& inputGenerator() const;
107 InputGenerator& inputGenerator();
108 /**@}*/
109
110 /**
111 * A string that will be used in the MoleQueue interface to identify this
112 * batch job. Taken from the InputGenerator configured title.
113 */
114 QString description() const;
115
116 /**
117 * @return The state of the job with the batch id @a batchId.
118 */
119 JobState jobState(BatchId batchId) const;
120
121 /**
122 * @return The server id of the job with the batch id @a batchId.
123 */
124 ServerId serverId(BatchId batchId) const;
125
126 /**
127 * @return The most recent JobObject for the job with the batch id @a batchId.
128 * These are updated for each change in job state.
129 */
130 JobObject jobObject(BatchId batchId) const;
131
132 /**
133 * @return True if @a state corresponds to a job that is finished running.
134 */
135 static bool isTerminal(JobState state);
136
137 /**
138 * @return True if there are still running jobs.
139 */
140 bool hasUnfinishedJobs() const;
141
142 /**
143 * @return The number of jobs that are running.
144 */
145 int unfinishedJobCount() const;
146
147 /**
148 * @return The number of completed jobs.
149 */
150 int finishedJobCount() const;
151
152 /**
153 * @return The total number of jobs in the batch.
154 */
155 int jobCount() const;
156
157 public slots:
158 /**
159 * Submit a job using the current configuration for @a mol.
160 * @return The BatchId of the job, or InvalidBatchId if there was an error.
161 */
162 virtual BatchId submitNextJob(const Core::Molecule& mol);
163
164 /**
165 * Request updated job details from the MoleQueue server for the job with
166 * the batch id @a batchId.
167 *
168 * jobUpdated is emitted when the request is complete.
169 *
170 * @return True if the request is sent.
171 */
172 bool lookupJob(BatchId batchId);
173
174 signals:
175 /**
176 * Emitted when the reply from lookupJob is received. @a success will be false
177 * if MoleQueue sends an error response (likely because the job was removed
178 * from the job manager).
179 */
180 void jobUpdated(Avogadro::MoleQueue::BatchJob::BatchId batchId, bool success);
181
182 /**
183 * Emitted when the job associated with @a batchId completes. @a status
184 * is the final state of the jobs and can be used to determine whether or not
185 * the job finished successfully.
186 */
187 void jobCompleted(Avogadro::MoleQueue::BatchJob::BatchId batchId,
188 Avogadro::MoleQueue::BatchJob::JobState status);
189
190 private slots:
191 void handleSubmissionReply(int requestId, unsigned int serverId);
192 void handleJobStateChange(unsigned int serverId, const QString& oldState,
193 const QString& newState);
194 void handleLookupJobReply(int requestId, const QJsonObject& jobInfo);
195 void handleErrorResponse(int requestId, int errorCode,
196 const QString& errorMessage,
197 const QJsonValue& errorData);
198
199 private: // structs
200 /**
201 * Internal struct for tracking request metadata.
202 */
203 struct Request
204 {
205 enum Type
206 {
207 InvalidType,
208 SubmitJob,
209 LookupJob
210 };
211 explicit Request(Type t = InvalidType, BatchId b = InvalidBatchId);
212 bool isValid() const { return type != InvalidType; }
213
214 Type type;
215 BatchId batchId;
216 };
217
218 private: // methods
219 void setup();
220 static JobState stringToState(const QString& string);
221 static QString stateToString(JobState state);
222
223 private: // variables
224 InputGenerator m_inputGenerator;
225 QJsonObject m_inputGeneratorOptions;
226 QJsonObject m_moleQueueOptions;
227
228 /// Cached job states.
229 QList<JobObject> m_jobObjects;
230 /// Lookup batch ids from server ids.
231 QMap<ServerId, BatchId> m_serverIds;
232 /// Job states. For fast lookups without string conversions.
233 QVector<JobState> m_states;
234 /// Pending requests.
235 QMap<RequestId, Request> m_requests;
236 };
237
238 inline BatchJob::Request::Request(Type t, BatchId b) : type(t), batchId(b) {}
239
240 inline void BatchJob::setInputGeneratorOptions(const QJsonObject& opts)
241 {
242 m_inputGeneratorOptions = opts;
243 }
244
245 inline QJsonObject BatchJob::inputGeneratorOptions() const
246 {
247 return m_inputGeneratorOptions;
248 }
249
250 inline void BatchJob::setMoleQueueOptions(const QJsonObject& opts)
251 {
252 m_moleQueueOptions = opts;
253 }
254
255 inline QJsonObject BatchJob::moleQueueOptions() const
256 {
257 return m_moleQueueOptions;
258 }
259
260 inline JobObject BatchJob::moleQueueJobTemplate() const
261 {
262 JobObject result;
263 result.fromJson(m_moleQueueOptions);
264 return result;
265 }
266
267 inline const InputGenerator& BatchJob::inputGenerator() const
268 {
269 return m_inputGenerator;
270 }
271
272 inline InputGenerator& BatchJob::inputGenerator()
273 {
274 return m_inputGenerator;
275 }
276
277 inline QString BatchJob::description() const
278 {
279 return moleQueueJobTemplate().description();
280 }
281
282 inline BatchJob::JobState BatchJob::jobState(BatchJob::BatchId id) const
283 {
284 return id < m_states.size() ? m_states[id] : Unknown;
285 }
286
287 inline BatchJob::ServerId BatchJob::serverId(BatchJob::BatchId id) const
288 {
289 return id < m_jobObjects.size() ? m_jobObjects[id]
290 .value("moleQueueId", InvalidServerId)
291 .value<ServerId>()
292 : InvalidServerId;
293 }
294
295 inline JobObject BatchJob::jobObject(BatchJob::BatchId id) const
296 {
297 return id < m_jobObjects.size() ? m_jobObjects[id] : JobObject();
298 }
299
300 inline bool BatchJob::isTerminal(BatchJob::JobState state)
301 {
302 switch (state) {
303 case Rejected:
304 case Finished:
305 case Canceled:
306 case Error:
307 return true;
308 default:
309 return false;
310 }
311 }
312
313 inline bool BatchJob::hasUnfinishedJobs() const
314 {
315 for (auto m_state : m_states) {
316 if (!isTerminal(m_state))
317 return true;
318 }
319 return false;
320 }
321
322 inline int BatchJob::unfinishedJobCount() const
323 {
324 int result = 0;
325 for (auto m_state : m_states) {
326 if (!isTerminal(m_state))
327 ++result;
328 }
329 return result;
330 }
331
332 inline int BatchJob::finishedJobCount() const
333 {
334 int result = 0;
335 for (auto m_state : m_states) {
336 if (isTerminal(m_state))
337 ++result;
338 }
339 return result;
340 }
341
342 inline int BatchJob::jobCount() const
343 {
344 return m_serverIds.size();
345 }
346
347 inline BatchJob::JobState BatchJob::stringToState(const QString& str)
348 {
349 if (str == QLatin1String("None"))
350 return None;
351 else if (str == QLatin1String("Rejected"))
352 return Rejected;
353 else if (str == QLatin1String("Accepted"))
354 return Accepted;
355 else if (str == QLatin1String("QueuedLocal"))
356 return QueuedLocal;
357 else if (str == QLatin1String("Submitted"))
358 return Submitted;
359 else if (str == QLatin1String("QueuedRemote"))
360 return QueuedRemote;
361 else if (str == QLatin1String("RunningLocal"))
362 return RunningLocal;
363 else if (str == QLatin1String("RunningRemote"))
364 return RunningRemote;
365 else if (str == QLatin1String("Finished"))
366 return Finished;
367 else if (str == QLatin1String("Canceled"))
368 return Canceled;
369 else if (str == QLatin1String("Error"))
370 return Error;
371 else
372 return Unknown;
373 }
374
375 inline QString BatchJob::stateToString(BatchJob::JobState state)
376 {
377 switch (state) {
378 case None:
379 return QString("None");
380 case Accepted:
381 return QString("Accepted");
382 case Rejected:
383 return QString("Rejected");
384 case QueuedLocal:
385 return QString("QueuedLocal");
386 case Submitted:
387 return QString("Submitted");
388 case QueuedRemote:
389 return QString("QueuedRemote");
390 case RunningLocal:
391 return QString("RunningLocal");
392 case RunningRemote:
393 return QString("RunningRemote");
394 case Finished:
395 return QString("Finished");
396 case Canceled:
397 return QString("Canceled");
398 case Error:
399 return QString("Error");
400 default:
401 case Unknown:
402 return QString("Unknown");
403 }
404 }
405
406 } // namespace MoleQueue
407 } // namespace Avogadro
408
409 #endif // AVOGADRO_MOLEQUEUE_BATCHJOB_H