Search in sources :

Example 16 with Unlocker

use of org.eclipse.che.commons.lang.concurrent.Unlocker in project che by eclipse.

the class WorkspaceRuntimes method stop.

/**
     * Stops running workspace runtime.
     *
     * <p>Stops environment in an implementation specific way.
     * During the stop of the workspace its runtime is accessible with {@link WorkspaceStatus#STOPPING stopping} status.
     * Workspace may be stopped only if its status is {@link WorkspaceStatus#RUNNING}.
     *
     * @param workspaceId
     *         identifier of workspace which should be stopped
     * @throws NotFoundException
     *         when workspace with specified identifier is not running
     * @throws ServerException
     *         when any error occurs during workspace stopping
     * @throws ConflictException
     *         when running workspace status is different from {@link WorkspaceStatus#RUNNING}
     * @see CheEnvironmentEngine#stop(String)
     * @see WorkspaceStatus#STOPPING
     */
public void stop(String workspaceId) throws NotFoundException, ServerException, ConflictException, EnvironmentException {
    requireNonNull(workspaceId, "Required not-null workspace id");
    RuntimeState prevState;
    try (@SuppressWarnings("unused") Unlocker u = locks.writeLock(workspaceId)) {
        checkIsNotTerminated("stop the workspace");
        RuntimeState state = getExistingState(workspaceId);
        if (state.status != WorkspaceStatus.RUNNING && state.status != WorkspaceStatus.STARTING) {
            throw new ConflictException(format("Couldn't stop the workspace '%s' because its status is '%s'. " + "Workspace can be stopped only if it is 'RUNNING' or 'STARTING'", workspaceId, state.status));
        }
        prevState = new RuntimeState(state);
        state.status = WorkspaceStatus.STOPPING;
    }
    // workspace is running, stop normally
    if (prevState.status == WorkspaceStatus.RUNNING) {
        stopEnvironmentAndPublishEvents(workspaceId, WorkspaceStatus.RUNNING);
        return;
    }
    // interrupt workspace start thread
    prevState.startFuture.cancel(true);
    // if task wasn't called by executor service, then
    // no real machines were started but, the clients still
    // have to be notified about the workspace shut down
    StartTask startTask = prevState.startTask;
    if (startTask.markAsUsed()) {
        removeStateAndPublishStopEvents(workspaceId);
        prevState.startTask.earlyComplete();
        return;
    }
    // otherwise stop will be triggered by the start task, wait for it to finish
    try {
        startTask.await();
    } catch (EnvironmentStartInterruptedException ignored) {
    // environment start successfully interrupted
    } catch (InterruptedException x) {
        Thread.currentThread().interrupt();
        throw new ServerException("Interrupted while waiting for start task cancellation", x);
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException)

Example 17 with Unlocker

use of org.eclipse.che.commons.lang.concurrent.Unlocker in project che by eclipse.

the class WorkspaceRuntimes method startMachine.

/**
     * Starts machine in running workspace.
     *
     * @param workspaceId
     *         ID of workspace that owns machine
     * @param machineConfig
     *         config of machine that should be started
     * @return running machine
     * @throws ConflictException
     *         if environment is not running or conflicting machine already exists in the environment
     * @throws ConflictException
     *         if environment was stopped during start of machine
     * @throws ServerException
     *         if any other error occurs
     */
public Instance startMachine(String workspaceId, MachineConfig machineConfig) throws ServerException, ConflictException, NotFoundException, EnvironmentException {
    try (@SuppressWarnings("unused") Unlocker u = locks.readLock(workspaceId)) {
        getRunningState(workspaceId);
    }
    // Copy constructor makes deep copy of objects graph
    // which means that original values won't affect the values in used further in this class
    MachineConfigImpl machineConfigCopy = new MachineConfigImpl(machineConfig);
    List<String> agents = Collections.singletonList("org.eclipse.che.terminal");
    Instance instance = envEngine.startMachine(workspaceId, machineConfigCopy, agents);
    launchAgents(instance, agents);
    try (@SuppressWarnings("unused") Unlocker u = locks.writeLock(workspaceId)) {
        checkIsNotTerminated("start the machine");
        RuntimeState workspaceState = states.get(workspaceId);
        if (workspaceState == null || workspaceState.status != RUNNING) {
            try {
                envEngine.stopMachine(workspaceId, instance.getId());
            } catch (NotFoundException | ServerException | ConflictException e) {
                LOG.error(e.getLocalizedMessage(), e);
            }
            throw new ConflictException(format("Environment of workspace '%s' was stopped during start of  machine", workspaceId));
        }
    }
    return instance;
}
Also used : MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) ServerException(org.eclipse.che.api.core.ServerException) Instance(org.eclipse.che.api.machine.server.spi.Instance) ConflictException(org.eclipse.che.api.core.ConflictException) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) NotFoundException(org.eclipse.che.api.core.NotFoundException)

