/** * Gets workspace status by its identifier. * * @param workspaceId workspace identifier */ public WorkspaceStatus getStatus(String workspaceId) { try (Unlocker ignored = lockService.readLock(workspaceId)) { final WorkspaceStatus status = statuses.get(workspaceId); return status != null ? status : STOPPED; } }
/** Returns a set of supported recipe types */ public Set<String> getSupportedRecipes() { return runtimes.getSupportedRecipes(); }
/** * Returns true if workspace was started and its status is {@link WorkspaceStatus#RUNNING * running}, {@link WorkspaceStatus#STARTING starting} or {@link WorkspaceStatus#STOPPING * stopping} - otherwise returns false. * * @param workspaceId workspace identifier to perform check * @return true if workspace is running, otherwise false */ public boolean hasRuntime(String workspaceId) { return statuses.get(workspaceId) != null; }
/** * 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); } }
private WorkspaceImpl normalizeState(WorkspaceImpl workspace, boolean includeRuntimes) throws ServerException { if (includeRuntimes) { runtimes.injectRuntime(workspace); } else { workspace.setStatus(runtimes.getStatus(workspace.getId())); } return workspace; }
/** * Blocks starting new workspaces and stops all that already running * * @throws InterruptedException */ @Override public void terminate() throws InterruptedException { Preconditions.checkState(runtimes.refuseStart()); WorkspaceStoppedEventsPropagator propagator = new WorkspaceStoppedEventsPropagator(); eventService.subscribe(propagator); try { stopRunningAndStartingWorkspacesAsync(); waitAllWorkspacesStopped(); sharedPool.shutdown(); } finally { eventService.unsubscribe(propagator); } try { workspaceRemover.shutdown(); } catch (Exception ignored) { } }
/** * Blocks starting new workspaces and waits until all workspaces that are currently in a * starting/stopping state to finish this process * * @throws InterruptedException * @throws UnsupportedOperationException */ @Override public void suspend() throws InterruptedException, UnsupportedOperationException { try { runtimeInfrastructure.getIdentities(); } catch (UnsupportedOperationException | InfrastructureException e) { throw new UnsupportedOperationException("Current infrastructure does not support suspend."); } Preconditions.checkState(runtimes.refuseStart()); WorkspaceSuspendedEventsPropagator propagator = new WorkspaceSuspendedEventsPropagator(); eventService.subscribe(propagator); try { waitAllWorkspacesRunningOrStopped(); sharedPool.shutdown(); } finally { eventService.unsubscribe(propagator); } try { workspaceRemover.shutdown(); } catch (Exception ignored) { } }
private WorkspaceDto asDtoWithLinksAndToken(Workspace workspace) throws ServerException { WorkspaceDto workspaceDto = asDto(workspace).withLinks(linksGenerator.genLinks(workspace, getServiceContext())); RuntimeDto runtimeDto = workspaceDto.getRuntime(); if (runtimeDto != null) { try { runtimeDto.setMachineToken(machineTokenProvider.getToken(workspace.getId())); } catch (MachineAccessForbidden e) { // set runtime to null since user doesn't have the required permissions workspaceDto.setRuntime(null); } catch (MachineTokenException e) { throw new ServerException(e.getMessage(), e); } } return workspaceDto; }
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()); } } } } }
@Override protected InternalEnvironment doCreate( @Nullable InternalRecipe recipe, Map<String, InternalMachineConfig> machines, List<Warning> warnings) throws InternalInfrastructureException { if (recipe != null) { throw new InternalInfrastructureException( "No environment factory doesn't accept non-null workspace recipes"); } return new NoEnvInternalEnvironment(); }
@PostConstruct void init() { subscribeAbnormalRuntimeStopListener(); recover(); }
private List<WorkspaceDto> withLinks(List<WorkspaceDto> workspaces) throws ServerException { for (WorkspaceDto workspace : workspaces) { workspace.setLinks(linksGenerator.genLinks(workspace, getServiceContext())); } return workspaces; }
private void subscribeAbnormalRuntimeStopListener() { eventService.subscribe(new AbnormalRuntimeStoppingListener()); eventService.subscribe(new AbnormalRuntimeStoppedListener()); }
private Workspace doUpdate(String id, Workspace update) throws BadRequestException, ConflictException, NotFoundException, ServerException { try { return workspaceManager.updateWorkspace(id, update); } catch (ValidationException x) { throw new BadRequestException(x.getMessage()); } }
/** * Returns true if there is at least one workspace active(it's status is different from {@link * WorkspaceStatus#STOPPED}), otherwise returns false. */ public boolean isAnyActive() { return !statuses.asMap().isEmpty(); }
@Override public void run() { if (!runtimes.isAnyInProgress()) { timer.cancel(); latch.countDown(); } } },
@Override public void run() { if (!runtimes.isAnyActive()) { timer.cancel(); latch.countDown(); } } },
private WorkspaceStoppedEventsPropagator() { this.totalRunning = runtimes.getActive().size(); this.currentlyStopped = new AtomicInteger(0); }
private void relativizeRecipeLinks(WorkspaceConfigDto config) { if (config != null) { Map<String, EnvironmentDto> environments = config.getEnvironments(); if (environments != null && !environments.isEmpty()) { for (EnvironmentDto environment : environments.values()) { relativizeRecipeLinks(environment); } } } }
/** * Gets the workspaces identifiers managed by this component. If an identifier is present in set * then that workspace wasn't stopped at the moment of method execution. * * @return workspaces identifiers for those workspaces that are active(not stopped), or an empty * set if there is no a single active workspace */ public Set<String> getActive() { return ImmutableSet.copyOf(statuses.asMap().keySet()); }