Skip to content

Commit

Permalink
Fix eclipse-che#3988 : graceful stop should work with any system state (
Browse files Browse the repository at this point in the history
eclipse-che#4063)

- RUNNING -> call stop and wait for system to be in READY_TO_SHUTDOWN state
  - READY_TO_SHUTDOWN -> do nothing(maybe print something useful)
  - ANY_OTHER_STATE -> wait for system to be in READY_TO_SHUTDOWN state

Change-Id: I55a8b5ecb2b016d31dbbd95b8793af4952d8116b
Signed-off-by: Florent BENOIT <fbenoit@codenvy.com>
  • Loading branch information
benoitf authored Feb 9, 2017
1 parent b6a056f commit 5c7e9a9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
66 changes: 47 additions & 19 deletions dockerfiles/lib/src/api/wsmaster/system/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export class System {
websocketLink = stateLink.getHref();
}
});

return this.websocket.getMessageBus(websocketLink + '?token=' + this.authData.getToken());
}

Expand All @@ -73,34 +72,63 @@ export class System {
* Stop the server and return a promise that will wait for the ready to shutdown event
*/
gracefulStop():Promise<org.eclipse.che.api.system.shared.dto.SystemStateDto> {

let currentSystemStateDto : org.eclipse.che.api.system.shared.dto.SystemStateDto;
let callbackSubscriber : SystemStopEventPromiseMessageBusSubscriber;

// get workspace DTO
return this.getState().then((systemStateDto: org.eclipse.che.api.system.shared.dto.SystemStateDto) => {
currentSystemStateDto = systemStateDto;
return this.getMessageBus(systemStateDto);
}).then((messageBus: MessageBus) => {
if ('READY_TO_SHUTDOWN' === systemStateDto.getStatus()) {
return this.readyTobeShutdown(systemStateDto);
} else if ('RUNNING' === systemStateDto.getStatus()) {
// call stop and wait
return this.shutdown(systemStateDto, true);
} else {
// only wait as stop has been called
return this.shutdown(systemStateDto, false);
}
});
}



/**
* In that case, system is already in a state ready to be shutdown so we do nothing
* @returns {Promise<string>}
*/
readyTobeShutdown(currentSystemStateDto: org.eclipse.che.api.system.shared.dto.SystemStateDto) : Promise<org.eclipse.che.api.system.shared.dto.SystemStateDto> {
return Promise.resolve(currentSystemStateDto);
}

/**
* We want or we're already in a shutdown process so we need to wait the end of the action
* @param systemStateDto the current state
* @param callStop if true, we need to call the stop action, else we only listen to the stop process event
* @returns {Promise<string>}
*/
shutdown(systemStateDto: org.eclipse.che.api.system.shared.dto.SystemStateDto, callStop: boolean) : Promise<org.eclipse.che.api.system.shared.dto.SystemStateDto> {
let callbackSubscriber : SystemStopEventPromiseMessageBusSubscriber;
return this.getMessageBus(systemStateDto).then((messageBus: MessageBus) => {
callbackSubscriber = new SystemStopEventPromiseMessageBusSubscriber(messageBus);
let channelToListen : string;
currentSystemStateDto.getLinks().forEach(stateLink => {
systemStateDto.getLinks().forEach(stateLink => {
if ('system.state.channel' === stateLink.getRel()) {
channelToListen = stateLink.getParameters()[0].getDefaultValue();
}
});
return messageBus.subscribeAsync(channelToListen, callbackSubscriber);
}).then((subscribed: string) => {
var jsonRequest:HttpJsonRequest = new DefaultHttpJsonRequest(this.authData, '/api/system/stop', 204).setMethod('POST');
return jsonRequest.request().then((jsonResponse:HttpJsonResponse) => {
return;
}).then(() => {
return callbackSubscriber.promise;
}).then(() => {
return this.getState();
});
if (callStop) {
var jsonRequest: HttpJsonRequest = new DefaultHttpJsonRequest(this.authData, '/api/system/stop', 204).setMethod('POST');
return jsonRequest.request().then((jsonResponse: HttpJsonResponse) => {
return;
}).then(() => {
return callbackSubscriber.promise;
}).then(() => {
return this.getState();
});
} else {
return callbackSubscriber.promise.then(() => {
return this.getState();
});
}
});


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,9 @@ export class GracefulStopAction {
run() : Promise<any> {
// first, login
return this.authData.login().then(() => {
return this.system.getState()
.then((state:org.eclipse.che.api.system.shared.dto.SystemStateDto) => {
if (state.getStatus() != "RUNNING") {
return Promise.reject("Status is not in RUNNING state so the graceful can't be called.");
}
});
}).then(() => {
return this.system.gracefulStop();
}).then((state:org.eclipse.che.api.system.shared.dto.SystemStateDto) => {
Log.getLogger().info("Success: system is now in status '" + state.getStatus() + "'.");
Log.getLogger().info("Success: System state '" + state.getStatus() + "'.");
});
}

Expand Down

0 comments on commit 5c7e9a9

Please sign in to comment.