added a "Cancel" button to interrupt the server configuration process when it is found that it is busy installing other software

This commit is contained in:
vladimir.kuznetsov 2023-01-02 17:32:27 +03:00
parent 08a8eadb49
commit 6ec090ea0d
17 changed files with 193 additions and 48 deletions

View file

@ -31,6 +31,7 @@ enum ErrorCode
ServerPortAlreadyAllocatedError,
ServerContainerMissingError,
ServerDockerFailedError,
ServerCancelInstallation,
// Ssh connection errors
SshSocketError, SshTimeoutError, SshProtocolError,

View file

@ -15,6 +15,7 @@ QString errorString(ErrorCode code){
case(ServerPortAlreadyAllocatedError): return QObject::tr("Server port already used. Check for another software");
case(ServerContainerMissingError): return QObject::tr("Server error: Docker container missing");
case(ServerDockerFailedError): return QObject::tr("Server error: Docker failed");
case(ServerCancelInstallation): return QObject::tr("Installation canceled by user");
// Ssh connection errors
case(SshSocketError): return QObject::tr("Ssh connection error");

View file

@ -530,10 +530,13 @@ ErrorCode ServerController::installDockerWorker(const ServerCredentials &credent
stdOut += data + "\n";
};
QFutureWatcher<void> watcher;
QFutureWatcher<ErrorCode> watcher;
QFuture<void> future = QtConcurrent::run([this, &stdOut, &cbReadStdOut, &cbReadStdErr, &credentials]() {
QFuture<ErrorCode> future = QtConcurrent::run([this, &stdOut, &cbReadStdOut, &cbReadStdErr, &credentials]() {
do {
if (m_cancelInstallation) {
return ErrorCode::ServerCancelInstallation;
}
stdOut.clear();
runScript(credentials,
replaceVars(amnezia::scriptData(SharedScriptType::check_server_is_busy),
@ -543,16 +546,22 @@ ErrorCode ServerController::installDockerWorker(const ServerCredentials &credent
QThread::msleep(1000);
}
} while (!stdOut.isEmpty());
return ErrorCode::NoError;
});
watcher.setFuture(future);
QEventLoop wait;
QObject::connect(&watcher, &QFutureWatcher<void>::finished, &wait, &QEventLoop::quit);
QObject::connect(&watcher, &QFutureWatcher<ErrorCode>::finished, &wait, &QEventLoop::quit);
wait.exec();
m_cancelInstallation = false;
emit serverIsBusy(false);
if (future.result() != ErrorCode::NoError) {
return future.result();
}
ErrorCode error = runScript(credentials,
replaceVars(amnezia::scriptData(SharedScriptType::install_docker),
genVarsForScript(credentials)), cbReadStdOut, cbReadStdErr);
@ -820,6 +829,11 @@ SshConnection *ServerController::connectToHost(const SshConnectionParameters &ss
return client;
}
void ServerController::setCancelInstallation(const bool cancel)
{
m_cancelInstallation = cancel;
}
void ServerController::disconnectFromHost(const ServerCredentials &credentials)
{
SshConnection *client = acquireConnection(sshParams(credentials));

View file

@ -73,6 +73,7 @@ public:
QString checkSshConnection(const ServerCredentials &credentials, ErrorCode *errorCode = nullptr);
QSsh::SshConnection *connectToHost(const QSsh::SshConnectionParameters &sshParams);
void setCancelInstallation(const bool cancel);
private:
ErrorCode installDockerWorker(const ServerCredentials &credentials, DockerContainer container);
@ -85,6 +86,7 @@ private:
std::shared_ptr<Settings> m_settings;
std::shared_ptr<VpnConfigurator> m_configurator;
bool m_cancelInstallation = false;
signals:
void serverIsBusy(const bool isBusy);
};