org.sonar.server.platform.ws.StatusAction Maven / Gradle / Ivy
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.ws;
import com.google.common.io.Resources;
import org.sonar.api.platform.Server;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.core.util.Protobuf;
import org.sonar.server.app.RestartFlagHolder;
import org.sonar.server.platform.Platform;
import org.sonar.server.platform.db.migration.DatabaseMigrationState;
import org.sonar.server.ws.WsUtils;
import org.sonarqube.ws.WsSystem;
/**
* Implementation of the {@code status} action for the System WebService.
*/
public class StatusAction implements SystemWsAction {
private final Server server;
private final DatabaseMigrationState migrationState;
private final Platform platform;
private final RestartFlagHolder restartFlagHolder;
public StatusAction(Server server, DatabaseMigrationState migrationState,
Platform platform, RestartFlagHolder restartFlagHolder) {
this.server = server;
this.migrationState = migrationState;
this.platform = platform;
this.restartFlagHolder = restartFlagHolder;
}
@Override
public void define(WebService.NewController controller) {
controller.createAction("status")
.setDescription("Get state information about SonarQube." +
"status: the running status" +
"
" +
" - STARTING: SonarQube Web Server is up and serving some Web Services (eg. api/system/status) " +
"but initialization is still ongoing
" +
" - UP: SonarQube instance is up and running
" +
" - DOWN: SonarQube instance is up but not running because " +
"migration has failed (refer to WS /api/system/migrate_db for details) or some other reason (check logs).
" +
" - RESTARTING: SonarQube instance is still up but a restart has been requested " +
"(refer to WS /api/system/restart for details).
" +
" - DB_MIGRATION_NEEDED: database migration is required. DB migration can be started using WS /api/system/migrate_db.
" +
" - DB_MIGRATION_RUNNING: DB migration is running (refer to WS /api/system/migrate_db for details)
" +
"
" +
"")
.setSince("5.2")
.setResponseExample(Resources.getResource(this.getClass(), "example-status.json"))
.setHandler(this);
}
@Override
public void handle(Request request, Response response) throws Exception {
WsSystem.StatusResponse.Builder protobuf = WsSystem.StatusResponse.newBuilder();
Protobuf.setNullable(server.getId(), protobuf::setId);
Protobuf.setNullable(server.getVersion(), protobuf::setVersion);
protobuf.setStatus(computeStatus());
WsUtils.writeProtobuf(protobuf.build(), request, response);
}
private WsSystem.Status computeStatus() {
Platform.Status platformStatus = platform.status();
switch (platformStatus) {
case BOOTING:
// can not happen since there can not even exist an instance of the current class
// unless the Platform's status is UP/SAFEMODE/STARTING
return WsSystem.Status.DOWN;
case UP:
return restartFlagHolder.isRestarting() ? WsSystem.Status.RESTARTING : WsSystem.Status.UP;
case STARTING:
return computeStatusInStarting();
case SAFEMODE:
return computeStatusInSafemode();
default:
throw new IllegalArgumentException("Unsupported Platform.Status " + platformStatus);
}
}
private WsSystem.Status computeStatusInStarting() {
DatabaseMigrationState.Status databaseMigrationStatus = migrationState.getStatus();
switch (databaseMigrationStatus) {
case NONE:
return WsSystem.Status.STARTING;
case RUNNING:
return WsSystem.Status.DB_MIGRATION_RUNNING;
case FAILED:
return WsSystem.Status.DOWN;
case SUCCEEDED:
// DB migration can be finished while we haven't yet finished SQ's initialization
return WsSystem.Status.STARTING;
default:
throw new IllegalArgumentException("Unsupported DatabaseMigration.Status " + databaseMigrationStatus);
}
}
private WsSystem.Status computeStatusInSafemode() {
DatabaseMigrationState.Status databaseMigrationStatus = migrationState.getStatus();
switch (databaseMigrationStatus) {
case NONE:
return WsSystem.Status.DB_MIGRATION_NEEDED;
case RUNNING:
return WsSystem.Status.DB_MIGRATION_RUNNING;
case FAILED:
return WsSystem.Status.DOWN;
case SUCCEEDED:
return WsSystem.Status.STARTING;
default:
throw new IllegalArgumentException("Unsupported DatabaseMigration.Status " + databaseMigrationStatus);
}
}
}