Skip to content

Commit

Permalink
Do not fetch runtimes when getting list of all workspaces (eclipse-ch…
Browse files Browse the repository at this point in the history
…e#3557)

* Do not fetch runtimes when getting list of all workspaces
  • Loading branch information
Michail Kuznetsov authored Jan 10, 2017
1 parent d85a753 commit 93fd6c6
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ export class CreateProjectController {
* @param workspace existing workspace
*/
checkExistingWorkspaceState(workspace: any): void {
if (workspace.runtime) {
if (workspace.status === 'RUNNING') {
let websocketUrl = this.cheAPI.getWorkspace().getWebsocketUrl(workspace.id);
// get bus
let websocketStream = this.$websocket(websocketUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public List<WorkspaceImpl> getWorkspaces(String user) throws ServerException {
requireNonNull(user, "Required non-null user id");
final List<WorkspaceImpl> workspaces = workspaceDao.getWorkspaces(user);
for (WorkspaceImpl workspace : workspaces) {
normalizeState(workspace);
workspace.setStatus(runtimes.getStatus(workspace.getId()));
}
return workspaces;
}
Expand All @@ -258,7 +258,7 @@ public List<WorkspaceImpl> getByNamespace(String namespace) throws ServerExcepti
requireNonNull(namespace, "Required non-null namespace");
final List<WorkspaceImpl> workspaces = workspaceDao.getByNamespace(namespace);
for (WorkspaceImpl workspace : workspaces) {
normalizeState(workspace);
workspace.setStatus(runtimes.getStatus(workspace.getId()));
}
return workspaces;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,23 @@ public Map<String, WorkspaceState> getWorkspaces() {
return new HashMap<>(workspaces);
}

/**
* Return status of the workspace.
*
* @param workspaceId
* ID of requested workspace
* @return workspace status
*/
public WorkspaceStatus getStatus(String workspaceId) {
try (@SuppressWarnings("unused") CloseableLock l = locks.acquireReadLock(workspaceId)) {
final WorkspaceState state = workspaces.get(workspaceId);
if (state == null) {
return WorkspaceStatus.STOPPED;
}
return state.status;
}
}

private MessageConsumer<MachineLogMessage> getEnvironmentLogger(String workspaceId) throws ServerException {
WebsocketMessageConsumer<MachineLogMessage> envMessageConsumer =
new WebsocketMessageConsumer<>(format(ENVIRONMENT_OUTPUT_CHANNEL_TEMPLATE, workspaceId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ public void shouldBeAbleToGetWorkspacesAvailableForUser() throws Exception {

when(workspaceDao.getWorkspaces(NAMESPACE)).thenReturn(asList(workspace1, workspace2));
final RuntimeDescriptor descriptor = createDescriptor(workspace2, RUNNING);
when(runtimes.get(workspace2.getId())).thenReturn(descriptor);
when(runtimes.get(workspace1.getId())).thenThrow(new NotFoundException("no runtime"));
when(runtimes.getStatus(workspace2.getId())).thenReturn(RUNNING);
when(runtimes.getStatus(workspace1.getId())).thenReturn(STOPPED);

// when
final List<WorkspaceImpl> result = workspaceManager.getWorkspaces(NAMESPACE);
Expand Down Expand Up @@ -856,6 +856,7 @@ private RuntimeDescriptor createAndMockDescriptor(WorkspaceImpl workspace, Works
throws ServerException, NotFoundException, ConflictException {
RuntimeDescriptor descriptor = createDescriptor(workspace, status);
when(runtimes.get(workspace.getId())).thenReturn(descriptor);
when(runtimes.getStatus(workspace.getId())).thenReturn(status);
return descriptor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.eclipse.che.api.core.model.workspace.WorkspaceStatus.RUNNING;
import static org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STOPPED;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyString;
Expand Down Expand Up @@ -519,6 +520,38 @@ public void shouldBeAbleToGetMachine() throws Exception {
verify(envEngine).getMachine(WORKSPACE_ID, expected.getId());
}

@Test
public void shouldBeAbleToGetStatusOfRunningWorkspace() throws Exception {
// given
WorkspaceImpl workspace = createWorkspace();
runtimes.start(workspace,
workspace.getConfig().getDefaultEnv(),
false);

// when
WorkspaceStatus status = runtimes.getStatus(workspace.getId());

// then
assertEquals(status, RUNNING);
}


@Test
public void shouldBeAbleToGetStatusOfStoppedWorkspace() throws Exception {
// given
WorkspaceImpl workspace = createWorkspace();
runtimes.start(workspace,
workspace.getConfig().getDefaultEnv(),
false);
runtimes.stop(workspace.getId());

// when
WorkspaceStatus status = runtimes.getStatus(workspace.getId());

// then
assertEquals(status, STOPPED);
}

@Test(expectedExceptions = NotFoundException.class,
expectedExceptionsMessageRegExp = "test exception")
public void shouldThrowExceptionIfGetMachineFromEnvEngineThrowsException() throws Exception {
Expand Down

0 comments on commit 93fd6c6

Please sign in to comment.