Daemon manager improvements

This commit is contained in:
Jaquee 2017-02-25 14:57:39 +01:00
parent ce10016408
commit 2ed59f41ed
4 changed files with 83 additions and 15 deletions

View file

@ -235,7 +235,6 @@ ApplicationWindow {
} }
function connectWallet(wallet) { function connectWallet(wallet) {
showProcessingSplash("Please wait...")
currentWallet = wallet currentWallet = wallet
updateSyncing(false) updateSyncing(false)
@ -341,9 +340,6 @@ ApplicationWindow {
function onWalletRefresh() { function onWalletRefresh() {
console.log(">>> wallet refreshed") console.log(">>> wallet refreshed")
if (splash.visible) {
hideProcessingSplash()
}
// Daemon connected // Daemon connected
leftPanel.networkStatus.connected = currentWallet.connected() leftPanel.networkStatus.connected = currentWallet.connected()
@ -398,6 +394,7 @@ ApplicationWindow {
console.log("daemon started"); console.log("daemon started");
daemonRunning = true; daemonRunning = true;
hideProcessingSplash(); hideProcessingSplash();
currentWallet.connected(true);
} }
function onDaemonStopped(){ function onDaemonStopped(){
console.log("daemon stopped"); console.log("daemon stopped");
@ -1233,6 +1230,7 @@ ApplicationWindow {
} }
onClosing: { onClosing: {
// Close wallet non async on exit // Close wallet non async on exit
daemonManager.exit();
walletManager.closeWallet(); walletManager.closeWallet();
} }

View file

@ -407,7 +407,7 @@ Rectangle {
console.log("Settings page loaded"); console.log("Settings page loaded");
initSettings(); initSettings();
viewOnly = currentWallet.viewOnly; viewOnly = currentWallet.viewOnly;
daemonManager.running(persistentSettings.testnet) appWindow.daemonRunning = daemonManager.running(persistentSettings.testnet)
} }
// fires only once // fires only once

View file

@ -67,11 +67,22 @@ bool DaemonManager::start(const QString &flags, bool testnet)
// add state changed listener // add state changed listener
connect(m_daemon,SIGNAL(stateChanged(QProcess::ProcessState)),this,SLOT(stateChanged(QProcess::ProcessState))); connect(m_daemon,SIGNAL(stateChanged(QProcess::ProcessState)),this,SLOT(stateChanged(QProcess::ProcessState)));
if (!started) { if (!started)
qDebug() << "Daemon start error: " + m_daemon->errorString(); qDebug() << "Daemon start error: " + m_daemon->errorString();
} else {
emit daemonStarted(); // Start start watcher
} QFuture<bool> future = QtConcurrent::run(this, &DaemonManager::startWatcher, testnet);
QFutureWatcher<bool> * watcher = new QFutureWatcher<bool>();
connect(watcher, &QFutureWatcher<bool>::finished,
this, [this, watcher]() {
QFuture<bool> future = watcher->future();
watcher->deleteLater();
if(future.result()) {
emit daemonStarted();
}
});
watcher->setFuture(future);
return started; return started;
} }
@ -79,13 +90,63 @@ bool DaemonManager::start(const QString &flags, bool testnet)
bool DaemonManager::stop(bool testnet) bool DaemonManager::stop(bool testnet)
{ {
QString message; QString message;
bool stopped = sendCommand("exit",testnet,message); sendCommand("exit",testnet,message);
qDebug() << message; qDebug() << message;
if(stopped)
emit daemonStopped(); // Start stop watcher - Will kill if not shutting down
return stopped; QFuture<bool> future = QtConcurrent::run(this, &DaemonManager::stopWatcher, testnet);
QFutureWatcher<bool> * watcher = new QFutureWatcher<bool>();
connect(watcher, &QFutureWatcher<bool>::finished,
this, [this, watcher]() {
QFuture<bool> future = watcher->future();
watcher->deleteLater();
if(future.result()) {
emit daemonStopped();
}
});
watcher->setFuture(future);
return true;
} }
bool DaemonManager::startWatcher(bool testnet) const
{
// Check if daemon is started every 2 seconds
while(true && !m_app_exit) {
QThread::sleep(2);
if(!running(testnet)) {
qDebug() << "daemon not running. checking again in 2 seconds.";
} else
return true;
}
return false;
}
bool DaemonManager::stopWatcher(bool testnet) const
{
// Check if daemon is running every 2 seconds. Kill if still running after 10 seconds
int counter = 0;
while(true && !m_app_exit) {
QThread::sleep(2);
counter++;
if(running(testnet)) {
qDebug() << "Daemon still running. " << counter;
if(counter >= 5) {
qDebug() << "Killing it! ";
#ifdef Q_OS_WIN
QProcess::execute("taskkill /F /IM monerod.exe");
#else
QProcess::execute("pkill monerod");
#endif
}
} else
return true;
}
return false;
}
void DaemonManager::stateChanged(QProcess::ProcessState state) void DaemonManager::stateChanged(QProcess::ProcessState state)
{ {
qDebug() << "STATE CHANGED: " << state; qDebug() << "STATE CHANGED: " << state;
@ -124,10 +185,8 @@ bool DaemonManager::running(bool testnet) const
// `./monerod status` returns BUSY when syncing. // `./monerod status` returns BUSY when syncing.
// Treat busy as connected, until fixed upstream. // Treat busy as connected, until fixed upstream.
if (status.contains("Height:") || status.contains("BUSY") ) { if (status.contains("Height:") || status.contains("BUSY") ) {
emit daemonStarted();
return true; return true;
} }
emit daemonStopped();
return false; return false;
} }
bool DaemonManager::sendCommand(const QString &cmd,bool testnet) const bool DaemonManager::sendCommand(const QString &cmd,bool testnet) const
@ -155,6 +214,12 @@ bool DaemonManager::sendCommand(const QString &cmd,bool testnet, QString &messag
return started; return started;
} }
void DaemonManager::exit()
{
qDebug("DaemonManager: exit()");
m_app_exit = true;
}
DaemonManager::DaemonManager(QObject *parent) DaemonManager::DaemonManager(QObject *parent)
: QObject(parent) : QObject(parent)
{ {

View file

@ -20,9 +20,13 @@ public:
Q_INVOKABLE bool running(bool testnet) const; Q_INVOKABLE bool running(bool testnet) const;
// Send daemon command from qml and prints output in console window. // Send daemon command from qml and prints output in console window.
Q_INVOKABLE bool sendCommand(const QString &cmd, bool testnet) const; Q_INVOKABLE bool sendCommand(const QString &cmd, bool testnet) const;
Q_INVOKABLE void exit();
private: private:
bool sendCommand(const QString &cmd, bool testnet, QString &message) const; bool sendCommand(const QString &cmd, bool testnet, QString &message) const;
bool startWatcher(bool testnet) const;
bool stopWatcher(bool testnet) const;
signals: signals:
void daemonStarted() const; void daemonStarted() const;
void daemonStopped() const; void daemonStopped() const;
@ -41,6 +45,7 @@ private:
bool initialized = false; bool initialized = false;
QString m_monerod; QString m_monerod;
bool m_has_daemon = true; bool m_has_daemon = true;
bool m_app_exit = false;
}; };