private void handleStartupSuccess(Workspace workspace) { workspace.getAttributes().remove(STOPPED_ATTRIBUTE_NAME); workspace.getAttributes().remove(STOPPED_ABNORMALLY_ATTRIBUTE_NAME); workspace.getAttributes().remove(ERROR_MESSAGE_ATTRIBUTE_NAME); try { updateWorkspace(workspace.getId(), workspace); } catch (NotFoundException | ServerException | ValidationException | ConflictException e) { LOG.warn( String.format( "Cannot clear error status status of the workspace %s. Error is: %s", workspace.getId(), e.getMessage())); } }
private void handleStartupError(Workspace workspace, Throwable t) { workspace .getAttributes() .put( ERROR_MESSAGE_ATTRIBUTE_NAME, t instanceof RuntimeException ? t.getCause().getMessage() : t.getMessage()); workspace.getAttributes().put(STOPPED_ATTRIBUTE_NAME, Long.toString(currentTimeMillis())); workspace.getAttributes().put(STOPPED_ABNORMALLY_ATTRIBUTE_NAME, Boolean.toString(true)); try { updateWorkspace(workspace.getId(), workspace); } catch (NotFoundException | ServerException | ValidationException | ConflictException e) { LOG.warn( String.format( "Cannot set error status of the workspace %s. Error is: %s", workspace.getId(), e.getMessage())); } }
private static EnvironmentImpl copyEnv(Workspace workspace, String envName) { requireNonNull(workspace, "Workspace should not be null."); requireNonNull(envName, "Environment name should not be null."); final Environment environment = workspace.getConfig().getEnvironments().get(envName); if (environment == null) { throw new IllegalArgumentException( format("Workspace '%s' doesn't contain environment '%s'", workspace.getId(), envName)); } return new EnvironmentImpl(environment); }
/** * Ensure workspace has running status, or throw IllegalStateException. * * @param workspace workspace description to get status and id. * @throws IllegalStateException if workspace with certain workspaceId doesn't have RUNNING * status. */ @Override public void ensureRunningStatus(Workspace workspace) throws IllegalStateException { if (workspace.getStatus() != WorkspaceStatus.RUNNING) { throw new IllegalStateException( format( "Workspace with id='%s' should has '%s' status, but its actual state='%s'", workspace.getId(), WorkspaceStatus.RUNNING, workspace.getStatus())); } }
@Override @Nullable public String getId() throws ExecutionException, InterruptedException { if (future == null) { try { Workspace wsConfig = testWorkspaceServiceClient.getByName(name, owner.getName()); id.set(wsConfig.getId()); return id.get(); } catch (Exception e) { String errorMessage = format("Failed to obtain id of workspace name='%s' owner='%s'", name, owner.getName()); LOG.warn(errorMessage, e); return null; } } return future.thenApply(aVoid -> id.get()).get(); }
/** Returns 'rel -> url' map of links for the given workspace. */ public Map<String, String> genLinks(Workspace workspace, ServiceContext serviceContext) throws ServerException { final UriBuilder uriBuilder = serviceContext.getServiceUriBuilder(); final LinkedHashMap<String, String> links = new LinkedHashMap<>(); links.put( LINK_REL_SELF, uriBuilder .clone() .path(WorkspaceService.class, "getByKey") .build(workspace.getId()) .toString()); links.put( LINK_REL_IDE_URL, uriBuilder .clone() .replacePath("") .path(workspace.getNamespace()) .path(workspace.getConfig().getName()) .build() .toString()); if (workspace.getStatus() != WorkspaceStatus.STOPPED) { addRuntimeLinks(links, workspace.getId(), serviceContext); } return links; }
@Override public void onEvent(WorkspaceCreatedEvent event) { try { workerDao.store( new WorkerImpl( event.getWorkspace().getId(), EnvironmentContext.getCurrent().getSubject().getUserId(), new ArrayList<>(new WorkspaceDomain().getAllowedActions()))); } catch (ServerException e) { LOG.error( "Can't add creator's permissions for workspace with id '" + event.getWorkspace().getId() + "'", e); } } }
requestFactory.fromUrl(getIdBasedUrl(workspace.getId())).useDeleteMethod().request(); format( "Error of waiting on workspace name='%s', id='%s', username='%s' removal.", workspaceName, workspace.getId(), userName), e); "Workspace name='{}', id='{}', username='{}' removed", workspaceName, workspace.getId(), userName);
public WorkspaceImpl(Workspace workspace, Account account) { this( workspace.getId(), account, workspace.getConfig(), workspace.getRuntime(), workspace.getAttributes(), workspace.isTemporary(), workspace.getStatus()); }
/** Sends stop workspace request. */ private void sendStopRequest(String workspaceName, String userName) throws Exception { if (!exists(workspaceName, userName)) { return; } Workspace workspace = getByName(workspaceName, userName); String apiUrl = getIdBasedUrl(workspace.getId()) + "/runtime/"; requestFactory.fromUrl(apiUrl).useDeleteMethod().request(); }
public CompletableFuture<Void> stopAsync(Workspace workspace, Map<String, String> options) throws NotFoundException, ConflictException { String workspaceId = workspace.getId(); WorkspaceStatus status = statuses.get(workspaceId); if (status == null) { workspace.getNamespace(), workspace.getConfig().getName(), workspace.getId(), stoppedBy); publishWorkspaceStatusEvent(workspaceId, STOPPING, status, options.get(WORKSPACE_STOP_REASON));
@Override public void run() { String workspaceId = workspace.getId();
@Override public void run() { String workspaceId = workspace.getId(); try { runtime.start(options);
runtime = infra.prepare(identity, internalEnv).getRuntime(); WorkspaceStatus runtimeStatus = runtime.getStatus(); try (Unlocker ignored = lockService.writeLock(workspace.getId())) { statuses.replace(identity.getWorkspaceId(), runtimeStatus); runtimes.putIfAbsent(identity.getWorkspaceId(), runtime);
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; }
/** Converts {@link Workspace} to {@link WorkspaceDto}. */ public static WorkspaceDto asDto(Workspace workspace) { WorkspaceDto workspaceDto = newDto(WorkspaceDto.class) .withId(workspace.getId()) .withStatus(workspace.getStatus()) .withNamespace(workspace.getNamespace()) .withTemporary(workspace.isTemporary()) .withAttributes(workspace.getAttributes()) .withConfig(asDto(workspace.getConfig())); if (workspace.getRuntime() != null) { RuntimeDto runtime = asDto(workspace.getRuntime()); workspaceDto.setRuntime(runtime); } return workspaceDto; }