Skip to content

Commit

Permalink
Fire events on REPL (finos#159)
Browse files Browse the repository at this point in the history
* Fire events on REPL

* Fix typo on variable name
  • Loading branch information
rafaelbey authored Mar 25, 2024
1 parent 5dfbbc7 commit 160a12b
Showing 1 changed file with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,43 @@

package org.finos.legend.engine.ide.lsp.server;

import java.lang.management.ManagementFactory;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import org.finos.legend.engine.ide.lsp.extension.LegendLSPFeature;
import org.finos.legend.engine.ide.lsp.extension.features.LegendREPLFeature;
import org.finos.legend.engine.ide.lsp.extension.features.LegendUsageEventConsumer;

public class LegendREPLTerminal
{
public static void main(String... args) throws InterruptedException
{
List<LegendLSPFeature> features = new ArrayList<>();
Instant startTime = Instant.now().minusMillis(ManagementFactory.getRuntimeMXBean().getUptime());
Map<String, Object> metadata = new HashMap<>();
metadata.put("replId", ManagementFactory.getRuntimeMXBean().getName());

try
{
Optional<LegendREPLFeature> repl = ServiceLoader.load(LegendLSPFeature.class)
ServiceLoader.load(LegendLSPFeature.class).forEach(features::add);

Optional<LegendREPLFeature> repl = features
.stream()
.map(ServiceLoader.Provider::get)
.filter(LegendREPLFeature.class::isInstance)
.map(LegendREPLFeature.class::cast)
.findAny();


if (repl.isPresent())
{
fireEvent(features, LegendUsageEventConsumer.event("startReplTerminal", startTime, Instant.now(), metadata));
repl.get().startREPL();
fireEvent(features, LegendUsageEventConsumer.event("closeReplTerminal", startTime, Instant.now(), metadata));
}
else
{
Expand All @@ -48,10 +64,22 @@ public static void main(String... args) throws InterruptedException
System.out.println("An error has occurred and cannot start the REPL terminal:");
e.printStackTrace(System.out);
System.out.println("The terminal will close itself in 60 seconds");
metadata.put("error", true);
metadata.put("errorMessage", e.getMessage());
fireEvent(features, LegendUsageEventConsumer.event("errorReplTerminal", startTime, Instant.now(), metadata));
Thread.sleep(60_000);
System.exit(1);
}

System.exit(0);
}

private static void fireEvent(List<LegendLSPFeature> features, LegendUsageEventConsumer.LegendUsageEvent event)
{
features
.stream()
.filter(LegendUsageEventConsumer.class::isInstance)
.map(LegendUsageEventConsumer.class::cast)
.forEach(x -> x.consume(event));
}
}

0 comments on commit 160a12b

Please sign in to comment.