Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not fetch runtimes when getting list of all workspaces #3557

Merged
merged 13 commits into from
Jan 10, 2017
Original file line number Diff line number Diff line change
@@ -1046,7 +1046,7 @@ export class CreateProjectController {
* @param workspace existing workspace
*/
checkExistingWorkspaceState(workspace: any): void {
if (workspace.runtime) {
if (workspace.status === 'RUNNING') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (workspace.status === 'RUNNING')

is not the same to

if (workspace.runtime)

it looks like

if (workspace.status !== 'STOPPED')

is the correct option here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'RUNNING' is fine here, cause wait for this status to start importing projects

let websocketUrl = this.cheAPI.getWorkspace().getWebsocketUrl(workspace.id);
// get bus
let websocketStream = this.$websocket(websocketUrl);
Original file line number Diff line number Diff line change
@@ -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;
}
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests

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));
Original file line number Diff line number Diff line change
@@ -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);
@@ -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;
}

Original file line number Diff line number Diff line change
@@ -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;
@@ -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 {