use of org.eclipse.che.api.core.NotFoundException in project che by eclipse.
the class WorkspaceRuntimes method snapshotAndUpdateStatus.
/** Creates a snapshot and changes status SNAPSHOTTING -> RUNNING. */
private void snapshotAndUpdateStatus(String workspaceId) throws NotFoundException, ConflictException, ServerException {
eventsService.publish(DtoFactory.newDto(WorkspaceStatusEvent.class).withWorkspaceId(workspaceId).withStatus(WorkspaceStatus.SNAPSHOTTING).withEventType(EventType.SNAPSHOT_CREATING).withPrevStatus(WorkspaceStatus.RUNNING));
WorkspaceRuntimeImpl runtime = getRuntime(workspaceId);
List<MachineImpl> machines = runtime.getMachines();
machines.sort(comparing(m -> !m.getConfig().isDev(), Boolean::compare));
LOG.info("Creating snapshot of workspace '{}', machines to snapshot: '{}'", workspaceId, machines.size());
List<SnapshotImpl> newSnapshots = new ArrayList<>(machines.size());
for (MachineImpl machine : machines) {
try {
newSnapshots.add(envEngine.saveSnapshot(workspaceId, machine.getId()));
} catch (ServerException | NotFoundException x) {
if (machine.getConfig().isDev()) {
compareAndSetStatus(workspaceId, WorkspaceStatus.SNAPSHOTTING, WorkspaceStatus.RUNNING);
eventsService.publish(DtoFactory.newDto(WorkspaceStatusEvent.class).withWorkspaceId(workspaceId).withStatus(WorkspaceStatus.RUNNING).withEventType(EventType.SNAPSHOT_CREATION_ERROR).withPrevStatus(WorkspaceStatus.SNAPSHOTTING).withError(x.getMessage()));
throw x;
}
LOG.warn(format("Couldn't create snapshot of machine '%s:%s' in workspace '%s'", machine.getEnvName(), machine.getConfig().getName(), workspaceId));
}
}
LOG.info("Saving new snapshots metadata, workspace id '{}'", workspaceId);
try {
List<SnapshotImpl> removed = snapshotDao.replaceSnapshots(workspaceId, runtime.getActiveEnv(), newSnapshots);
if (!removed.isEmpty()) {
LOG.info("Removing old snapshots binaries, workspace id '{}', snapshots to remove '{}'", workspaceId, removed.size());
removeBinaries(removed);
}
} catch (SnapshotException x) {
LOG.error(format("Couldn't remove existing snapshots metadata for workspace '%s'", workspaceId), x);
LOG.info("Removing newly created snapshots, workspace id '{}', snapshots to remove '{}'", workspaceId, newSnapshots.size());
removeBinaries(newSnapshots);
compareAndSetStatus(workspaceId, WorkspaceStatus.SNAPSHOTTING, WorkspaceStatus.RUNNING);
eventsService.publish(DtoFactory.newDto(WorkspaceStatusEvent.class).withWorkspaceId(workspaceId).withStatus(WorkspaceStatus.RUNNING).withEventType(EventType.SNAPSHOT_CREATION_ERROR).withPrevStatus(WorkspaceStatus.SNAPSHOTTING).withError(x.getMessage()));
throw x;
}
compareAndSetStatus(workspaceId, WorkspaceStatus.SNAPSHOTTING, WorkspaceStatus.RUNNING);
eventsService.publish(DtoFactory.newDto(WorkspaceStatusEvent.class).withStatus(WorkspaceStatus.RUNNING).withWorkspaceId(workspaceId).withEventType(EventType.SNAPSHOT_CREATED).withPrevStatus(WorkspaceStatus.SNAPSHOTTING));
}
use of org.eclipse.che.api.core.NotFoundException in project che by eclipse.
the class WorkspaceManager method stopRunningWorkspacesNormally.
/**
* Stops all the running and starting workspaces - snapshotting them before if needed.
* Workspace stop operations executed asynchronously while the method waits
* for async task to finish.
*/
private void stopRunningWorkspacesNormally() throws InterruptedException {
if (runtimes.isAnyRunning()) {
// getting all the running or starting workspaces
ArrayList<WorkspaceImpl> runningOrStarting = new ArrayList<>();
for (String workspaceId : runtimes.getRuntimesIds()) {
try {
WorkspaceImpl workspace = workspaceDao.get(workspaceId);
workspace.setStatus(runtimes.getStatus(workspaceId));
if (workspace.getStatus() == WorkspaceStatus.RUNNING || workspace.getStatus() == WorkspaceStatus.STARTING) {
runningOrStarting.add(workspace);
}
} catch (NotFoundException | ServerException x) {
if (runtimes.hasRuntime(workspaceId)) {
LOG.error("Couldn't get the workspace '{}' while it's running, the occurred error: '{}'", workspaceId, x.getMessage());
}
}
}
// stopping them asynchronously
CountDownLatch stopLatch = new CountDownLatch(runningOrStarting.size());
for (WorkspaceImpl workspace : runningOrStarting) {
try {
stopAsync(workspace, null).whenComplete((res, ex) -> stopLatch.countDown());
} catch (Exception x) {
stopLatch.countDown();
if (runtimes.hasRuntime(workspace.getId())) {
LOG.warn("Couldn't stop the workspace '{}' normally, due to error: {}", workspace.getId(), x.getMessage());
}
}
}
// wait for stopping workspaces to complete
stopLatch.await();
}
}
use of org.eclipse.che.api.core.NotFoundException in project che by eclipse.
the class StackService method getIcon.
@GET
@Path("/{id}/icon")
@Produces("image/*")
@GenerateLink(rel = LINK_REL_GET_ICON)
@ApiOperation(value = "Get icon by stack id", notes = "This operation can be performed only by authorized user", response = byte[].class)
@ApiResponses({ @ApiResponse(code = 200, message = "The response contains requested image entity"), @ApiResponse(code = 403, message = "The user does not have access to get image entity"), @ApiResponse(code = 500, message = "Internal server error occurred") })
public Response getIcon(@ApiParam("The stack id") @PathParam("id") final String id) throws NotFoundException, ServerException, BadRequestException {
StackImpl stack = stackDao.getById(id);
if (stack == null) {
throw new NotFoundException("Stack with id '" + id + "' was not found.");
}
StackIcon image = stack.getStackIcon();
if (image == null) {
throw new NotFoundException("Image for stack with id '" + id + "' was not found.");
}
return Response.ok(image.getData(), image.getMediaType()).build();
}
use of org.eclipse.che.api.core.NotFoundException in project che by eclipse.
the class JpaWorkspaceDao method doUpdate.
@Transactional
protected WorkspaceImpl doUpdate(WorkspaceImpl update) throws NotFoundException {
EntityManager manager = managerProvider.get();
if (manager.find(WorkspaceImpl.class, update.getId()) == null) {
throw new NotFoundException(format("Workspace with id '%s' doesn't exist", update.getId()));
}
if (update.getConfig() != null) {
update.getConfig().getProjects().forEach(ProjectConfigImpl::prePersistAttributes);
}
WorkspaceImpl merged = manager.merge(update);
manager.flush();
return merged;
}
use of org.eclipse.che.api.core.NotFoundException 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