From af04544218447141757cc7365aac8363b99cb9cf Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 20 Jul 2015 16:43:48 +0200 Subject: Give up after trying to connect for a while --- common/resourceaccess.cpp | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'common') diff --git a/common/resourceaccess.cpp b/common/resourceaccess.cpp index 78a5155..84c5dcb 100644 --- a/common/resourceaccess.cpp +++ b/common/resourceaccess.cpp @@ -113,11 +113,12 @@ KAsync::Job > ResourceAccess::connectToServer(const KAsync::Job ResourceAccess::Private::tryToConnect() { + auto counter = QSharedPointer::create(); + *counter = 0; return KAsync::dowhile([this]() -> bool { - //TODO abort after N retries? return !socket; }, - [this](KAsync::Future &future) { + [this, counter](KAsync::Future &future) { Trace() << "Loop"; KAsync::wait(50) .then(connectToServer(resourceInstanceIdentifier)) @@ -126,8 +127,15 @@ KAsync::Job ResourceAccess::Private::tryToConnect() socket = s; future.setFinished(); }, - [&future](int errorCode, const QString &errorString) { - future.setFinished(); + [&future, counter](int errorCode, const QString &errorString) { + const int maxRetries = 10; + if (*counter > maxRetries) { + Trace() << "Giving up"; + future.setError(-1, "Failed to connect to socket"); + } else { + future.setFinished(); + } + *counter = *counter + 1; }).exec(); }); } @@ -153,6 +161,9 @@ KAsync::Job ResourceAccess::Private::initializeSocket() tryToConnect() .then([&future]() { future.setFinished(); + }, [this, &future](int errorCode, const QString &errorString) { + Warning() << "Failed to connect to started resource"; + future.setError(errorCode, errorString); }).exec(); } else { Warning() << "Failed to start resource"; @@ -179,7 +190,7 @@ ResourceAccess::~ResourceAccess() { log("Closing access"); if (!d->resultHandler.isEmpty()) { - Warning() << "Left jobs running while shutting down ResourceAccess"; + Warning() << "Left jobs running while shutting down ResourceAccess: " << d->resultHandler.keys(); } } @@ -257,6 +268,9 @@ void ResourceAccess::open() QObject::connect(d->socket.data(), &QIODevice::readyRead, this, &ResourceAccess::readResourceMessage); connected(); + }, + [](int error, const QString &errorString) { + Warning() << "Failed to initialize socket " << errorString; }).exec(); } -- cgit v1.2.3