-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework Composer plugin to avoid using Everrest based Websocket calls
Fixes #5348 Signed-off-by: Kaloyan Raev <kaloyan.r@zend.com>
- Loading branch information
1 parent
aff3be6
commit 403d30d
Showing
11 changed files
with
339 additions
and
163 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
...omposer-ide/src/main/java/org/eclipse/che/plugin/composer/ide/ComposerJsonRpcHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2016-2017 Rogue Wave Software, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Rogue Wave Software, Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.plugin.composer.ide; | ||
|
||
import com.google.inject.Inject; | ||
import com.google.inject.Singleton; | ||
|
||
import org.eclipse.che.api.core.jsonrpc.commons.RequestHandlerConfigurator; | ||
import org.eclipse.che.api.core.jsonrpc.commons.RequestTransmitter; | ||
import org.eclipse.che.plugin.composer.shared.dto.ComposerOutput; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
|
||
import static org.eclipse.che.plugin.composer.shared.Constants.COMPOSER_CHANNEL_OUTPUT; | ||
import static org.eclipse.che.plugin.composer.shared.Constants.COMPOSER_CHANNEL_SUBSCRIBE; | ||
|
||
/** | ||
* A mechanism for handling all messages from the Composer and applying | ||
* registered consumers. | ||
* | ||
* @author Kaloyan Raev | ||
*/ | ||
@Singleton | ||
public class ComposerJsonRpcHandler { | ||
private static final String WS_AGENT_ENDPOINT = "ws-agent"; | ||
|
||
private RequestHandlerConfigurator configurator; | ||
|
||
private Set<Consumer<ComposerOutput>> composerOutputConsumers = new HashSet<>(); | ||
|
||
private boolean isSubscribed = false; | ||
|
||
@Inject | ||
public ComposerJsonRpcHandler(RequestHandlerConfigurator configurator) { | ||
this.configurator = configurator; | ||
|
||
handleComposerMessages(); | ||
} | ||
|
||
@Inject | ||
private void subscribe(RequestTransmitter requestTransmitter) { | ||
if (isSubscribed) { | ||
return; | ||
} | ||
|
||
requestTransmitter.newRequest() | ||
.endpointId(WS_AGENT_ENDPOINT) | ||
.methodName(COMPOSER_CHANNEL_SUBSCRIBE) | ||
.noParams() | ||
.sendAndSkipResult(); | ||
|
||
isSubscribed = true; | ||
} | ||
|
||
/** | ||
* Adds consumer for the event with {@link ComposerOutput}. | ||
* | ||
* @param consumer | ||
* new consumer | ||
*/ | ||
public void addComposerOutputHandler(Consumer<ComposerOutput> consumer) { | ||
composerOutputConsumers.add(consumer); | ||
} | ||
|
||
private void handleComposerMessages() { | ||
configurator.newConfiguration() | ||
.methodName(COMPOSER_CHANNEL_OUTPUT) | ||
.paramsAsDto(ComposerOutput.class) | ||
.noResult() | ||
.withConsumer(archetypeOutput -> composerOutputConsumers.forEach(it -> it.accept(archetypeOutput))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...rc/main/java/org/eclipse/che/plugin/composer/server/executor/ComposerCommandExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2016-2017 Rogue Wave Software, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Rogue Wave Software, Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.plugin.composer.server.executor; | ||
|
||
import org.eclipse.che.api.core.notification.EventService; | ||
import org.eclipse.che.api.core.util.AbstractLineConsumer; | ||
import org.eclipse.che.api.core.util.LineConsumer; | ||
import org.eclipse.che.api.core.util.ProcessUtil; | ||
import org.eclipse.che.api.core.util.ValueHolder; | ||
import org.eclipse.che.api.core.util.Watchdog; | ||
import org.eclipse.che.plugin.composer.shared.dto.ComposerOutput; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
/** | ||
* @author Kaloyan Raev | ||
*/ | ||
@Singleton | ||
public class ComposerCommandExecutor { | ||
|
||
private EventService eventService; | ||
|
||
@Inject | ||
public ComposerCommandExecutor(EventService eventService) { | ||
this.eventService = eventService; | ||
} | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(ComposerCommandExecutor.class); | ||
|
||
public void execute(String[] commandLine, File workDir) | ||
throws TimeoutException, IOException, InterruptedException { | ||
ProcessBuilder pb = new ProcessBuilder(commandLine).redirectErrorStream(true).directory(workDir); | ||
|
||
eventService.publish(new ComposerOutputImpl(String.join(" ", commandLine), ComposerOutput.State.START)); | ||
|
||
LineConsumer lineConsumer = new AbstractLineConsumer() { | ||
@Override | ||
public void writeLine(String line) throws IOException { | ||
eventService.publish(new ComposerOutputImpl(line, ComposerOutput.State.IN_PROGRESS)); | ||
} | ||
}; | ||
|
||
// process will be stopped after timeout | ||
Watchdog watcher = new Watchdog(10, TimeUnit.MINUTES); | ||
|
||
try { | ||
final Process process = pb.start(); | ||
final ValueHolder<Boolean> isTimeoutExceeded = new ValueHolder<>(false); | ||
watcher.start(() -> { | ||
isTimeoutExceeded.set(true); | ||
ProcessUtil.kill(process); | ||
}); | ||
// consume logs until process ends | ||
ProcessUtil.process(process, lineConsumer); | ||
process.waitFor(); | ||
eventService.publish(new ComposerOutputImpl("Done", ComposerOutput.State.DONE)); | ||
if (isTimeoutExceeded.get()) { | ||
LOG.error("Command time expired : command-line " + Arrays.toString(commandLine)); | ||
eventService.publish(new ComposerOutputImpl("Installing dependencies time expired", ComposerOutput.State.ERROR)); | ||
throw new TimeoutException(); | ||
} else if (process.exitValue() != 0) { | ||
LOG.error("Command failed : command-line " + Arrays.toString(commandLine)); | ||
eventService.publish(new ComposerOutputImpl("Error occurred", ComposerOutput.State.ERROR)); | ||
throw new IOException("Process failed. Exit code " + process.exitValue() + " command-line : " + Arrays.toString(commandLine)); | ||
} | ||
} finally { | ||
watcher.stop(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.