private void stopRunningAndStartingWorkspacesAsync() { for (String workspaceId : runtimes.getActive()) { WorkspaceStatus status = runtimes.getStatus(workspaceId); if (status == WorkspaceStatus.RUNNING || status == WorkspaceStatus.STARTING) { try { manager.stopWorkspace(workspaceId, Collections.emptyMap()); } catch (ServerException | ConflictException | NotFoundException x) { if (runtimes.hasRuntime(workspaceId)) { LOG.error( "Couldn't get the workspace '{}' while it's running, the occurred error: '{}'", workspaceId, x.getMessage()); } } } } }
private WorkspaceSuspendedEventsPropagator() { this.totalRunning = runtimes.getInProgress().size(); this.currentlyStopped = new AtomicInteger(0); }
/** Returns a set of supported recipe types */ public Set<String> getSupportedRecipes() { return runtimes.getSupportedRecipes(); }
private WorkspaceImpl normalizeState(WorkspaceImpl workspace, boolean includeRuntimes) throws ServerException { if (includeRuntimes) { runtimes.injectRuntime(workspace); } else { workspace.setStatus(runtimes.getStatus(workspace.getId())); } return workspace; }
environment = copyEnv(workspace, envName); requireNonNull(environment, "Environment should not be null " + workspaceId); requireNonNull(environment.getRecipe(), "Recipe should not be null " + workspaceId); try { InternalEnvironment internalEnv = createInternalEnvironment( environment, workspaceConfig.getAttributes(), workspaceConfig.getCommands()); RuntimeContext runtimeContext = infrastructure.prepare(runtimeId, internalEnv); workspaceId, existingStatus)); setRuntimesId(workspaceId); runtimes.put(workspaceId, runtime); workspaceConfig.getName(), workspace.getId(), sessionUserNameOr("undefined")); publishWorkspaceStatusEvent(workspaceId, STARTING, STOPPED, null); return CompletableFuture.runAsync( ThreadLocalPropagateContext.wrap(new StartRuntimeTask(workspace, options, runtime)),
private void addRuntimeLinks( Map<String, String> links, String workspaceId, ServiceContext serviceContext) throws ServerException { URI uri = serviceContext.getServiceUriBuilder().build(); links.put( LINK_REL_ENVIRONMENT_STATUS_CHANNEL, UriBuilder.fromUri(cheWebsocketEndpoint) .scheme(uri.getScheme().equals("https") ? "wss" : "ws") .host(uri.getHost()) .port(uri.getPort()) .build() .toString()); Optional<RuntimeContext> ctxOpt = workspaceRuntimes.getRuntimeContext(workspaceId); if (ctxOpt.isPresent()) { try { links.put(LINK_REL_ENVIRONMENT_OUTPUT_CHANNEL, ctxOpt.get().getOutputChannel().toString()); } catch (UnsupportedOperationException e) { // Do not include output channel to links since it is not supported by context } catch (InfrastructureException x) { throw new ServerException(x.getMessage(), x); } } } }
/** * Injects runtime information such as status and {@link * org.eclipse.che.api.core.model.workspace.Runtime} 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) throws ServerException { try (Unlocker ignored = lockService.writeLock(workspace.getId())) { WorkspaceStatus workspaceStatus = statuses.get(workspace.getId()); if (workspaceStatus == null) { workspace.setStatus(STOPPED); return; } InternalRuntime<?> internalRuntime; try { internalRuntime = getInternalRuntime(workspace.getId()); } catch (ServerException | InfrastructureException e) { workspace.setStatus(STOPPED); return; } workspace.setRuntime(asRuntime(internalRuntime)); workspace.setStatus(workspaceStatus); } }
public void validate(Environment environment) throws NotFoundException, InfrastructureException, ValidationException { String type = environment.getRecipe().getType(); if (!infrastructure.getRecipeTypes().contains(type)) { throw new NotFoundException("Infrastructure not found for type: " + type); } // try to create internal environment to check if the specified environment is valid createInternalEnvironment(environment, emptyMap(), emptyList()); }
private WorkspaceStoppedEventsPropagator() { this.totalRunning = runtimes.getActive().size(); this.currentlyStopped = new AtomicInteger(0); }
/** * Removes workspace with specified identifier. * * <p> * * <p>Does not remove the workspace if it has the runtime, throws {@link ConflictException} in * this case. Won't throw any exception if workspace doesn't exist. * * @param workspaceId workspace id to remove workspace * @throws ConflictException when workspace has runtime * @throws ServerException when any server error occurs * @throws NullPointerException when {@code workspaceId} is null */ @Traced public void removeWorkspace(String workspaceId) throws ConflictException, ServerException { requireNonNull(workspaceId, "Required non-null workspace id"); if (runtimes.hasRuntime(workspaceId)) { throw new ConflictException( format("The workspace '%s' is currently running and cannot be removed.", workspaceId)); } workspaceDao.remove(workspaceId); LOG.info("Workspace '{}' removed by user '{}'", workspaceId, sessionUserNameOrUndefined()); }
@VisibleForTesting void removeTemporaryWs() throws ServerException { for (WorkspaceImpl workspace : Pages.iterate( (maxItems, skipCount) -> workspaceDao.getWorkspaces(true, maxItems, skipCount))) { WorkspaceStatus status = runtimes.getStatus(workspace.getId()); if (status == WorkspaceStatus.STOPPED) { try { workspaceDao.remove(workspace.getId()); } catch (ServerException e) { LOG.error( "Unable to cleanup temporary workspace {}. Reason is {}", workspace.getId(), e.getMessage()); } } } } }
InternalRuntime<?> runtime = null; try { runtime = getInternalRuntime(workspaceId);
@SuppressWarnings("rawtypes") Optional<RuntimeContext> context = workspaceRuntimeProvider.get().getRuntimeContext(workspaceId); workspaceOwnerId = context.map(c -> c.getIdentity().getOwnerId()).orElse(null);
try { InternalEnvironment internalEnv = createInternalEnvironment( environment, workspaceConfig.getAttributes(), workspaceConfig.getCommands()); runtime = infra.prepare(identity, internalEnv).getRuntime();