Search in sources :

Example 1 with WorkspaceStatus

use of org.eclipse.che.api.core.model.workspace.WorkspaceStatus in project che by eclipse.

the class WorkspaceRuntimes method startEnvironmentAndPublishEvents.

/**
     * Starts the environment publishing all the necessary events.
     * Respects task interruption & stops the workspace if starting task is cancelled.
     */
private void startEnvironmentAndPublishEvents(EnvironmentImpl environment, String workspaceId, String envName, boolean recover) throws ServerException, EnvironmentException, ConflictException {
    try {
        envEngine.start(workspaceId, envName, environment, recover, new WebsocketMessageConsumer<>(format(ENVIRONMENT_OUTPUT_CHANNEL_TEMPLATE, workspaceId)), machineAgentsLauncher);
    } catch (EnvironmentStartInterruptedException x) {
        // environment start was interrupted, it's either shutdown or direct stop
        // in the case of shutdown make sure the status is correct,
        // otherwise workspace is already stopping
        compareAndSetStatus(workspaceId, WorkspaceStatus.STARTING, WorkspaceStatus.STOPPING);
        removeStateAndPublishStopEvents(workspaceId);
        throw x;
    } catch (EnvironmentException | ServerException | ConflictException x) {
        // environment can't be started for some reason, STARTING -> STOPPED
        removeState(workspaceId);
        eventsService.publish(DtoFactory.newDto(WorkspaceStatusEvent.class).withWorkspaceId(workspaceId).withEventType(EventType.ERROR).withPrevStatus(WorkspaceStatus.STARTING).withStatus(WorkspaceStatus.STOPPED).withError("Start of environment '" + envName + "' failed. Error: " + x.getMessage()));
        throw x;
    }
    // disallow direct start cancellation, STARTING -> RUNNING
    WorkspaceStatus prevStatus;
    try (@SuppressWarnings("unused") Unlocker u = locks.writeLock(workspaceId)) {
        checkIsNotTerminated("finish workspace start");
        RuntimeState state = states.get(workspaceId);
        prevStatus = state.status;
        if (state.status == WorkspaceStatus.STARTING) {
            state.status = WorkspaceStatus.RUNNING;
            state.startTask = null;
            state.startFuture = null;
        }
    }
    // or stop is called directly, anyway stop the environment
    if (Thread.interrupted() || prevStatus != WorkspaceStatus.STARTING) {
        try {
            stopEnvironmentAndPublishEvents(workspaceId, WorkspaceStatus.STARTING);
        } catch (Exception x) {
            LOG.error("Couldn't stop the environment '{}' of the workspace '{}'. Error: {}", envName, workspaceId, x.getMessage());
        }
        throw new EnvironmentStartInterruptedException(workspaceId, envName);
    }
    // normally started, notify clients
    eventsService.publish(DtoFactory.newDto(WorkspaceStatusEvent.class).withWorkspaceId(workspaceId).withStatus(WorkspaceStatus.RUNNING).withEventType(EventType.RUNNING).withPrevStatus(WorkspaceStatus.STARTING));
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) WorkspaceStatusEvent(org.eclipse.che.api.workspace.shared.dto.event.WorkspaceStatusEvent) WorkspaceStatus(org.eclipse.che.api.core.model.workspace.WorkspaceStatus) EnvironmentException(org.eclipse.che.api.environment.server.exception.EnvironmentException) AgentException(org.eclipse.che.api.agent.server.exception.AgentException) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) CancellationException(java.util.concurrent.CancellationException) EnvironmentException(org.eclipse.che.api.environment.server.exception.EnvironmentException) ConflictException(org.eclipse.che.api.core.ConflictException) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) NotFoundException(org.eclipse.che.api.core.NotFoundException) SnapshotException(org.eclipse.che.api.machine.server.exception.SnapshotException) ServerException(org.eclipse.che.api.core.ServerException)

Example 2 with WorkspaceStatus

use of org.eclipse.che.api.core.model.workspace.WorkspaceStatus in project che by eclipse.

the class WorkspaceComponent method handleWorkspaceEvents.

