dash/src/qt/initexecutor.cpp

86 lines
2.4 KiB
C++
Raw Normal View History

// Copyright (c) 2014-2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <qt/initexecutor.h>
#include <interfaces/node.h>
#include <util/system.h>
#include <util/threadnames.h>
#include <exception>
#include <QApplication>
#include <QDebug>
#include <QObject>
#include <QProcess>
#include <QString>
#include <QThread>
InitExecutor::InitExecutor(interfaces::Node& node)
: QObject(), m_node(node)
{
this->moveToThread(&m_thread);
m_thread.start();
}
InitExecutor::~InitExecutor()
{
qDebug() << __func__ << ": Stopping thread";
m_thread.quit();
m_thread.wait();
qDebug() << __func__ << ": Stopped thread";
}
void InitExecutor::handleRunawayException(const std::exception_ptr e)
{
PrintExceptionContinue(e, "Runaway exception");
Q_EMIT runawayException(QString::fromStdString(m_node.getWarnings().translated));
}
void InitExecutor::initialize()
{
try {
util::ThreadRename("qt-init");
qDebug() << __func__ << ": Running initialization in thread";
interfaces::BlockAndHeaderTipInfo tip_info;
bool rv = m_node.appInitMain(&tip_info);
Q_EMIT initializeResult(rv, tip_info);
} catch (...) {
handleRunawayException(std::current_exception());
}
}
void InitExecutor::restart(QStringList args)
{
static bool executing_restart{false};
if(!executing_restart) { // Only restart 1x, no matter how often a user clicks on a restart-button
executing_restart = true;
try {
qDebug() << __func__ << ": Running Restart in thread";
m_node.appPrepareShutdown();
qDebug() << __func__ << ": Shutdown finished";
Q_EMIT shutdownResult();
CExplicitNetCleanup::callCleanup();
QProcess::startDetached(QApplication::applicationFilePath(), args);
qDebug() << __func__ << ": Restart initiated...";
QApplication::quit();
} catch (...) {
handleRunawayException(std::current_exception());
}
}
}
void InitExecutor::shutdown()
{
try {
qDebug() << __func__ << ": Running Shutdown in thread";
m_node.appShutdown();
qDebug() << __func__ << ": Shutdown finished";
Q_EMIT shutdownResult();
} catch (...) {
handleRunawayException(std::current_exception());
}
}