/** * 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; }); }