Example 18 with Unlocker

use of org.eclipse.che.commons.lang.concurrent.Unlocker in project che by eclipse.

the class CheEnvironmentEngine method saveSnapshot.

/**
     * Saves machine into snapshot.
     *
     * @param workspaceId
     *         ID of workspace that owns environment
     * @param machineId
     *         ID of machine to save
     * @return snapshot
     * @throws EnvironmentNotRunningException
     *         if environment of machine is not running
     * @throws NotFoundException
     *         if machine is not running
     * @throws ServerException
     *         if another error occurs
     */
public SnapshotImpl saveSnapshot(String workspaceId, String machineId) throws ServerException, NotFoundException {
    EnvironmentHolder environmentHolder;
    SnapshotImpl snapshot = null;
    Instance instance = null;
    try (@SuppressWarnings("unused") Unlocker u = stripedLocks.readLock(workspaceId)) {
        environmentHolder = environments.get(workspaceId);
        if (environmentHolder == null || environmentHolder.status != EnvStatus.RUNNING) {
            throw new EnvironmentNotRunningException(format("Environment '%s' is not running", workspaceId));
        }
        for (Instance machine : environmentHolder.machines) {
            if (machine.getId().equals(machineId)) {
                instance = machine;
                snapshot = SnapshotImpl.builder().generateId().setType(machine.getConfig().getType()).setWorkspaceId(machine.getWorkspaceId()).setDescription(machine.getEnvName()).setDev(machine.getConfig().isDev()).setEnvName(machine.getEnvName()).setMachineName(machine.getConfig().getName()).useCurrentCreationDate().build();
            }
        }
    }
    if (instance == null) {
        throw new NotFoundException(format("Machine with id '%s' is not found in environment of workspace '%s'", machineId, workspaceId));
    }
    try {
        MachineSource machineSource = instance.saveToSnapshot();
        snapshot.setMachineSource(new MachineSourceImpl(machineSource));
        return snapshot;
    } catch (ServerException e) {
        try {
            instance.getLogger().writeLine("Snapshot storing failed. " + e.getLocalizedMessage());
        } catch (IOException ignore) {
        }
        throw e;
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) Instance(org.eclipse.che.api.machine.server.spi.Instance) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) NotFoundException(org.eclipse.che.api.core.NotFoundException) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) IOException(java.io.IOException) MachineSource(org.eclipse.che.api.core.model.machine.MachineSource)

Aggregations

Unlocker (org.eclipse.che.commons.lang.concurrent.Unlocker)18 ServerException (org.eclipse.che.api.core.ServerException)11 ConflictException (org.eclipse.che.api.core.ConflictException)10 NotFoundException (org.eclipse.che.api.core.NotFoundException)8 EnvironmentNotRunningException (org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException)8 Instance (org.eclipse.che.api.machine.server.spi.Instance)8 EnvironmentStartInterruptedException (org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException)5 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)5 AgentException (org.eclipse.che.api.agent.server.exception.AgentException)4 EnvironmentException (org.eclipse.che.api.environment.server.exception.EnvironmentException)4 MachineConfigImpl (org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl)4 EnvironmentImpl (org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl)4 WorkspaceRuntimeImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl)4 ArrayList (java.util.ArrayList)3 CancellationException (java.util.concurrent.CancellationException)3 MachineConfig (org.eclipse.che.api.core.model.machine.MachineConfig)3 ExtendedMachine (org.eclipse.che.api.core.model.workspace.ExtendedMachine)3 CheServicesEnvironmentImpl (org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl)3 SnapshotException (org.eclipse.che.api.machine.server.exception.SnapshotException)3 SourceNotFoundException (org.eclipse.che.api.machine.server.exception.SourceNotFoundException)3