Search in sources :

Example 1 with GetProcessLogsResponseDto

use of org.eclipse.che.api.machine.shared.dto.execagent.GetProcessLogsResponseDto in project che by eclipse.

the class ProcessesPanelPresenter method restoreState.

private void restoreState(final MachineEntity machine) {
    execAgentCommandManager.getProcesses(machine.getId(), false).then(new Operation<List<GetProcessesResponseDto>>() {

        @Override
        public void apply(List<GetProcessesResponseDto> processes) throws OperationException {
            for (GetProcessesResponseDto process : processes) {
                final int pid = process.getPid();
                final String type = process.getType();
                /*
                     * Do not show the process if the command line has prefix #hidden
                     */
                if (!isNullOrEmpty(process.getCommandLine()) && process.getCommandLine().startsWith("#hidden")) {
                    continue;
                }
                /*
                     * Hide the processes which are launched by command of unknown type
                     */
                if (isProcessLaunchedByCommandOfKnownType(type)) {
                    final String processName = process.getName();
                    final CommandImpl commandByName = getWorkspaceCommandByName(processName);
                    if (commandByName == null) {
                        final String commandLine = process.getCommandLine();
                        final CommandImpl command = new CommandImpl(processName, commandLine, type);
                        final CommandOutputConsole console = commandConsoleFactory.create(command, machine);
                        getAndPrintProcessLogs(console, pid);
                        subscribeToProcess(console, pid);
                        addCommandOutput(machine.getId(), console);
                    } else {
                        macroProcessor.expandMacros(commandByName.getCommandLine()).then(new Operation<String>() {

                            @Override
                            public void apply(String expandedCommandLine) throws OperationException {
                                final CommandImpl command = new CommandImpl(commandByName.getName(), expandedCommandLine, commandByName.getType(), commandByName.getAttributes());
                                final CommandOutputConsole console = commandConsoleFactory.create(command, machine);
                                getAndPrintProcessLogs(console, pid);
                                subscribeToProcess(console, pid);
                                addCommandOutput(machine.getId(), console);
                            }
                        });
                    }
                }
            }
        }

        private void getAndPrintProcessLogs(final CommandOutputConsole console, final int pid) {
            String from = null;
            String till = null;
            int limit = 50;
            int skip = 0;
            execAgentCommandManager.getProcessLogs(machine.getId(), pid, from, till, limit, skip).then(new Operation<List<GetProcessLogsResponseDto>>() {

                @Override
                public void apply(List<GetProcessLogsResponseDto> logs) throws OperationException {
                    for (GetProcessLogsResponseDto log : logs) {
                        String text = log.getText();
                        console.printOutput(text);
                    }
                }
            }).catchError(new Operation<PromiseError>() {

                @Override
                public void apply(PromiseError arg) throws OperationException {
                    String error = "Error trying to get process log with pid: " + pid + ". " + arg.getMessage();
                    Log.error(getClass(), error);
                }
            });
        }

        private void subscribeToProcess(CommandOutputConsole console, int pid) {
            String stderr = "stderr";
            String stdout = "stdout";
            String processStatus = "process_status";
            String after = null;
            execAgentCommandManager.subscribe(machine.getId(), pid, asList(stderr, stdout, processStatus), after).thenIfProcessStartedEvent(console.getProcessStartedOperation()).thenIfProcessDiedEvent(console.getProcessDiedOperation()).thenIfProcessStdOutEvent(console.getStdOutOperation()).thenIfProcessStdErrEvent(console.getStdErrOperation()).then(console.getProcessSubscribeOperation());
        }
    }).catchError(new Operation<PromiseError>() {

        @Override
        public void apply(PromiseError arg) throws OperationException {
            notificationManager.notify(localizationConstant.failedToGetProcesses(machine.getId()));
        }
    });
}
Also used : CommandImpl(org.eclipse.che.ide.api.command.CommandImpl) GetProcessLogsResponseDto(org.eclipse.che.api.machine.shared.dto.execagent.GetProcessLogsResponseDto) GetProcessesResponseDto(org.eclipse.che.api.machine.shared.dto.execagent.GetProcessesResponseDto) Operation(org.eclipse.che.api.promises.client.Operation) PromiseError(org.eclipse.che.api.promises.client.PromiseError) CommandOutputConsole(org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandOutputConsole) Arrays.asList(java.util.Arrays.asList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ArrayList(java.util.ArrayList) OperationException(org.eclipse.che.api.promises.client.OperationException)

Aggregations

ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Collections.emptyList (java.util.Collections.emptyList)1 List (java.util.List)1 GetProcessLogsResponseDto (org.eclipse.che.api.machine.shared.dto.execagent.GetProcessLogsResponseDto)1 GetProcessesResponseDto (org.eclipse.che.api.machine.shared.dto.execagent.GetProcessesResponseDto)1 Operation (org.eclipse.che.api.promises.client.Operation)1 OperationException (org.eclipse.che.api.promises.client.OperationException)1 PromiseError (org.eclipse.che.api.promises.client.PromiseError)1 CommandImpl (org.eclipse.che.ide.api.command.CommandImpl)1 CommandOutputConsole (org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandOutputConsole)1