bool SgeQueueInterface::startJob(Structure *s) { SSHConnection *ssh = m_opt->ssh()->getFreeConnection(); if (ssh == NULL) { m_opt->warning(tr("Cannot connect to ssh server")); return false; } QWriteLocker wlocker (s->lock()); QString command = "cd \"" + s->getRempath() + "\" && " + m_qsub + " job.sh"; QString stdout_str; QString stderr_str; int ec; if (!ssh->execute(command, stdout_str, stderr_str, ec) || ec != 0) { m_opt->warning(tr("Error executing %1: %2") .arg(command).arg(stderr_str)); m_opt->ssh()->unlockConnection(ssh); return false; } m_opt->ssh()->unlockConnection(ssh); // Assuming stdout_str value is // your job <jobID> ('job.sh') has been submitted unsigned int jobID = stdout_str.split(QRegExp("\\s+"))[2].toUInt(); s->setJobID(jobID); s->startOptTimer(); return true; }
void SSHManagerCLITest::lockAllAndExecute() { QList<SSHConnection*> list; for (int i = 0; i < NUM_CONN; i++) { list.append(manager->getFreeConnection()); } QString command = "expr 2 + 4"; QString stdout_str, stderr_str; SSHConnection* conn; int ec; for (int i = 0; i < NUM_CONN; i++) { conn = list.at(i); QVERIFY(conn->execute(command, stdout_str, stderr_str, ec)); QCOMPARE(ec, 0); QCOMPARE(stdout_str, QString("6\n")); QVERIFY(stderr_str.isEmpty()); } for (int i = 0; i < NUM_CONN; i++) { manager->unlockConnection(list.at(i)); } }
bool SgeQueueInterface::stopJob(Structure *s) { SSHConnection *ssh = m_opt->ssh()->getFreeConnection(); if (ssh == NULL) { m_opt->warning(tr("Cannot connect to ssh server")); return false; } // lock structure QWriteLocker locker (s->lock()); // jobid has not been set, cannot delete! if (s->getJobID() == 0) { if (m_cleanRemoteOnStop) { this->cleanRemoteDirectory(s, ssh); } m_opt->ssh()->unlockConnection(ssh); return true; } const QString command = m_qdel + " " + QString::number(s->getJobID()); // Execute QString stdout_str; QString stderr_str; int ec; bool ret = true; if (!ssh->execute(command, stdout_str, stderr_str, ec) || ec != 0) { // Most likely job is already gone from queue. ret = false; } s->setJobID(0); s->stopOptTimer(); m_opt->ssh()->unlockConnection(ssh); return ret; }
QStringList SgeQueueInterface::getQueueList() const { // recast queue mutex as mutable for safe access: QReadWriteLock &queueMutex = const_cast<QReadWriteLock&> (m_queueMutex); queueMutex.lockForRead(); // Limit queries to once per second if (m_queueTimeStamp.isValid() && // QDateTime::msecsTo is not implemented until Qt 4.7 #if QT_VERSION >= 0x040700 m_queueTimeStamp.msecsTo(QDateTime::currentDateTime()) <= 1000*m_interval #else // Check if day is the same. If not, refresh. Otherwise check // msecsTo current time (m_queueTimeStamp.date() == QDate::currentDate() && m_queueTimeStamp.time().msecsTo(QTime::currentTime()) <= 1000*m_interval) #endif // QT_VERSION >= 4.7 ) { // If the cache is valid, return it QStringList ret (m_queueData); queueMutex.unlock(); return ret; } // Otherwise, store a copy of the current timestamp and switch // queuemutex to writelock QDateTime oldTimeStamp (m_queueTimeStamp); queueMutex.unlock(); // Relock mutex QWriteLocker queueLocker (&queueMutex); // Non-fatal assert: Check current timestamp against the // oldTimeStamp from earlier. If they don't match, another thread // has already updated the queueData, so tail-recurse this // function and try again. if (m_queueTimeStamp.time().msecsTo(oldTimeStamp.time()) != 0) { queueLocker.unlock(); return this->getQueueList(); } // Queue will be updated -- cast queue cache and timestamp as // mutable QStringList &queueData = const_cast<QStringList&> (m_queueData); QDateTime &queueTimeStamp = const_cast<QDateTime&> (m_queueTimeStamp); // Get SSH connection SSHConnection *ssh = m_opt->ssh()->getFreeConnection(); if (ssh == NULL) { m_opt->warning(tr("Cannot connect to ssh server")); queueTimeStamp = QDateTime::currentDateTime(); queueData.clear(); queueData << "CommError"; QStringList ret (m_queueData); return ret; } QString command = m_qstat + " | grep " + m_opt->username; // Execute QString stdout_str; QString stderr_str; int ec; // Valid exit codes for grep: (0) matches found, execution successful // (1) no matches found, execution successful // (2) execution unsuccessful if (!ssh->execute(command, stdout_str, stderr_str, ec) || (ec != 0 && ec != 1 ) ) { m_opt->ssh()->unlockConnection(ssh); m_opt->warning(tr("Error executing %1: (%2) %3\n\t" "Using cached queue data.") .arg(command) .arg(QString::number(ec)) .arg(stderr_str)); queueTimeStamp = QDateTime::currentDateTime(); QStringList ret (m_queueData); return ret; } m_opt->ssh()->unlockConnection(ssh); queueData = stdout_str.split("\n", QString::SkipEmptyParts); QStringList ret (m_queueData); queueTimeStamp = QDateTime::currentDateTime(); return ret; }