use of org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl in project che by eclipse.
the class WorkspaceServiceTest method stateOfWsAgentShouldBeChecked.
@Test
public void stateOfWsAgentShouldBeChecked() throws Exception {
final WorkspaceImpl workspace = createWorkspace(createConfigDto());
workspace.setStatus(RUNNING);
WsAgentHealthStateDto wsAgentState = newDto(WsAgentHealthStateDto.class);
WorkspaceRuntimeImpl runtime = mock(WorkspaceRuntimeImpl.class);
MachineImpl machine = mock(MachineImpl.class);
when(runtime.getDevMachine()).thenReturn(machine);
when(wsAgentHealthChecker.check(machine)).thenReturn(wsAgentState);
workspace.setRuntime(runtime);
when(wsManager.getWorkspace(workspace.getId())).thenReturn(workspace);
final Response response = given().auth().basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD).when().get(SECURE_PATH + "/workspace/" + workspace.getId() + "/check");
verify(wsAgentHealthChecker).check(machine);
assertEquals(RUNNING, wsAgentState.getWorkspaceStatus());
assertEquals(200, response.getStatusCode());
}
use of org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl in project che by eclipse.
the class WorkspaceManagerTest method shouldBeAbleToStopMachine.
@Test
public void shouldBeAbleToStopMachine() throws Exception {
// given
final WorkspaceImpl workspace = createAndMockWorkspace();
WorkspaceRuntimeImpl runtime = mockRuntime(workspace, RUNNING);
MachineImpl machine = runtime.getMachines().get(0);
// when
workspaceManager.stopMachine(workspace.getId(), machine.getId());
// then
verify(runtimes).stopMachine(workspace.getId(), machine.getId());
}
use of org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl in project che by eclipse.
the class WorkspaceRuntimes method injectRuntime.
/**
* Injects runtime information such as status and {@link WorkspaceRuntimeImpl}
* into the workspace object, if the workspace doesn't have runtime sets the
* status to {@link WorkspaceStatus#STOPPED}.
*
* @param workspace
* the workspace to inject runtime into
*/
public void injectRuntime(WorkspaceImpl workspace) {
requireNonNull(workspace, "Required non-null workspace");
RuntimeState state = null;
try (@SuppressWarnings("unused") Unlocker u = locks.readLock(workspace.getId())) {
if (states.containsKey(workspace.getId())) {
state = new RuntimeState(states.get(workspace.getId()));
}
}
if (state == null) {
workspace.setStatus(WorkspaceStatus.STOPPED);
} else {
workspace.setStatus(state.status);
try {
workspace.setRuntime(new WorkspaceRuntimeImpl(state.envName, envEngine.getMachines(workspace.getId())));
} catch (Exception x) {
workspace.setRuntime(new WorkspaceRuntimeImpl(state.envName, Collections.emptyList()));
}
}
}
use of org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl in project che by eclipse.
the class WorkspaceRuntimes method startAsync.
/**
* Asynchronously starts the environment of the workspace.
* Before executing start task checks whether all conditions
* are met and throws appropriate exceptions if not, so
* there is no way to start the same workspace twice.
*
* <p>Note that cancellation of resulting future won't
* interrupt workspace start, call {@link #stop(String)} directly instead.
*
* <p>If starting process is interrupted let's say within call
* to {@link #stop(String)} method, resulting future will
* be exceptionally completed(eventually) with an instance of
* {@link EnvironmentStartInterruptedException}. Note that clients
* don't have to cleanup runtime resources, the component
* will do necessary cleanup when interrupted.
*
* <p>Implementation notes:
* if thread which executes the task is interrupted, then the
* task is also eventually(depends on the environment engine implementation)
* interrupted as if {@link #stop(String)} is called directly.
* That helps to shutdown gracefully when thread pool is asked
* to {@link ExecutorService#shutdownNow()} and also reduces
* shutdown time when there are starting workspaces.
*
* @param workspace
* workspace containing target environment
* @param envName
* the name of the environment to start
* @param recover
* whether to recover from the snapshot
* @return completable future describing the instance of running environment
* @throws ConflictException
* when the workspace is already started
* @throws ConflictException
* when workspaces start refused {@link #refuseWorkspacesStart()} was called
* @throws ServerException
* when any other error occurs
* @throws IllegalArgumentException
* when the workspace doesn't contain the environment
* @throws NullPointerException
* when either {@code workspace} or {@code envName} is null
*/
public CompletableFuture<WorkspaceRuntimeImpl> startAsync(Workspace workspace, String envName, boolean recover) throws ConflictException, ServerException {
requireNonNull(workspace, "Non-null workspace required");
requireNonNull(envName, "Non-null environment name required");
EnvironmentImpl environment = copyEnv(workspace, envName);
String workspaceId = workspace.getId();
CompletableFuture<WorkspaceRuntimeImpl> cmpFuture;
StartTask startTask;
try (@SuppressWarnings("unused") Unlocker u = locks.writeLock(workspaceId)) {
checkIsNotTerminated("start the workspace");
if (isStartRefused.get()) {
throw new ConflictException(format("Start of the workspace '%s' is rejected by the system, " + "no more workspaces are allowed to start", workspace.getConfig().getName()));
}
RuntimeState state = states.get(workspaceId);
if (state != null) {
throw new ConflictException(format("Could not start workspace '%s' because its status is '%s'", workspace.getConfig().getName(), state.status));
}
startTask = new StartTask(workspaceId, envName, environment, recover, cmpFuture = new CompletableFuture<>());
states.put(workspaceId, new RuntimeState(WorkspaceStatus.STARTING, envName, startTask, sharedPool.submit(startTask)));
}
// publish event synchronously as the task may not be executed by
// executors service(due to legal cancellation), clients still have
// to receive STOPPED -> STARTING event
eventsService.publish(DtoFactory.newDto(WorkspaceStatusEvent.class).withWorkspaceId(workspaceId).withStatus(WorkspaceStatus.STARTING).withEventType(EventType.STARTING).withPrevStatus(WorkspaceStatus.STOPPED));
// so the start thread is free to go and start the environment
startTask.unlockStart();
return cmpFuture;
}
use of org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl in project che by eclipse.
the class WorkspaceRuntimes method getRuntime.
/**
* Gets workspace runtime descriptor.
*
* @param workspaceId
* the id of the workspace to get its runtime
* @return descriptor which describes current state of the workspace runtime
* @throws NotFoundException
* when workspace with given {@code workspaceId} is not found
* @throws ServerException
* if any error occurs while getting machines runtime information
*/
public WorkspaceRuntimeImpl getRuntime(String workspaceId) throws NotFoundException, ServerException {
requireNonNull(workspaceId, "Required non-null workspace id");
RuntimeState state;
try (@SuppressWarnings("unused") Unlocker u = locks.readLock(workspaceId)) {
state = new RuntimeState(getExistingState(workspaceId));
}
return new WorkspaceRuntimeImpl(state.envName, envEngine.getMachines(workspaceId));
}
Aggregations