| 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 "molequeuewidget.h" |
| 7 |
#include "ui_molequeuewidget.h" |
| 8 |
|
| 9 |
#include "molequeuemanager.h" |
| 10 |
|
| 11 |
#include <QtWidgets/QMessageBox> |
| 12 |
|
| 13 |
#include <QtCore/QTimer> |
| 14 |
|
| 15 |
#include <limits> |
| 16 |
|
| 17 |
namespace Avogadro::MoleQueue { |
| 18 |
|
| 19 |
const unsigned int MoleQueueWidget::InvalidMoleQueueId( |
| 20 |
std::numeric_limits<unsigned int>::max()); |
| 21 |
|
| 22 |
MoleQueueWidget::MoleQueueWidget(QWidget* parent_) |
| 23 |
: QWidget(parent_), m_ui(new Ui::MoleQueueWidget), m_jobState("Unknown"), |
| 24 |
m_requestId(-1), m_moleQueueId(InvalidMoleQueueId) |
| 25 |
{ |
| 26 |
m_ui->setupUi(this); |
| 27 |
|
| 28 |
connect(m_ui->refreshProgramsButton, SIGNAL(clicked()), |
| 29 |
SLOT(refreshPrograms())); |
| 30 |
|
| 31 |
MoleQueueManager& mqManager = MoleQueueManager::instance(); |
| 32 |
m_ui->queueListView->setModel(&mqManager.queueListModel()); |
| 33 |
|
| 34 |
if (mqManager.connectIfNeeded()) |
| 35 |
mqManager.requestQueueList(); |
| 36 |
} |
| 37 |
|
| 38 |
MoleQueueWidget::~MoleQueueWidget() |
| 39 |
{ |
| 40 |
delete m_ui; |
| 41 |
} |
| 42 |
|
| 43 |
JobObject& MoleQueueWidget::jobTemplate() |
| 44 |
{ |
| 45 |
return m_jobTemplate; |
| 46 |
} |
| 47 |
|
| 48 |
const JobObject& MoleQueueWidget::jobTemplate() const |
| 49 |
{ |
| 50 |
return m_jobTemplate; |
| 51 |
} |
| 52 |
|
| 53 |
void MoleQueueWidget::setJobTemplate(const JobObject& job) |
| 54 |
{ |
| 55 |
m_jobTemplate = job; |
| 56 |
|
| 57 |
m_ui->numberOfCores->setValue(job.value("numberOfCores", 1).toInt()); |
| 58 |
m_ui->cleanRemoteFiles->setChecked( |
| 59 |
job.value("cleanRemoteFiles", false).toBool()); |
| 60 |
m_ui->hideFromGui->setChecked(job.value("hideFromGui", false).toBool()); |
| 61 |
m_ui->popupOnStateChange->setChecked( |
| 62 |
job.value("popupOnStateChange", false).toBool()); |
| 63 |
} |
| 64 |
|
| 65 |
void MoleQueueWidget::refreshPrograms() |
| 66 |
{ |
| 67 |
MoleQueueManager& mqManager = MoleQueueManager::instance(); |
| 68 |
if (!mqManager.connectIfNeeded()) { |
| 69 |
QMessageBox::information(this, tr("Cannot connect to MoleQueue"), |
| 70 |
tr("Cannot connect to MoleQueue server. Please " |
| 71 |
"ensure that it is running and try again.")); |
| 72 |
return; |
| 73 |
} |
| 74 |
mqManager.requestQueueList(); |
| 75 |
} |
| 76 |
|
| 77 |
int MoleQueueWidget::submitJobRequest() |
| 78 |
{ |
| 79 |
m_submissionError.clear(); |
| 80 |
m_jobState = "Unknown"; |
| 81 |
m_requestId = -1; |
| 82 |
m_moleQueueId = InvalidMoleQueueId; |
| 83 |
|
| 84 |
MoleQueueManager& mqManager = MoleQueueManager::instance(); |
| 85 |
if (!mqManager.connectIfNeeded()) |
| 86 |
return -1; |
| 87 |
|
| 88 |
MoleQueue::JobObject job(configuredJob()); |
| 89 |
if (job.queue().isEmpty()) // if the queue is not set, the job config failed. |
| 90 |
return -1; |
| 91 |
|
| 92 |
m_requestId = mqManager.client().submitJob(job); |
| 93 |
if (m_requestId >= 0) { |
| 94 |
listenForJobSubmitReply(); |
| 95 |
listenForJobStateChange(); |
| 96 |
} else { |
| 97 |
m_submissionError = tr("Client failed to submit job to MoleQueue."); |
| 98 |
// Single shot ensures that this signal is emitted after control returns |
| 99 |
// to the event loop |
| 100 |
QTimer::singleShot(0, this, SIGNAL(jobSubmitted(false))); |
| 101 |
} |
| 102 |
return m_requestId; |
| 103 |
} |
| 104 |
|
| 105 |
void MoleQueueWidget::showAndSelectProgram(const QString& programName) |
| 106 |
{ |
| 107 |
MoleQueueManager& mqManager = MoleQueueManager::instance(); |
| 108 |
setProperty("selectProgramName", programName); |
| 109 |
|
| 110 |
connect(&mqManager, SIGNAL(queueListUpdated()), |
| 111 |
SLOT(showAndSelectProgramHandler())); |
| 112 |
|
| 113 |
if (mqManager.connectIfNeeded()) |
| 114 |
mqManager.requestQueueList(); |
| 115 |
} |
| 116 |
|
| 117 |
bool MoleQueueWidget::openOutput() const |
| 118 |
{ |
| 119 |
return m_ui->openOutput->isChecked(); |
| 120 |
} |
| 121 |
|
| 122 |
bool MoleQueueWidget::requestJobLookup() |
| 123 |
{ |
| 124 |
MoleQueueManager& mqManager = MoleQueueManager::instance(); |
| 125 |
if (m_moleQueueId != InvalidMoleQueueId && mqManager.connectIfNeeded()) { |
| 126 |
listenForLookupJobReply(); |
| 127 |
int reqId = mqManager.client().lookupJob(m_moleQueueId); |
| 128 |
setProperty("lookupJobRequestId", reqId); |
| 129 |
return true; |
| 130 |
} |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
MoleQueue::JobObject MoleQueueWidget::configuredJob() const |
| 135 |
{ |
| 136 |
MoleQueueManager& mqManager = MoleQueueManager::instance(); |
| 137 |
|
| 138 |
// get queue/program |
| 139 |
QModelIndexList sel(m_ui->queueListView->selectionModel()->selectedIndexes()); |
| 140 |
if (sel.size() < 1) { |
| 141 |
QMessageBox::information(parentWidget(), tr("No program selected."), |
| 142 |
tr("Please select the target program from the " |
| 143 |
"\"Queue and Program\" list.")); |
| 144 |
return MoleQueue::JobObject(); |
| 145 |
} |
| 146 |
|
| 147 |
QString queue; |
| 148 |
QString program; |
| 149 |
if (!mqManager.queueListModel().lookupProgram(sel.first(), queue, program)) { |
| 150 |
QMessageBox::critical(parentWidget(), tr("Internal error."), |
| 151 |
tr("Unable to resolve program selection. This is " |
| 152 |
"a bug.")); |
| 153 |
return MoleQueue::JobObject(); |
| 154 |
} |
| 155 |
|
| 156 |
MoleQueue::JobObject job(m_jobTemplate); |
| 157 |
job.setQueue(queue); |
| 158 |
job.setProgram(program); |
| 159 |
job.setValue("numberOfCores", m_ui->numberOfCores->value()); |
| 160 |
job.setValue("cleanRemoteFiles", m_ui->cleanRemoteFiles->isChecked()); |
| 161 |
job.setValue("hideFromGui", m_ui->hideFromGui->isChecked()); |
| 162 |
job.setValue("popupOnStateChange", m_ui->popupOnStateChange->isChecked()); |
| 163 |
|
| 164 |
return job; |
| 165 |
} |
| 166 |
|
| 167 |
void MoleQueueWidget::setBatchMode(bool batch) |
| 168 |
{ |
| 169 |
m_ui->openOutput->setHidden(batch); |
| 170 |
m_ui->openOutputLabel->setHidden(batch); |
| 171 |
} |
| 172 |
|
| 173 |
bool MoleQueueWidget::batchMode() const |
| 174 |
{ |
| 175 |
return m_ui->openOutput->isHidden(); |
| 176 |
} |
| 177 |
|
| 178 |
void MoleQueueWidget::showAndSelectProgramHandler() |
| 179 |
{ |
| 180 |
MoleQueueManager& mqManager = MoleQueueManager::instance(); |
| 181 |
const QString program(property("selectProgramName").toString()); |
| 182 |
setProperty("selectProgramName", QVariant()); |
| 183 |
disconnect(&mqManager, SIGNAL(queueListUpdated()), this, |
| 184 |
SLOT(showAndSelectProgramHandler())); |
| 185 |
|
| 186 |
// Get all program nodes that match the name |
| 187 |
QModelIndexList list(mqManager.queueListModel().findProgramIndices(program)); |
| 188 |
|
| 189 |
// Expand the corresponding queues |
| 190 |
foreach (const QModelIndex& mi, list) |
| 191 |
m_ui->queueListView->expand(mi.parent()); |
| 192 |
|
| 193 |
// Select the first entry |
| 194 |
if (!list.isEmpty()) { |
| 195 |
m_ui->queueListView->selectionModel()->select( |
| 196 |
list.first(), QItemSelectionModel::ClearAndSelect); |
| 197 |
m_ui->queueListView->scrollTo(list.first()); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
void MoleQueueWidget::onLookupJobReply(int reqId, const QJsonObject& result) |
| 202 |
{ |
| 203 |
QVariant reqIdVariant(property("lookupJobRequestId")); |
| 204 |
bool ok; |
| 205 |
int myReqId = reqIdVariant.toInt(&ok); |
| 206 |
if (ok && reqId == myReqId) { |
| 207 |
setProperty("lookupJobRequestId", QVariant()); |
| 208 |
listenForLookupJobReply(false); |
| 209 |
MoleQueue::JobObject job; |
| 210 |
job.fromJson(result); |
| 211 |
emit jobUpdated(job); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
void MoleQueueWidget::onSubmissionSuccess(int localId, unsigned int mqId) |
| 216 |
{ |
| 217 |
if (localId != m_requestId) |
| 218 |
return; |
| 219 |
|
| 220 |
listenForJobSubmitReply(false); |
| 221 |
m_moleQueueId = mqId; |
| 222 |
emit jobSubmitted(true); |
| 223 |
} |
| 224 |
|
| 225 |
void MoleQueueWidget::onSubmissionFailure(int localId, unsigned int, |
| 226 |
const QString& error) |
| 227 |
{ |
| 228 |
if (localId != m_requestId) |
| 229 |
return; |
| 230 |
|
| 231 |
listenForJobSubmitReply(false); |
| 232 |
m_submissionError = error; |
| 233 |
emit jobSubmitted(false); |
| 234 |
} |
| 235 |
|
| 236 |
void MoleQueueWidget::onJobStateChange(unsigned int mqId, const QString&, |
| 237 |
const QString& newState) |
| 238 |
{ |
| 239 |
if (mqId != m_moleQueueId) |
| 240 |
return; |
| 241 |
|
| 242 |
m_jobState = newState; |
| 243 |
|
| 244 |
if (m_jobState == QLatin1String("Finished")) { |
| 245 |
listenForJobStateChange(false); |
| 246 |
emit jobFinished(true); |
| 247 |
} else if (m_jobState == QLatin1String("Error") || |
| 248 |
m_jobState == QLatin1String("Canceled")) { |
| 249 |
listenForJobStateChange(false); |
| 250 |
emit jobFinished(false); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
void MoleQueueWidget::listenForLookupJobReply(bool listen) |
| 255 |
{ |
| 256 |
Client& mqClient(MoleQueueManager::instance().client()); |
| 257 |
if (listen) { |
| 258 |
connect(&mqClient, SIGNAL(lookupJobResponse(int, QJsonObject)), this, |
| 259 |
SLOT(onLookupJobReply(int, QJsonObject))); |
| 260 |
} else { |
| 261 |
disconnect(&mqClient, SIGNAL(lookupJobResponse(int, QJsonObject)), this, |
| 262 |
SLOT(onLookupJobReply(int, QJsonObject))); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
void MoleQueueWidget::listenForJobSubmitReply(bool listen) |
| 267 |
{ |
| 268 |
MoleQueue::Client& mqClient(MoleQueueManager::instance().client()); |
| 269 |
|
| 270 |
if (listen) { |
| 271 |
connect(&mqClient, SIGNAL(submitJobResponse(int, uint)), this, |
| 272 |
SLOT(onSubmissionSuccess(int, uint))); |
| 273 |
connect(&mqClient, SIGNAL(errorReceived(int, uint, QString)), this, |
| 274 |
SLOT(onSubmissionFailure(int, uint, QString))); |
| 275 |
} else { |
| 276 |
disconnect(&mqClient, SIGNAL(submitJobResponse(int, uint)), this, |
| 277 |
SLOT(onSubmissionSuccess(int, uint))); |
| 278 |
disconnect(&mqClient, SIGNAL(errorReceived(int, uint, QString)), this, |
| 279 |
SLOT(onSubmissionFailure(int, uint, QString))); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
void MoleQueueWidget::listenForJobStateChange(bool listen) |
| 284 |
{ |
| 285 |
MoleQueue::Client& mqClient(MoleQueueManager::instance().client()); |
| 286 |
|
| 287 |
if (listen) { |
| 288 |
connect(&mqClient, SIGNAL(jobStateChanged(uint, QString, QString)), this, |
| 289 |
SLOT(onJobStateChange(uint, QString, QString))); |
| 290 |
} else { |
| 291 |
disconnect(&mqClient, SIGNAL(jobStateChanged(uint, QString, QString)), this, |
| 292 |
SLOT(onJobStateChange(uint, QString, QString))); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
bool MoleQueueWidget::programSelected() |
| 297 |
{ |
| 298 |
QModelIndexList sel(m_ui->queueListView->selectionModel()->selectedIndexes()); |
| 299 |
return sel.size() > 0; |
| 300 |
} |
| 301 |
|
| 302 |
} // namespace Avogadro::MoleQueue |