/** * 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(); }
/** * 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; }
throws NotFoundException, ConflictException { String workspaceId = workspace.getId(); WorkspaceStatus status = statuses.get(workspaceId); if (status == null) { throw new NotFoundException("Workspace with id '" + workspaceId + "' is not running."); format("Could not stop workspace '%s' because its state is '%s'", workspaceId, status)); if (!statuses.replace(workspaceId, status, STOPPING)) { WorkspaceStatus newStatus = statuses.get(workspaceId); throw new ConflictException( format(
@Override public void onEvent(RuntimeAbnormalStoppingEvent event) { RuntimeIdentity identity = event.getIdentity(); String workspaceId = identity.getWorkspaceId(); WorkspaceStatus previousStatus; try (Unlocker ignored = lockService.writeLock(workspaceId)) { previousStatus = statuses.replace(workspaceId, STOPPING); } if (previousStatus == null) { LOG.error( "Runtime '{}:{}:{}' became abnormally stopping but it was not considered as active before", workspaceId, identity.getEnvName(), identity.getOwnerId()); } LOG.info( "Runtime '{}:{}:{}' is stopping abnormally. Reason: {}", workspaceId, identity.getEnvName(), identity.getOwnerId(), event.getReason()); publishWorkspaceStatusEvent( workspaceId, STOPPING, previousStatus, "Workspace is going to be STOPPED. Reason: " + event.getReason()); } }
statuses.putIfAbsent(workspaceId, STARTING);
WorkspaceStatus runtimeStatus = runtime.getStatus(); try (Unlocker ignored = lockService.writeLock(workspace.getId())) { statuses.replace(identity.getWorkspaceId(), runtimeStatus); runtimes.putIfAbsent(identity.getWorkspaceId(), runtime);
final WorkspaceStatus existingStatus = statuses.putIfAbsent(workspaceId, STARTING); if (existingStatus != null) { throw new ConflictException(
/** * 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()); }
/** * 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; } }
try (Unlocker ignored = lockService.writeLock(workspaceId)) { runtimes.remove(workspaceId); previousStatus = statuses.remove(workspaceId);
/** Returns workspace ids which has {@link WorkspaceStatus#RUNNING} runtimes. */ public Set<String> getRunning() { return statuses .asMap() .entrySet() .stream() .filter(e -> RUNNING == e.getValue()) .map(Entry::getKey) .collect(toSet()); }
/** * 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); } }
/** * Gets the list of workspace id's which are currently starting or stopping on given node. (it's * status is {@link WorkspaceStatus#STARTING} or {@link WorkspaceStatus#STOPPING}) */ public Set<String> getInProgress() { return statuses .asMap() .entrySet() .stream() .filter(e -> STARTING == e.getValue() || STOPPING == e.getValue()) .map(Entry::getKey) .filter(this::containsThisRuntimesId) .collect(toSet()); }
/** * Returns true if there is at least one local workspace starting or stopping (it's status is * {@link WorkspaceStatus#STARTING} or {@link WorkspaceStatus#STOPPING}), otherwise returns false. */ public boolean isAnyInProgress() { return statuses .asMap() .entrySet() .stream() .filter(e -> STARTING == e.getValue() || STOPPING == e.getValue()) .map(Entry::getKey) .anyMatch(this::containsThisRuntimesId); }