Refactor server request code by removing some duplicated code in the runtime and improves some of the debug logs.

This commit is contained in:
Dave Page 2020-07-11 16:59:26 +05:30 committed by Akshay Joshi
parent dd38fed6e0
commit e6edf40048
2 changed files with 27 additions and 57 deletions

View File

@ -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;
}

View File

@ -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