From 3df08566ce8ae085a6d785bf47b411f27247f55c Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Mon, 16 Oct 2017 17:15:50 +0200 Subject: [PATCH] SONAR-8146 Upgrade form does not fail if server stops --- server/sonar-web/src/main/js/api/system.ts | 10 ++- .../src/main/js/apps/maintenance/main-view.js | 72 ++++++++++--------- .../templates/_maintenance-status-offline.hbs | 5 ++ .../templates/maintenance-main.hbs | 34 +++++---- .../resources/org/sonar/l10n/core.properties | 2 + 5 files changed, 76 insertions(+), 47 deletions(-) create mode 100644 server/sonar-web/src/main/js/apps/maintenance/templates/_maintenance-status-offline.hbs diff --git a/server/sonar-web/src/main/js/api/system.ts b/server/sonar-web/src/main/js/api/system.ts index 18851e79091..a98d8bdff8a 100644 --- a/server/sonar-web/src/main/js/api/system.ts +++ b/server/sonar-web/src/main/js/api/system.ts @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import { getJSON, post } from '../helpers/request'; +import { getJSON, post, postJSON } from '../helpers/request'; import throwGlobalError from '../app/utils/throwGlobalError'; export type SysValue = boolean | string | number | HealthType | SysValueObject | SysValueArray; @@ -70,6 +70,14 @@ export function getSystemStatus(): Promise { return getJSON('/api/system/status'); } +export function getMigrationStatus(): Promise { + return getJSON('/api/system/db_migration_status'); +} + +export function migrateDatabase() { + return postJSON('/api/system/migrate_db'); +} + export function restart(): Promise { return post('/api/system/restart').catch(throwGlobalError); } diff --git a/server/sonar-web/src/main/js/apps/maintenance/main-view.js b/server/sonar-web/src/main/js/apps/maintenance/main-view.js index d3bf4f8c64c..7a0ebd4afc4 100644 --- a/server/sonar-web/src/main/js/apps/maintenance/main-view.js +++ b/server/sonar-web/src/main/js/apps/maintenance/main-view.js @@ -17,10 +17,10 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import $ from 'jquery'; -import Backbone from 'backbone'; import Marionette from 'backbone.marionette'; import Template from './templates/maintenance-main.hbs'; +import { getSystemStatus, getMigrationStatus, migrateDatabase } from '../../api/system'; +import { getBaseUrl } from '../../helpers/urls'; export default Marionette.ItemView.extend({ template: Template, @@ -30,33 +30,40 @@ export default Marionette.ItemView.extend({ }, initialize() { - this.requestOptions = { - type: 'GET', - url: window.baseUrl + '/api/system/' + (this.options.setup ? 'db_migration_status' : 'status') - }; this.pollingInternal = setInterval(() => { this.refresh(); }, 5000); this.wasStarting = false; }, + getStatus() { + return this.options.setup ? getMigrationStatus() : getSystemStatus(); + }, + refresh() { - return Backbone.ajax(this.requestOptions).done(r => { - if (r.status === 'STARTING') { - this.wasStarting = true; - } - this.model.set(r); - this.render(); - if (this.model.get('status') === 'UP' || this.model.get('state') === 'NO_MIGRATION') { - this.stopPolling(); - } - if (this.model.get('status') === 'UP' && this.wasStarting) { - this.loadPreviousPage(); + return this.getStatus().then( + r => { + if (r.status === 'STARTING') { + this.wasStarting = true; + } + // unset `status` in case if was `OFFLINE` previously + this.model.set({ status: undefined, ...r }); + this.render(); + if (this.model.get('status') === 'UP' || this.model.get('state') === 'NO_MIGRATION') { + this.stopPolling(); + } + if (this.model.get('status') === 'UP' && this.wasStarting) { + this.loadPreviousPage(); + } + if (this.model.get('state') === 'MIGRATION_SUCCEEDED') { + this.loadPreviousPage(); + } + }, + () => { + this.model.set({ status: 'OFFLINE' }); + this.render(); } - if (this.model.get('state') === 'MIGRATION_SUCCEEDED') { - this.loadPreviousPage(); - } - }); + ); }, stopPolling() { @@ -64,25 +71,24 @@ export default Marionette.ItemView.extend({ }, startMigration() { - Backbone.ajax({ - url: window.baseUrl + '/api/system/migrate_db', - type: 'POST' - }).done(r => { - this.model.set(r); - this.render(); - }); + migrateDatabase().then( + r => { + this.model.set(r); + this.render(); + }, + () => {} + ); }, onRender() { - $('.page-simple').toggleClass( - 'panel-warning', - this.model.get('state') === 'MIGRATION_REQUIRED' - ); + document + .querySelector('.page-simple') + .classList.toggle('panel-warning', this.model.get('state') === 'MIGRATION_REQUIRED'); }, loadPreviousPage() { setInterval(() => { - window.location = this.options.returnTo || window.baseUrl; + window.location = this.options.returnTo || getBaseUrl(); }, 2500); }, diff --git a/server/sonar-web/src/main/js/apps/maintenance/templates/_maintenance-status-offline.hbs b/server/sonar-web/src/main/js/apps/maintenance/templates/_maintenance-status-offline.hbs new file mode 100644 index 00000000000..6e7bac35c3f --- /dev/null +++ b/server/sonar-web/src/main/js/apps/maintenance/templates/_maintenance-status-offline.hbs @@ -0,0 +1,5 @@ +

{{t 'maintenance.sonarqube_is_offline'}}

+

{{t 'maintenance.sonarqube_is_offline.text'}}

+

+ {{t 'maintenance.try_again'}} +

diff --git a/server/sonar-web/src/main/js/apps/maintenance/templates/maintenance-main.hbs b/server/sonar-web/src/main/js/apps/maintenance/templates/maintenance-main.hbs index 6b660cf1841..3a7f14e72a3 100644 --- a/server/sonar-web/src/main/js/apps/maintenance/templates/maintenance-main.hbs +++ b/server/sonar-web/src/main/js/apps/maintenance/templates/maintenance-main.hbs @@ -1,18 +1,26 @@ -{{#unless setup}} +{{#eq status 'OFFLINE'}} - {{#eq status 'UP'}}{{> '_maintenance-status-up'}}{{/eq}} - {{#eq status 'STARTING'}}{{> '_maintenance-status-starting'}}{{/eq}} - {{#eq status 'DOWN'}}{{> '_maintenance-status-down'}}{{/eq}} - {{#eq status 'DB_MIGRATION_NEEDED'}}{{> '_maintenance-status-migration'}}{{/eq}} - {{#eq status 'DB_MIGRATION_RUNNING'}}{{> '_maintenance-status-migration'}}{{/eq}} + {{> '_maintenance-status-offline'}} {{else}} - {{#eq state 'NO_MIGRATION'}}{{> '_maintenance-state-no-migration'}}{{/eq}} - {{#eq state 'MIGRATION_REQUIRED'}}{{> '_maintenance-state-migration-required'}}{{/eq}} - {{#eq state 'NOT_SUPPORTED'}}{{> '_maintenance-state-migration-not-supported'}}{{/eq}} - {{#eq state 'MIGRATION_RUNNING'}}{{> '_maintenance-state-migration-running'}}{{/eq}} - {{#eq state 'MIGRATION_SUCCEEDED'}}{{> '_maintenance-state-migration-succeeded'}}{{/eq}} - {{#eq state 'MIGRATION_FAILED'}}{{> '_maintenance-state-migration-failed'}}{{/eq}} + {{#unless setup}} -{{/unless}} + {{#eq status 'UP'}}{{> '_maintenance-status-up'}}{{/eq}} + {{#eq status 'STARTING'}}{{> '_maintenance-status-starting'}}{{/eq}} + {{#eq status 'DOWN'}}{{> '_maintenance-status-down'}}{{/eq}} + {{#eq status 'DB_MIGRATION_NEEDED'}}{{> '_maintenance-status-migration'}}{{/eq}} + {{#eq status 'DB_MIGRATION_RUNNING'}}{{> '_maintenance-status-migration'}}{{/eq}} + + {{else}} + + {{#eq state 'NO_MIGRATION'}}{{> '_maintenance-state-no-migration'}}{{/eq}} + {{#eq state 'MIGRATION_REQUIRED'}}{{> '_maintenance-state-migration-required'}}{{/eq}} + {{#eq state 'NOT_SUPPORTED'}}{{> '_maintenance-state-migration-not-supported'}}{{/eq}} + {{#eq state 'MIGRATION_RUNNING'}}{{> '_maintenance-state-migration-running'}}{{/eq}} + {{#eq state 'MIGRATION_SUCCEEDED'}}{{> '_maintenance-state-migration-succeeded'}}{{/eq}} + {{#eq state 'MIGRATION_FAILED'}}{{> '_maintenance-state-migration-failed'}}{{/eq}} + + {{/unless}} + +{{/eq}} diff --git a/sonar-core/src/main/resources/org/sonar/l10n/core.properties b/sonar-core/src/main/resources/org/sonar/l10n/core.properties index 43cf2a8e953..724b0dd0bf0 100644 --- a/sonar-core/src/main/resources/org/sonar/l10n/core.properties +++ b/sonar-core/src/main/resources/org/sonar/l10n/core.properties @@ -2641,3 +2641,5 @@ maintenance.sonarqube_is_under_maintenance.2=If you are an administrator and hav maintenance.sonarqube_is_starting=SonarQube is starting maintenance.sonarqube_is_up=SonarQube is up maintenance.all_systems_opetational=All systems operational. +maintenance.sonarqube_is_offline=SonarQube is offline +maintenance.sonarqube_is_offline.text=The connection to SonarQube is lost. Please contact your system administrator. -- 2.39.5