Check server's 204 endpoint every minute and restore connection immidiately after...
authoralex-z <blackslayer4@gmail.com>
Wed, 19 Apr 2023 15:00:16 +0000 (17:00 +0200)
committerMatthieu Gallien <matthieu_gallien@yahoo.fr>
Wed, 26 Jul 2023 14:18:39 +0000 (16:18 +0200)
Signed-off-by: alex-z <blackslayer4@gmail.com>
src/gui/accountstate.cpp
src/gui/accountstate.h

index f5938ad9a415984df7174250d77660f751e25204..6bca4bff4a2b4377d6e610e80790b17dc33cfaa7 100644 (file)
@@ -72,6 +72,10 @@ AccountState::AccountState(AccountPtr account)
     _checkConnectionTimer.setInterval(ConnectionValidator::DefaultCallingIntervalMsec);
     _checkConnectionTimer.start();
 
+    connect(&_checkServerAvailibilityTimer, &QTimer::timeout, this, &AccountState::slotCheckServerAvailibility);
+    _checkServerAvailibilityTimer.setInterval(ConnectionValidator::DefaultCallingIntervalMsec);
+    _checkServerAvailibilityTimer.start();
+
     QTimer::singleShot(0, this, &AccountState::slotCheckConnection);
 }
 
@@ -557,6 +561,28 @@ void AccountState::slotCheckConnection()
     }
 }
 
+void AccountState::slotCheckServerAvailibility()
+{
+    if (state() == AccountState::Connected
+        || state() == AccountState::SignedOut
+        || state() == AccountState::MaintenanceMode
+        || state() == AccountState::AskingCredentials) {
+        qCInfo(lcAccountState) << "Skipping server availibility check for account" << _account->id() << "with state" << state();
+        return;
+    }
+    qCInfo(lcAccountState) << "Checking server availibility for account" << _account->id();
+    const auto serverAvailibilityUrl = Utility::concatUrlPath(_account->url(), QLatin1String("/index.php/204"));
+    auto checkServerAvailibilityJob = _account->sendRequest(QByteArrayLiteral("GET"), serverAvailibilityUrl);
+    connect(checkServerAvailibilityJob, &SimpleNetworkJob::finishedSignal, this, [this](QNetworkReply *reply) {
+        if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 204) {
+            qCInfo(lcAccountState) << "Server is now available for account" << _account->id();
+            _lastCheckConnectionTimer.invalidate();
+            resetRetryCount();
+            QMetaObject::invokeMethod(this, "slotCheckConnection", Qt::QueuedConnection);
+        }
+    });
+}
+
 void AccountState::slotPushNotificationsReady()
 {
     if (state() != AccountState::State::Connected) {
index fc6a4ab858fa728f0289bd3b2e4afccef965e691..ea4062735931a0637d2c708bbac502d243d957e8 100644 (file)
@@ -216,6 +216,7 @@ protected Q_SLOTS:
 private Q_SLOTS:
 
     void slotCheckConnection();
+    void slotCheckServerAvailibility();
     void slotPushNotificationsReady();
     void slotServerUserStatusChanged();
 
@@ -261,6 +262,8 @@ private:
     QTimer _checkConnectionTimer;
     QElapsedTimer _lastCheckConnectionTimer;
 
+    QTimer _checkServerAvailibilityTimer;
+
     explicit AccountState() = default;
 
     friend class ::FakeAccountState;