Handle slow startup of the web server.

If the web server is slow to start, we may need to retry the initial
page load. Implement a retry system that will retry the initial load
4 times, after 1, 2, 3 and 4 seconds, before giving up and leaving
the user with an error message and a link to retry manually.
This commit is contained in:
Dave Page 2013-06-22 00:49:40 +01:00
parent 7fd90ac10f
commit 9080a782d5
4 changed files with 62 additions and 4 deletions

View File

@ -30,6 +30,7 @@
// App headers // App headers
#include "BrowserWindow.h" #include "BrowserWindow.h"
// Constructor // Constructor
BrowserWindow::BrowserWindow() BrowserWindow::BrowserWindow()
{ {
@ -39,11 +40,15 @@ BrowserWindow::BrowserWindow()
// Create the WebKit control // Create the WebKit control
webView = new QWebView(this); webView = new QWebView(this);
setCentralWidget(webView); setCentralWidget(webView);
connect(webView, SIGNAL(loadFinished(bool)), SLOT(finishLoading(bool)));
// Display the app // Display the app
webView->setUrl(QString("http://127.0.0.1:8080")); m_initialload = true;
m_loadattempt = 1;
webView->setUrl(PGA_SERVER_URL);
} }
// Create the actions for the window // Create the actions for the window
void BrowserWindow::createActions() void BrowserWindow::createActions()
{ {
@ -65,6 +70,7 @@ void BrowserWindow::createActions()
connect(aboutAction, SIGNAL(triggered()), this, SLOT(about())); connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
} }
// Create the application menus // Create the application menus
void BrowserWindow::createMenus() void BrowserWindow::createMenus()
{ {
@ -81,12 +87,54 @@ void BrowserWindow::createMenus()
helpMenu->addAction(aboutAction); helpMenu->addAction(aboutAction);
} }
// Process loading finished signals from the web view.
void BrowserWindow::finishLoading(bool ok)
{
if (m_initialload && !ok)
{
// The load attempt failed. Try again up to 4 times with an
// incremental backoff.
if (m_loadattempt < 5)
{
qDebug() << "Initial load failed. Retrying in" << m_loadattempt << "seconds.";
webView->setHtml(QString(tr("<p>Failed to connect to the pgAdmin application server. Retrying in %1 seconds, ") +
tr("or click <a href=\"%2\">here</a> to try again now.</p>")).arg(m_loadattempt).arg(PGA_SERVER_URL));
pause(m_loadattempt);
webView->setUrl(PGA_SERVER_URL);
m_loadattempt++;
return;
}
else
{
qDebug() << "Initial page load failed after multiple attempts. Aborting.";
webView->setHtml(QString(tr("<p>Failed to connect to the pgAdmin application server. ") +
tr("Click <a href=\"%1\">here</a> to try again.</p>")).arg(PGA_SERVER_URL));
}
}
m_initialload = false;
}
// Pause for n seconds, without freezing the UI.
void BrowserWindow::pause(int seconds)
{
QTime dieTime = QTime::currentTime().addSecs(seconds);
while (QTime::currentTime() < dieTime)
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
// Display the about box // Display the about box
void BrowserWindow::about() void BrowserWindow::about()
{ {
QMessageBox::about(this, tr("About pgAdmin 4"), tr("pgAdmin 4 - PostgreSQL Tools")); QMessageBox::about(this, tr("About pgAdmin 4"), tr("pgAdmin 4 - PostgreSQL Tools"));
} }
// Open an arbitrary URL // Open an arbitrary URL
void BrowserWindow::openUrl() void BrowserWindow::openUrl()
{ {

View File

@ -36,6 +36,9 @@ class BrowserWindow : public QMainWindow
public: public:
BrowserWindow(); BrowserWindow();
protected slots:
void finishLoading(bool);
private slots: private slots:
void openUrl(); void openUrl();
void about(); void about();
@ -48,8 +51,12 @@ private:
QAction *exitAction; QAction *exitAction;
QAction *aboutAction; QAction *aboutAction;
bool m_initialload;
int m_loadattempt;
void createActions(); void createActions();
void createMenus(); void createMenus();
void pause(int seconds = 1);
}; };
#endif // BROWSERWINDOW_H #endif // BROWSERWINDOW_H

View File

@ -25,7 +25,7 @@
Server::Server() Server::Server()
{ {
// Initialise Python // Initialise Python
Py_SetProgramName(PGA_APPNAME.toUtf8().data()); Py_SetProgramName(PGA_APP_NAME.toUtf8().data());
Py_Initialize(); Py_Initialize();
} }

View File

@ -25,7 +25,10 @@
#include <QApplication> #include <QApplication>
#endif #endif
// char *PGA_APPNAME = "pgAdmin 4"; // Application name
const QString PGA_APPNAME = QString("pgAdmin 4"); const QString PGA_APP_NAME = QString("pgAdmin 4");
// Server URL
const QString PGA_SERVER_URL = QString("http://127.0.0.1:8080");
#endif // PGADMIN4_H #endif // PGADMIN4_H