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);
}
}
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;
}
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;
}
}
Aggregations