Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of recover from snapshot dialog #4328

Merged
merged 1 commit into from
Mar 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,8 @@ public interface WorkspaceServiceClient {
* if <code>false</code> workspace will not be restored from snapshot
* even if auto-restore is enabled and snapshot exists
* @return a promise that resolves to the {@link WorkspaceDto}, or rejects with an error
* @see WorkspaceService#startById(String, String, Boolean, String)
*/
Promise<WorkspaceDto> startById(String id, String envName, boolean restore);
Promise<WorkspaceDto> startById(String id, String envName, Boolean restore);

/**
* Stops running workspace.
Expand Down Expand Up @@ -273,6 +272,7 @@ public interface WorkspaceServiceClient {
* @return a promise that will provide a list of {@link SnapshotDto}s, or rejects with an error
* @see WorkspaceService#getSnapshot(String)
*/
@Deprecated
Promise<List<SnapshotDto>> getSnapshot(String workspaceId);

/**
Expand All @@ -283,6 +283,7 @@ public interface WorkspaceServiceClient {
* @return a promise that will resolve when the snapshot has been created, or rejects with an error
* @see WorkspaceService#createSnapshot(String)
*/
@Deprecated
Promise<Void> createSnapshot(String workspaceId);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
import com.google.inject.Inject;
import com.google.inject.Singleton;

import org.eclipse.che.api.promises.client.Operation;
import org.eclipse.che.api.promises.client.OperationException;
import org.eclipse.che.api.promises.client.PromiseError;
import org.eclipse.che.ide.api.workspace.WorkspaceServiceClient;
import org.eclipse.che.ide.CoreLocalizationConstant;
import org.eclipse.che.ide.api.notification.NotificationManager;
import org.eclipse.che.ide.api.notification.StatusNotification;
Expand All @@ -27,30 +23,35 @@
import static org.eclipse.che.ide.api.notification.StatusNotification.Status.SUCCESS;

/**
* Creates snapshot of the workspace using {@link WorkspaceServiceClient}.
*
* <p>This component is for managing notifications which are related to creating snapshot process.
* Shows notifications about workspace snapshotting progress.
* Each call to {@link #creationStarted()} must be eventually followed by
* either call to {@link #creationError(String)} or {@link #successfullyCreated()}.
*
* @author Yevhenii Voevodin
*/
@Singleton
public class WorkspaceSnapshotCreator {
public class WorkspaceSnapshotNotifier {

private final WorkspaceServiceClient workspaceService;
private final NotificationManager notificationManager;
private final CoreLocalizationConstant locale;

private StatusNotification notification;

@Inject
public WorkspaceSnapshotCreator(WorkspaceServiceClient workspaceService,
NotificationManager notificationManager,
CoreLocalizationConstant locale) {
this.workspaceService = workspaceService;
public WorkspaceSnapshotNotifier(NotificationManager notificationManager, CoreLocalizationConstant locale) {
this.notificationManager = notificationManager;
this.locale = locale;
}

/**
* Starts showing snapshotting notification.
* The notification is shown until either {@link #creationError(String)}
* or {@link #successfullyCreated()} is called.
*/
public void creationStarted() {
notification = notificationManager.notify(locale.createSnapshotProgress(), PROGRESS, FLOAT_MODE);
}

/**
* Changes notification state to finished with an error.
*/
Expand All @@ -71,28 +72,4 @@ public void successfullyCreated() {
notification.setTitle(locale.createSnapshotSuccess());
}
}

/**
* Returns true if workspace creation process is not done, otherwise when it is done - returns false
*/
public boolean isInProgress() {
return notification != null && notification.getStatus() == PROGRESS;
}

/**
* Creates snapshot from workspace with given id and shows appropriate notification.
*
* @param workspaceId
* id of the workspace to create snapshot from.
*/
public void createSnapshot(String workspaceId) {
notification = notificationManager.notify(locale.createSnapshotProgress(), PROGRESS, FLOAT_MODE);
workspaceService.createSnapshot(workspaceId)
.catchError(new Operation<PromiseError>() {
@Override
public void apply(PromiseError error) throws OperationException {
creationError(error.getMessage());
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void start(final Callback<Component, Exception> callback) {
new Operation<WorkspaceDto>() {
@Override
public void apply(WorkspaceDto workspaceDto) throws OperationException {
handleWorkspaceEvents(workspaceDto, callback, true, false);
handleWorkspaceEvents(workspaceDto, callback, null);
}
}).catchError(new Operation<PromiseError>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import org.eclipse.che.api.core.model.workspace.Workspace;
import org.eclipse.che.api.core.model.workspace.WorkspaceStatus;
import org.eclipse.che.api.machine.shared.dto.SnapshotDto;
import org.eclipse.che.api.promises.client.Function;
import org.eclipse.che.api.promises.client.FunctionException;
import org.eclipse.che.api.promises.client.Operation;
Expand All @@ -26,8 +25,6 @@
import org.eclipse.che.ide.CoreLocalizationConstant;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.component.Component;
import org.eclipse.che.ide.api.dialogs.CancelCallback;
import org.eclipse.che.ide.api.dialogs.ConfirmCallback;
import org.eclipse.che.ide.api.dialogs.DialogFactory;
import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent;
import org.eclipse.che.ide.api.machine.events.WsAgentStateHandler;
Expand All @@ -48,7 +45,6 @@
import org.eclipse.che.ide.workspace.create.CreateWorkspacePresenter;
import org.eclipse.che.ide.workspace.start.StartWorkspacePresenter;

import java.util.List;
import java.util.Map;

import static org.eclipse.che.api.workspace.shared.Constants.CHE_WORKSPACE_AUTO_START;
Expand Down Expand Up @@ -165,13 +161,11 @@ public void setCurrentWorkspace(Workspace workspace) {
* workspace to listen
* @param callback
* callback
* @param checkForShapshots
* whether is needed checking workspace for snapshots
* @param restoreFromSnapshot
* restore or not the workspace from snapshot
*/
public void handleWorkspaceEvents(final WorkspaceDto workspace, final Callback<Component, Exception> callback,
final boolean checkForShapshots, final boolean restoreFromSnapshot) {
final Boolean restoreFromSnapshot) {
this.callback = callback;
if (messageBus != null) {
messageBus.cancelReconnection();
Expand Down Expand Up @@ -211,12 +205,7 @@ public void execute() {
@Override
public Map<String, String> apply(Map<String, String> settings) throws FunctionException {
if (Boolean.parseBoolean(settings.getOrDefault(CHE_WORKSPACE_AUTO_START, "true"))) {
if (checkForShapshots) {
checkWorkspaceForSnapshots(workspace);
} else {
startWorkspaceById(workspace.getId(), workspace.getConfig().getDefaultEnv(),
restoreFromSnapshot);
}
startWorkspaceById(workspace.getId(), workspace.getConfig().getDefaultEnv(), restoreFromSnapshot);
} else {
loader.show(WORKSPACE_STOPPED);
}
Expand All @@ -235,17 +224,15 @@ public Map<String, String> apply(Map<String, String> settings) throws FunctionEx
* workspace ID to start
* @param callback
* callback
* @param checkForShapshots
* whether is needed checking workspace for snapshots
* @param restoreFromSnapshot
* restore or not the workspace from snapshot
*/
public void startWorkspace(final String workspaceID, final Callback<Component, Exception> callback,
final boolean checkForShapshots, final boolean restoreFromSnapshot) {
final Boolean restoreFromSnapshot) {
workspaceServiceClient.getWorkspace(workspaceID).then(new Operation<WorkspaceDto>() {
@Override
public void apply(WorkspaceDto workspace) throws OperationException {
handleWorkspaceEvents(workspace, callback, checkForShapshots, restoreFromSnapshot);
handleWorkspaceEvents(workspace, callback, restoreFromSnapshot);
}
});
}
Expand All @@ -259,26 +246,7 @@ public void apply(WorkspaceDto workspace) throws OperationException {
* callback to be executed
*/
public void startWorkspace(final Workspace workspace, final Callback<Component, Exception> callback) {
startWorkspace(workspace.getId(), callback, true, false);
}

/**
* Checks workspace for snapshots and asks the uses for an action.
*
* @param workspace
* workspace
*/
private void checkWorkspaceForSnapshots(final Workspace workspace) {
workspaceServiceClient.getSnapshot(workspace.getId()).then(new Operation<List<SnapshotDto>>() {
@Override
public void apply(List<SnapshotDto> snapshots) throws OperationException {
if (snapshots.isEmpty()) {
startWorkspaceById(workspace.getId(), workspace.getConfig().getDefaultEnv(), false);
} else {
showRecoverWorkspaceConfirmDialog(workspace);
}
}
});
startWorkspace(workspace.getId(), callback, null);
}

/**
Expand All @@ -288,30 +256,6 @@ private native void notifyShowIDE() /*-{
$wnd.parent.postMessage("show-ide", "*");
}-*/;

/**
* Shows workspace recovering confirm dialog.
*/
private void showRecoverWorkspaceConfirmDialog(final Workspace workspace) {
dialogFactory.createConfirmDialog(locale.workspaceRecoveringDialogTitle(),
locale.workspaceRecoveringDialogText(),
locale.yesButtonTitle(),
locale.noButtonTitle(),
new ConfirmCallback() {
@Override
public void accepted() {
startWorkspaceById(workspace.getId(), workspace.getConfig().getDefaultEnv(), true);
}
},
new CancelCallback() {
@Override
public void cancelled() {
startWorkspaceById(workspace.getId(), workspace.getConfig().getDefaultEnv(), false);
}
}).show();

notifyShowIDE();
}

/**
* Starts specified workspace if it's {@link WorkspaceStatus} different of {@code RUNNING}
*/
Expand All @@ -326,7 +270,7 @@ public void apply(WorkspaceDto workspaceToStart) throws OperationException {

abstract void tryStartWorkspace();

private void startWorkspaceById(String workspaceId, String defaultEnvironment, boolean restoreFromSnapshot) {
private void startWorkspaceById(String workspaceId, String defaultEnvironment, Boolean restoreFromSnapshot) {
loader.show(STARTING_WORKSPACE_RUNTIME);
workspaceServiceClient.startById(workspaceId, defaultEnvironment, restoreFromSnapshot).catchError(new Operation<PromiseError>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.eclipse.che.api.workspace.shared.dto.event.WorkspaceStatusEvent;
import org.eclipse.che.ide.CoreLocalizationConstant;
import org.eclipse.che.ide.DelayedTask;
import org.eclipse.che.ide.actions.WorkspaceSnapshotCreator;
import org.eclipse.che.ide.actions.WorkspaceSnapshotNotifier;
import org.eclipse.che.ide.api.component.Component;
import org.eclipse.che.ide.api.dialogs.ConfirmCallback;
import org.eclipse.che.ide.api.dialogs.DialogFactory;
Expand Down Expand Up @@ -92,7 +92,7 @@ public class WorkspaceEventsHandler {
private final DtoUnmarshallerFactory dtoUnmarshallerFactory;
private final Provider<DefaultWorkspaceComponent> wsComponentProvider;
private final AsyncRequestFactory asyncRequestFactory;
private final WorkspaceSnapshotCreator snapshotCreator;
private final WorkspaceSnapshotNotifier snapshotNotifier;
private final WorkspaceServiceClient workspaceServiceClient;
private final StartWorkspaceNotification startWorkspaceNotification;
private final ExecAgentCommandManager execAgentCommandManager;
Expand Down Expand Up @@ -124,7 +124,7 @@ public class WorkspaceEventsHandler {
final DtoUnmarshallerFactory dtoUnmarshallerFactory,
final NotificationManager notificationManager,
final MessageBusProvider messageBusProvider,
final WorkspaceSnapshotCreator snapshotCreator,
final WorkspaceSnapshotNotifier snapshotNotifier,
final WorkspaceServiceClient workspaceServiceClient,
final StartWorkspaceNotification startWorkspaceNotification,
final Provider<DefaultWorkspaceComponent> wsComponentProvider,
Expand All @@ -134,7 +134,7 @@ public class WorkspaceEventsHandler {
this.eventBus = eventBus;
this.locale = locale;
this.messageBusProvider = messageBusProvider;
this.snapshotCreator = snapshotCreator;
this.snapshotNotifier = snapshotNotifier;
this.notificationManager = notificationManager;
this.dialogFactory = dialogFactory;
this.dtoUnmarshallerFactory = dtoUnmarshallerFactory;
Expand Down Expand Up @@ -417,11 +417,12 @@ protected void onMessageReceived(WorkspaceStatusEvent statusEvent) {

case SNAPSHOT_CREATING:
loader.show(LoaderPresenter.Phase.CREATING_WORKSPACE_SNAPSHOT);
snapshotNotifier.creationStarted();
break;

case SNAPSHOT_CREATED:
loader.setSuccess(LoaderPresenter.Phase.CREATING_WORKSPACE_SNAPSHOT);
snapshotCreator.successfullyCreated();
snapshotNotifier.successfullyCreated();

wsStartedNotification = new DelayedTask() {
@Override
Expand All @@ -442,7 +443,7 @@ public void onExecute() {

case SNAPSHOT_CREATION_ERROR:
loader.setError(LoaderPresenter.Phase.CREATING_WORKSPACE_SNAPSHOT);
snapshotCreator.creationError("Snapshot creation error: " + statusEvent.getError());
snapshotNotifier.creationError("Snapshot creation error: " + statusEvent.getError());
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,13 @@ public Promise<WorkspaceDto> startFromConfig(final WorkspaceConfigDto cfg, final
}

@Override
public Promise<WorkspaceDto> startById(@NotNull final String id, final String envName, final boolean restore) {
String url = baseHttpUrl + "/" + id + "/runtime?restore=" + restore;
public Promise<WorkspaceDto> startById(@NotNull final String id, final String envName, final Boolean restore) {
String url = baseHttpUrl + "/" + id + "/runtime";
if (restore != null) {
url += "?restore=" + restore;
}
if (envName != null) {
url += "&environment=" + envName;
url += (url.contains("?") ? '&' : '?') + "environment=" + envName;
}
return asyncRequestFactory.createPostRequest(url, null)
.header(ACCEPT, APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void onSuccess(Component result) {
@Override
public void onFailure(Exception reason) {
}
}, false, restore.getValue().booleanValue());
}, restore.getValue());
}

}