/**
     * Listens message bus and handles workspace events.
     *
     * @param workspace
     *         workspace to listen
     * @param callback
     *         callback
     * @param restoreFromSnapshot
     *         restore or not the workspace from snapshot
     */
public void handleWorkspaceEvents(final WorkspaceDto workspace, final Callback<Component, Exception> callback, final Boolean restoreFromSnapshot) {
    this.callback = callback;
    if (messageBus != null) {
        messageBus.cancelReconnection();
    }
    messageBus = messageBusProvider.createMessageBus();
    messageBus.addOnOpenHandler(new ConnectionOpenedHandler() {

        @Override
        public void onOpen() {
            loader.show(STARTING_WORKSPACE_RUNTIME);
            messageBus.removeOnOpenHandler(this);
            setCurrentWorkspace(workspace);
            workspaceEventsHandler.trackWorkspaceEvents(workspace, callback);
            final WorkspaceStatus workspaceStatus = workspace.getStatus();
            switch(workspaceStatus) {
                case SNAPSHOTTING:
                    loader.show(CREATING_WORKSPACE_SNAPSHOT);
                    break;
                case STARTING:
                    eventBus.fireEvent(new WorkspaceStartingEvent(workspace));
                    break;
                case RUNNING:
                    Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {

                        @Override
                        public void execute() {
                            loader.setSuccess(STARTING_WORKSPACE_RUNTIME);
                            eventBus.fireEvent(new WorkspaceStartedEvent(workspace));
                        }
                    });
                    break;
                default:
                    //
                    workspaceServiceClient.getSettings().then(new Function<Map<String, String>, Map<String, String>>() {

                        @Override
                        public Map<String, String> apply(Map<String, String> settings) throws FunctionException {
                            if (Boolean.parseBoolean(settings.getOrDefault(CHE_WORKSPACE_AUTO_START, "true"))) {
                                startWorkspaceById(workspace.getId(), workspace.getConfig().getDefaultEnv(), restoreFromSnapshot);
                            } else {
                                loader.show(WORKSPACE_STOPPED);
                            }
                            return settings;
                        }
                    });
            }
        }
    });
}
Also used : ConnectionOpenedHandler(org.eclipse.che.ide.websocket.events.ConnectionOpenedHandler) Function(org.eclipse.che.api.promises.client.Function) WorkspaceStartedEvent(org.eclipse.che.ide.api.workspace.event.WorkspaceStartedEvent) WorkspaceStatus(org.eclipse.che.api.core.model.workspace.WorkspaceStatus) WorkspaceStartingEvent(org.eclipse.che.ide.api.workspace.event.WorkspaceStartingEvent) Map(java.util.Map)

Aggregations

WorkspaceStatus (org.eclipse.che.api.core.model.workspace.WorkspaceStatus)2 Map (java.util.Map)1 CancellationException (java.util.concurrent.CancellationException)1 AgentException (org.eclipse.che.api.agent.server.exception.AgentException)1 ConflictException (org.eclipse.che.api.core.ConflictException)1 NotFoundException (org.eclipse.che.api.core.NotFoundException)1 ServerException (org.eclipse.che.api.core.ServerException)1 EnvironmentException (org.eclipse.che.api.environment.server.exception.EnvironmentException)1 EnvironmentNotRunningException (org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException)1 EnvironmentStartInterruptedException (org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException)1 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)1 SnapshotException (org.eclipse.che.api.machine.server.exception.SnapshotException)1 Function (org.eclipse.che.api.promises.client.Function)1 WorkspaceStatusEvent (org.eclipse.che.api.workspace.shared.dto.event.WorkspaceStatusEvent)1 Unlocker (org.eclipse.che.commons.lang.concurrent.Unlocker)1 WorkspaceStartedEvent (org.eclipse.che.ide.api.workspace.event.WorkspaceStartedEvent)1 WorkspaceStartingEvent (org.eclipse.che.ide.api.workspace.event.WorkspaceStartingEvent)1 ConnectionOpenedHandler (org.eclipse.che.ide.websocket.events.ConnectionOpenedHandler)1