mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2025-02-25 18:55:31 -06:00
Refactor server request code by removing some duplicated code in the runtime and improves some of the debug logs.
This commit is contained in:
parent
dd38fed6e0
commit
e6edf40048
@ -389,7 +389,7 @@ int main(int argc, char * argv[])
|
|||||||
Logger::GetLogger()->Log("The server should be up, we'll attempt to connect and get a response. Ping the server");
|
Logger::GetLogger()->Log("The server should be up, we'll attempt to connect and get a response. Ping the server");
|
||||||
while(QTime::currentTime() <= endTime)
|
while(QTime::currentTime() <= endTime)
|
||||||
{
|
{
|
||||||
alive = PingServer(QUrl(appServerUrl));
|
alive = pingServer(QUrl(appServerUrl));
|
||||||
|
|
||||||
if (alive)
|
if (alive)
|
||||||
{
|
{
|
||||||
@ -412,7 +412,7 @@ int main(int argc, char * argv[])
|
|||||||
|
|
||||||
// Attempt to connect one more time in case of a long network timeout while looping
|
// Attempt to connect one more time in case of a long network timeout while looping
|
||||||
Logger::GetLogger()->Log("Attempt to connect one more time in case of a long network timeout while looping");
|
Logger::GetLogger()->Log("Attempt to connect one more time in case of a long network timeout while looping");
|
||||||
if (!alive && !PingServer(QUrl(appServerUrl)))
|
if (!alive && !pingServer(QUrl(appServerUrl)))
|
||||||
{
|
{
|
||||||
splash->finish(Q_NULLPTR);
|
splash->finish(Q_NULLPTR);
|
||||||
QString error(QWidget::tr("The application server could not be contacted."));
|
QString error(QWidget::tr("The application server could not be contacted."));
|
||||||
@ -474,20 +474,20 @@ int main(int argc, char * argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Ping the application server to see if it's alive
|
QString serverRequest(QUrl url, QString path)
|
||||||
bool PingServer(QUrl url)
|
|
||||||
{
|
{
|
||||||
QNetworkAccessManager manager;
|
QNetworkAccessManager manager;
|
||||||
QEventLoop loop;
|
QEventLoop loop;
|
||||||
QNetworkReply *reply;
|
QNetworkReply *reply;
|
||||||
QVariant redirectUrl;
|
QVariant redirectUrl;
|
||||||
|
|
||||||
url.setPath("/misc/ping");
|
|
||||||
|
url.setPath(path);
|
||||||
|
QString requestUrl = url.toString();
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
reply = manager.get(QNetworkRequest(url));
|
reply = manager.get(QNetworkRequest(url));
|
||||||
|
|
||||||
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
|
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
|
||||||
loop.exec();
|
loop.exec();
|
||||||
|
|
||||||
@ -501,18 +501,28 @@ bool PingServer(QUrl url)
|
|||||||
|
|
||||||
if (reply->error() != QNetworkReply::NoError)
|
if (reply->error() != QNetworkReply::NoError)
|
||||||
{
|
{
|
||||||
return false;
|
qDebug() << "Failed to connect to the server:" << reply->errorString() << "( request URL:" << requestUrl << ").";
|
||||||
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString response = reply->readAll();
|
QString response = reply->readAll();
|
||||||
|
qDebug() << "Server response:" << response << "( request URL:" << requestUrl << ").";
|
||||||
|
|
||||||
if (response != "PING")
|
return response;
|
||||||
{
|
}
|
||||||
qDebug() << "Failed to connect, server response: " << response;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
|
// Ping the application server to see if it's alive
|
||||||
|
bool pingServer(QUrl url)
|
||||||
|
{
|
||||||
|
return serverRequest(url, "/misc/ping") == "PING";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Shutdown the application server
|
||||||
|
bool shutdownServer(QUrl url)
|
||||||
|
{
|
||||||
|
return serverRequest(url, "/misc/shutdown") == "SHUTDOWN";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -548,44 +558,3 @@ unsigned long sdbm(unsigned char *str)
|
|||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shutdown the application server
|
|
||||||
bool shutdownServer(QUrl url)
|
|
||||||
{
|
|
||||||
QNetworkAccessManager manager;
|
|
||||||
QEventLoop loop;
|
|
||||||
QNetworkReply *reply;
|
|
||||||
QVariant redirectUrl;
|
|
||||||
|
|
||||||
url.setPath("/misc/shutdown");
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
reply = manager.get(QNetworkRequest(url));
|
|
||||||
|
|
||||||
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
|
|
||||||
loop.exec();
|
|
||||||
|
|
||||||
redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
|
|
||||||
url = redirectUrl.toUrl();
|
|
||||||
|
|
||||||
if (!redirectUrl.isNull())
|
|
||||||
delete reply;
|
|
||||||
|
|
||||||
} while (!redirectUrl.isNull());
|
|
||||||
|
|
||||||
if (reply->error() != QNetworkReply::NoError)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString response = reply->readAll();
|
|
||||||
|
|
||||||
if (response != "SHUTDOWN")
|
|
||||||
{
|
|
||||||
qDebug() << "Failed to connect, server response: " << response;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
#ifndef PGADMIN4_H
|
#ifndef PGADMIN4_H
|
||||||
#define PGADMIN4_H
|
#define PGADMIN4_H
|
||||||
|
|
||||||
// Include the Python header here as it needs to appear before any QT
|
// Include the Python header here as it needs to appear before any QT
|
||||||
// headers anywhere in the app.
|
// headers anywhere in the app.
|
||||||
#ifdef __MINGW32__
|
#ifdef __MINGW32__
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
@ -35,10 +35,11 @@ const QString PGA_APP_NAME = QString("pgAdmin 4");
|
|||||||
|
|
||||||
// Global function prototypes
|
// Global function prototypes
|
||||||
int main(int argc, char * argv[]);
|
int main(int argc, char * argv[]);
|
||||||
bool PingServer(QUrl url);
|
QString serverRequest(QUrl url, QString path);
|
||||||
|
bool pingServer(QUrl url);
|
||||||
|
bool shutdownServer(QUrl url);
|
||||||
void delay(int milliseconds);
|
void delay(int milliseconds);
|
||||||
void cleanup();
|
void cleanup();
|
||||||
unsigned long sdbm(unsigned char *str);
|
unsigned long sdbm(unsigned char *str);
|
||||||
bool shutdownServer(QUrl url);
|
|
||||||
|
|
||||||
#endif // PGADMIN4_H
|
#endif // PGADMIN4_H
|
||||||
|
Loading…
Reference in New Issue
Block a user