@Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof WorkspaceImpl)) return false; final WorkspaceImpl other = (WorkspaceImpl) obj; return Objects.equals(id, other.id) && Objects.equals(getNamespace(), other.getNamespace()) && Objects.equals(status, other.status) && isTemporary == other.isTemporary && getAttributes().equals(other.getAttributes()) && Objects.equals(config, other.config) && Objects.equals(runtime, other.runtime); }
private void setAbnormalStopAttributes(String workspaceId, String error) { try { WorkspaceImpl workspace = workspaceDao.get(workspaceId); workspace.getAttributes().put(ERROR_MESSAGE_ATTRIBUTE_NAME, error); workspace.getAttributes().put(STOPPED_ATTRIBUTE_NAME, Long.toString(currentTimeMillis())); workspace.getAttributes().put(STOPPED_ABNORMALLY_ATTRIBUTE_NAME, Boolean.toString(true)); workspaceDao.update(workspace); } catch (NotFoundException | ServerException | ConflictException e) { LOG.warn( format( "Cannot set error status of the workspace %s. Error status is: %s. Cause: %s", workspaceId, error, e.getMessage())); } } }
@Override public int hashCode() { int hash = 7; hash = 31 * hash + Objects.hashCode(id); hash = 31 * hash + Objects.hashCode(getNamespace()); hash = 31 * hash + Objects.hashCode(status); hash = 31 * hash + Objects.hashCode(config); hash = 31 * hash + getAttributes().hashCode(); hash = 31 * hash + Boolean.hashCode(isTemporary); hash = 31 * hash + Objects.hashCode(runtime); return hash; }
/** Checks whether workspace with given id, related to this workspace runtimes. */ private boolean containsThisRuntimesId(String workspaceId) { try { final WorkspaceImpl workspace = workspaceDao.get(workspaceId); if (workspaceRuntimesId.equals( workspace.getAttributes().get(WORKSPACE_RUNTIMES_ID_ATTRIBUTE))) { return true; } } catch (NotFoundException | ServerException ex) { LOG.warn( String.format( "Failed to get processing workspace %s. Cause: %s", workspaceId, ex.getMessage())); } return false; }
private void setRuntimesId(String workspaceId) { try { final WorkspaceImpl workspace = workspaceDao.get(workspaceId); workspace.getAttributes().put(WORKSPACE_RUNTIMES_ID_ATTRIBUTE, workspaceRuntimesId); workspaceDao.update(workspace); } catch (NotFoundException | ServerException | ConflictException ex) { LOG.warn( String.format( "Cannot set runtimes id attribute for the workspace %s. Cause: %s", workspaceId, ex.getMessage())); } }
/** * Asynchronously stops the workspace. * * @param workspaceId the id of the workspace to stop * @throws ServerException when any server error occurs * @throws NullPointerException when {@code workspaceId} is null * @throws NotFoundException when workspace {@code workspaceId} doesn't have runtime */ public void stopWorkspace(String workspaceId, Map<String, String> options) throws ServerException, NotFoundException, ConflictException { requireNonNull(workspaceId, "Required non-null workspace id"); final WorkspaceImpl workspace = normalizeState(workspaceDao.get(workspaceId), true); checkWorkspaceIsRunningOrStarting(workspace); if (!workspace.isTemporary()) { workspace.getAttributes().put(STOPPED_ATTRIBUTE_NAME, Long.toString(currentTimeMillis())); workspace.getAttributes().put(STOPPED_ABNORMALLY_ATTRIBUTE_NAME, Boolean.toString(false)); workspaceDao.update(workspace); } runtimes .stopAsync(workspace, options) .whenComplete( (aVoid, throwable) -> { if (workspace.isTemporary()) { removeWorkspaceQuietly(workspace); } }); }
/** Asynchronously starts given workspace. */ private void startAsync( WorkspaceImpl workspace, @Nullable String envName, Map<String, String> options) throws ConflictException, NotFoundException, ServerException { String env = getValidatedEnvironmentName(workspace, envName); workspace.getAttributes().put(UPDATED_ATTRIBUTE_NAME, Long.toString(currentTimeMillis())); workspaceDao.update(workspace); runtimes .startAsync(workspace, env, firstNonNull(options, Collections.emptyMap())) .thenAccept(aVoid -> handleStartupSuccess(workspace)) .exceptionally( ex -> { if (workspace.isTemporary()) { removeWorkspaceQuietly(workspace); } else { handleStartupError(workspace, ex.getCause()); } return null; }); }
/** * Updates an existing workspace with a new configuration. * * <p> * * <p>Replace strategy is used for workspace update, it means that existing workspace data will be * replaced with given {@code update}. * * @param update workspace update * @return updated instance of the workspace * @throws NullPointerException when either {@code workspaceId} or {@code update} is null * @throws NotFoundException when workspace with given id doesn't exist * @throws ConflictException when any conflict occurs (e.g Workspace with such name already exists * in {@code namespace}) * @throws ServerException when any other error occurs */ public WorkspaceImpl updateWorkspace(String id, Workspace update) throws ConflictException, ServerException, NotFoundException, ValidationException { requireNonNull(id, "Required non-null workspace id"); requireNonNull(update, "Required non-null workspace update"); requireNonNull(update.getConfig(), "Required non-null workspace configuration update"); validator.validateConfig(update.getConfig()); validator.validateAttributes(update.getAttributes()); WorkspaceImpl workspace = workspaceDao.get(id); workspace.setConfig(new WorkspaceConfigImpl(update.getConfig())); workspace.setAttributes(update.getAttributes()); workspace.getAttributes().put(UPDATED_ATTRIBUTE_NAME, Long.toString(currentTimeMillis())); workspace.setTemporary(update.isTemporary()); return normalizeState(workspaceDao.update(workspace), true); }
private WorkspaceImpl doCreateWorkspace( WorkspaceConfig config, Account account, Map<String, String> attributes, boolean isTemporary) throws NotFoundException, ServerException, ConflictException { WorkspaceImpl workspace = WorkspaceImpl.builder() .generateId() .setConfig(config) .setAccount(account) .setAttributes(attributes) .setTemporary(isTemporary) .setStatus(STOPPED) .build(); workspace.getAttributes().put(CREATED_ATTRIBUTE_NAME, Long.toString(currentTimeMillis())); workspaceDao.create(workspace); LOG.info( "Workspace '{}/{}' with id '{}' created by user '{}'", account.getName(), workspace.getConfig().getName(), workspace.getId(), sessionUserNameOrUndefined()); eventService.publish(new WorkspaceCreatedEvent(workspace)); return workspace; }