Search in sources :

Example 11 with WorkspaceRuntimeImpl

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());
}
Also used : Response(com.jayway.restassured.response.Response) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) WorkspaceRuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl) WsAgentHealthStateDto(org.eclipse.che.api.workspace.shared.dto.WsAgentHealthStateDto) Test(org.testng.annotations.Test)

Example 12 with WorkspaceRuntimeImpl

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());
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) WorkspaceRuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl) Test(org.testng.annotations.Test)

Example 13 with WorkspaceRuntimeImpl

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()));
        }
    }
}
Also used : Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) WorkspaceRuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl) AgentException(org.eclipse.che.api.agent.server.exception.AgentException) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) CancellationException(java.util.concurrent.CancellationException) EnvironmentException(org.eclipse.che.api.environment.server.exception.EnvironmentException) ConflictException(org.eclipse.che.api.core.ConflictException) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) NotFoundException(org.eclipse.che.api.core.NotFoundException) SnapshotException(org.eclipse.che.api.machine.server.exception.SnapshotException) ServerException(org.eclipse.che.api.core.ServerException)

Example 14 with WorkspaceRuntimeImpl

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;
}
Also used : ConflictException(org.eclipse.che.api.core.ConflictException) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) WorkspaceStatusEvent(org.eclipse.che.api.workspace.shared.dto.event.WorkspaceStatusEvent) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) WorkspaceRuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl)

Example 15 with WorkspaceRuntimeImpl

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));
}
Also used : Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) WorkspaceRuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl)

Aggregations

WorkspaceRuntimeImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl)21 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)16 Test (org.testng.annotations.Test)16 MachineImpl (org.eclipse.che.api.machine.server.model.impl.MachineImpl)7 ExtendedMachineImpl (org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl)6 EnvironmentStartInterruptedException (org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException)4 CancellationException (java.util.concurrent.CancellationException)3 NoOpMachineInstance (org.eclipse.che.api.environment.server.NoOpMachineInstance)3 EnvironmentException (org.eclipse.che.api.environment.server.exception.EnvironmentException)3 MachineConfigImpl (org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl)3 Instance (org.eclipse.che.api.machine.server.spi.Instance)3 EnvironmentImpl (org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl)3 Unlocker (org.eclipse.che.commons.lang.concurrent.Unlocker)3 Response (com.jayway.restassured.response.Response)2 HashSet (java.util.HashSet)2 AgentException (org.eclipse.che.api.agent.server.exception.AgentException)2 ConflictException (org.eclipse.che.api.core.ConflictException)2 ServerException (org.eclipse.che.api.core.ServerException)2 Matchers.anyString (org.mockito.Matchers.anyString)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1