Search in sources :

Example 56 with ExecutionEnvironment

use of com.intellij.execution.runners.ExecutionEnvironment in project azure-tools-for-java by Microsoft.

the class SparkBatchJobDebuggerRunner method forkEnvironment.

/*
     * Build a child environment with specified host and type
     */
private ExecutionEnvironment forkEnvironment(@NotNull final ExecutionEnvironment parentEnv, final String host, final boolean isDriver) {
    final String savedProfileName = parentEnv.getUserData(ProfileNameKey);
    final String originProfileName = savedProfileName == null ? parentEnv.getRunProfile().getName() : savedProfileName;
    final RunConfiguration newRunConfiguration = ((RunConfiguration) parentEnv.getRunProfile()).clone();
    newRunConfiguration.setName(originProfileName + " [" + (isDriver ? "Driver " : "Executor ") + host + "]");
    final ExecutionEnvironment childEnv = new ExecutionEnvironmentBuilder(parentEnv).runProfile(newRunConfiguration).build();
    childEnv.putUserData(DEBUG_TARGET_KEY, isDriver ? DEBUG_DRIVER : DEBUG_EXECUTOR);
    childEnv.putUserData(ProfileNameKey, originProfileName);
    return childEnv;
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) LivySparkBatchJobRunConfiguration(com.microsoft.azure.hdinsight.spark.run.configuration.LivySparkBatchJobRunConfiguration) ExecutionEnvironmentBuilder(com.intellij.execution.runners.ExecutionEnvironmentBuilder)

Example 57 with ExecutionEnvironment

use of com.intellij.execution.runners.ExecutionEnvironment in project azure-tools-for-java by Microsoft.

the class LivySparkBatchJobRunConfiguration method getState.

@Nullable
@Override
public RunProfileState getState(@NotNull final Executor executor, @NotNull final ExecutionEnvironment executionEnvironment) throws ExecutionException {
    Operation operation = executionEnvironment.getUserData(TelemetryKeys.OPERATION);
    final String debugTarget = executionEnvironment.getUserData(SparkBatchJobDebuggerRunner.DEBUG_TARGET_KEY);
    final boolean isExecutor = StringUtils.equals(debugTarget, SparkBatchJobDebuggerRunner.DEBUG_EXECUTOR);
    RunProfileStateWithAppInsightsEvent state = null;
    final Artifact selectedArtifact = ArtifactUtil.getArtifactWithOutputPaths(getProject()).stream().filter(artifact -> artifact.getName().equals(getSubmitModel().getArtifactName())).findFirst().orElse(null);
    if (executor instanceof SparkBatchJobDebugExecutor) {
        final ISparkBatchJob remoteDebugBatch = sparkRemoteBatch;
        if (!(remoteDebugBatch instanceof SparkBatchRemoteDebugJob)) {
            throw new ExecutionException("Spark Batch Job is not prepared for " + executor.getId());
        }
        if (isExecutor) {
            setRunMode(RunMode.REMOTE_DEBUG_EXECUTOR);
            state = new SparkBatchRemoteDebugExecutorState(getModel().getSubmitModel(), operation, remoteDebugBatch);
        } else {
            if (selectedArtifact != null) {
                BuildArtifactsBeforeRunTaskProvider.setBuildArtifactBeforeRun(getProject(), this, selectedArtifact);
            }
            setRunMode(RunMode.REMOTE);
            state = new SparkBatchRemoteDebugState(getModel().getSubmitModel(), operation, sparkRemoteBatch);
        }
    } else if (executor instanceof SparkBatchJobRunExecutor) {
        final ISparkBatchJob remoteBatch = sparkRemoteBatch;
        if (remoteBatch == null) {
            throw new ExecutionException("Spark Batch Job is not prepared for " + executor.getId());
        }
        if (selectedArtifact != null) {
            BuildArtifactsBeforeRunTaskProvider.setBuildArtifactBeforeRun(getProject(), this, selectedArtifact);
        }
        setRunMode(RunMode.REMOTE);
        state = new SparkBatchRemoteRunState(getModel().getSubmitModel(), operation, remoteBatch);
    } else if (executor instanceof DefaultDebugExecutor) {
        setRunMode(RunMode.LOCAL);
        if (operation == null) {
            operation = TelemetryManager.createOperation(TelemetryConstants.HDINSIGHT, TelemetryConstants.DEBUG_LOCAL_SPARK_JOB);
            operation.start();
        }
        state = new SparkBatchLocalDebugState(getProject(), getModel().getLocalRunConfigurableModel(), operation);
    } else if (executor instanceof DefaultRunExecutor) {
        setRunMode(RunMode.LOCAL);
        if (operation == null) {
            operation = TelemetryManager.createOperation(TelemetryConstants.HDINSIGHT, TelemetryConstants.RUN_LOCAL_SPARK_JOB);
            operation.start();
        }
        state = new SparkBatchLocalRunState(getProject(), getModel().getLocalRunConfigurableModel(), operation);
    }
    if (state != null) {
        final Map<String, String> props = getActionProperties().entrySet().stream().collect(Collectors.toMap((Map.Entry<Object, Object> entry) -> entry.getKey() == null ? null : entry.getKey().toString(), (Map.Entry<Object, Object> entry) -> entry.getValue() == null ? "" : entry.getValue().toString()));
        final String configurationId = Optional.ofNullable(executionEnvironment.getRunnerAndConfigurationSettings()).map(settings -> settings.getType().getId()).orElse("");
        props.put("configurationId", configurationId);
        state.createAppInsightEvent(executor, props);
        EventUtil.logEvent(EventType.info, operation, props);
        // Clear the action properties
        getActionProperties().clear();
    }
    return state;
}
Also used : ISecureStore(com.microsoft.azure.toolkit.ide.common.store.ISecureStore) java.util(java.util) BLOB(com.microsoft.azure.hdinsight.spark.common.SparkSubmitStorageType.BLOB) TelemetryKeys(com.microsoft.intellij.telemetry.TelemetryKeys) NotNull(com.microsoft.azuretools.azurecommons.helpers.NotNull) ExecutionException(com.intellij.execution.ExecutionException) Artifact(com.intellij.packaging.artifacts.Artifact) CompileStepBeforeRun(com.intellij.compiler.options.CompileStepBeforeRun) InvalidDataException(com.intellij.openapi.util.InvalidDataException) StringUtils(org.apache.commons.lang3.StringUtils) com.intellij.execution.configurations(com.intellij.execution.configurations) SparkSubmitJobUploadStorageModelKt.getSecureStoreServiceOf(com.microsoft.azure.hdinsight.spark.common.SparkSubmitJobUploadStorageModelKt.getSecureStoreServiceOf) ILogger(com.microsoft.azure.hdinsight.common.logger.ILogger) Observable(rx.Observable) ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) BuildArtifactsBeforeRunTaskProvider(com.intellij.packaging.impl.run.BuildArtifactsBeforeRunTaskProvider) SparkSubmissionJobUploadStorageWithUploadPathPanel(com.microsoft.azure.hdinsight.spark.ui.SparkSubmissionJobUploadStorageWithUploadPathPanel) SparkApplicationType(com.microsoft.azure.hdinsight.spark.run.action.SparkApplicationType) Disposer(com.intellij.openapi.util.Disposer) SparkBatchJobConfigurable(com.microsoft.azure.hdinsight.spark.ui.SparkBatchJobConfigurable) Project(com.intellij.openapi.project.Project) BuildArtifactsBeforeRunTask(com.intellij.packaging.impl.run.BuildArtifactsBeforeRunTask) Module(com.intellij.openapi.module.Module) BeforeRunTask(com.intellij.execution.BeforeRunTask) SettingsEditor(com.intellij.openapi.options.SettingsEditor) DefaultRunExecutor(com.intellij.execution.executors.DefaultRunExecutor) DefaultDebugExecutor(com.intellij.execution.executors.DefaultDebugExecutor) ProgramRunner(com.intellij.execution.runners.ProgramRunner) Nullable(com.microsoft.azuretools.azurecommons.helpers.Nullable) Operation(com.microsoft.azuretools.telemetrywrapper.Operation) ADLS_GEN2(com.microsoft.azure.hdinsight.spark.common.SparkSubmitStorageType.ADLS_GEN2) AzureStoreManager(com.microsoft.azure.toolkit.ide.common.store.AzureStoreManager) Executor(com.intellij.execution.Executor) com.microsoft.azure.hdinsight.spark.run(com.microsoft.azure.hdinsight.spark.run) Collectors(java.util.stream.Collectors) File(java.io.File) JavaExecutionUtil(com.intellij.execution.JavaExecutionUtil) EventType(com.microsoft.azuretools.telemetrywrapper.EventType) Stream(java.util.stream.Stream) ArtifactUtil(com.intellij.packaging.impl.artifacts.ArtifactUtil) TelemetryConstants(com.microsoft.azuretools.telemetry.TelemetryConstants) TelemetryManager(com.microsoft.azuretools.telemetrywrapper.TelemetryManager) UsePassword(com.microsoft.azure.hdinsight.spark.common.SparkBatchRemoteDebugJobSshAuth.SSHAuthType.UsePassword) EventUtil(com.microsoft.azuretools.telemetrywrapper.EventUtil) WriteExternalException(com.intellij.openapi.util.WriteExternalException) Element(org.jdom.Element) com.microsoft.azure.hdinsight.spark.common(com.microsoft.azure.hdinsight.spark.common) Operation(com.microsoft.azuretools.telemetrywrapper.Operation) Artifact(com.intellij.packaging.artifacts.Artifact) DefaultDebugExecutor(com.intellij.execution.executors.DefaultDebugExecutor) ExecutionException(com.intellij.execution.ExecutionException) DefaultRunExecutor(com.intellij.execution.executors.DefaultRunExecutor) Nullable(com.microsoft.azuretools.azurecommons.helpers.Nullable)

Example 58 with ExecutionEnvironment

use of com.intellij.execution.runners.ExecutionEnvironment in project intellij by bazelbuild.

the class ClassFileManifestBuilder method buildManifest.

/**
 * Builds a .class file manifest, then diffs against any previously calculated manifest for this
 * debugging session.
 *
 * @return null if no diff is available (either no manifest could be calculated, or no previously
 *     calculated manifest is available.
 */
@Nullable
public static ClassFileManifest.Diff buildManifest(ExecutionEnvironment env, @Nullable HotSwapProgress progress) throws ExecutionException {
    if (!HotSwapUtils.canHotSwap(env)) {
        return null;
    }
    BlazeCommandRunConfiguration configuration = getConfiguration(env);
    Project project = configuration.getProject();
    BlazeProjectData projectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
    if (projectData == null) {
        throw new ExecutionException("Not synced yet; please sync project");
    }
    JavaClasspathAspectStrategy aspectStrategy = JavaClasspathAspectStrategy.findStrategy(projectData.blazeVersionData);
    if (aspectStrategy == null) {
        return null;
    }
    try (BuildResultHelper buildResultHelper = BuildResultHelper.forFiles(file -> true)) {
        ListenableFuture<BuildResult> buildOperation = BlazeBeforeRunCommandHelper.runBlazeBuild(configuration, buildResultHelper, aspectStrategy.getBuildFlags(), ImmutableList.of(), "Building debug binary");
        if (progress != null) {
            progress.setCancelWorker(() -> buildOperation.cancel(true));
        }
        try {
            SaveUtil.saveAllFiles();
            BuildResult result = buildOperation.get();
            if (result.status != BuildResult.Status.SUCCESS) {
                throw new ExecutionException("Blaze failure building debug binary");
            }
        } catch (InterruptedException | CancellationException e) {
            buildOperation.cancel(true);
            throw new RunCanceledByUserException();
        } catch (java.util.concurrent.ExecutionException e) {
            throw new ExecutionException(e);
        }
        ImmutableList<File> jars = buildResultHelper.getArtifactsForOutputGroups(ImmutableSet.of(JavaClasspathAspectStrategy.OUTPUT_GROUP)).stream().filter(f -> f.getName().endsWith(".jar")).collect(toImmutableList());
        ClassFileManifest oldManifest = getManifest(env);
        ClassFileManifest newManifest = ClassFileManifest.build(jars, oldManifest);
        env.getCopyableUserData(MANIFEST_KEY).set(newManifest);
        return oldManifest != null ? ClassFileManifest.modifiedClasses(oldManifest, newManifest) : null;
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) CancellationException(java.util.concurrent.CancellationException) ExecutionException(com.intellij.execution.ExecutionException) Key(com.intellij.openapi.util.Key) BuildResult(com.google.idea.blaze.base.sync.aspects.BuildResult) RunProfile(com.intellij.execution.configurations.RunProfile) BuildResultHelper(com.google.idea.blaze.base.command.buildresult.BuildResultHelper) AtomicReference(java.util.concurrent.atomic.AtomicReference) BlazeProjectDataManager(com.google.idea.blaze.base.sync.data.BlazeProjectDataManager) File(java.io.File) BlazeCommandRunConfiguration(com.google.idea.blaze.base.run.BlazeCommandRunConfiguration) WrappingRunConfiguration(com.intellij.execution.configurations.WrappingRunConfiguration) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) BlazeBeforeRunCommandHelper(com.google.idea.blaze.base.run.BlazeBeforeRunCommandHelper) RunCanceledByUserException(com.intellij.execution.RunCanceledByUserException) ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) ImmutableList(com.google.common.collect.ImmutableList) HotSwapProgress(com.intellij.debugger.impl.HotSwapProgress) Project(com.intellij.openapi.project.Project) SaveUtil(com.google.idea.blaze.base.util.SaveUtil) GuavaHelper.toImmutableList(com.google.idea.common.guava.GuavaHelper.toImmutableList) Nullable(javax.annotation.Nullable) RunCanceledByUserException(com.intellij.execution.RunCanceledByUserException) Project(com.intellij.openapi.project.Project) BuildResult(com.google.idea.blaze.base.sync.aspects.BuildResult) BuildResultHelper(com.google.idea.blaze.base.command.buildresult.BuildResultHelper) CancellationException(java.util.concurrent.CancellationException) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) ExecutionException(com.intellij.execution.ExecutionException) File(java.io.File) BlazeCommandRunConfiguration(com.google.idea.blaze.base.run.BlazeCommandRunConfiguration) Nullable(javax.annotation.Nullable)

Example 59 with ExecutionEnvironment

use of com.intellij.execution.runners.ExecutionEnvironment in project intellij by bazelbuild.

the class BlazeHotSwapManager method findHotSwappableBlazeDebuggerSession.

@Nullable
static HotSwappableDebugSession findHotSwappableBlazeDebuggerSession(Project project) {
    DebuggerManagerEx debuggerManager = DebuggerManagerEx.getInstanceEx(project);
    DebuggerSession session = debuggerManager.getContext().getDebuggerSession();
    if (session == null || !session.isAttached()) {
        return null;
    }
    JavaDebugProcess process = session.getProcess().getXdebugProcess();
    if (process == null) {
        return null;
    }
    ExecutionEnvironment env = ((XDebugSessionImpl) process.getSession()).getExecutionEnvironment();
    if (env == null || ClassFileManifestBuilder.getManifest(env) == null) {
        return null;
    }
    RunProfile runProfile = env.getRunProfile();
    if (!(runProfile instanceof BlazeCommandRunConfiguration)) {
        return null;
    }
    return new HotSwappableDebugSession(session, env, (BlazeCommandRunConfiguration) runProfile);
}
Also used : DebuggerManagerEx(com.intellij.debugger.DebuggerManagerEx) ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) DebuggerSession(com.intellij.debugger.impl.DebuggerSession) JavaDebugProcess(com.intellij.debugger.engine.JavaDebugProcess) RunProfile(com.intellij.execution.configurations.RunProfile) BlazeCommandRunConfiguration(com.google.idea.blaze.base.run.BlazeCommandRunConfiguration) XDebugSessionImpl(com.intellij.xdebugger.impl.XDebugSessionImpl) Nullable(javax.annotation.Nullable)

Example 60 with ExecutionEnvironment

use of com.intellij.execution.runners.ExecutionEnvironment in project intellij by bazelbuild.

the class ConnectBlazeTestDebuggerTask method launchDebugger.

/**
 * Nearly a clone of {@link ConnectJavaDebuggerTask#launchDebugger}. There are a few changes to
 * account for null variables that could occur in our implementation.
 */
@Override
public ProcessHandler launchDebugger(@NotNull LaunchInfo currentLaunchInfo, @NotNull Client client, @NotNull ProcessHandlerLaunchStatus launchStatus, @NotNull ProcessHandlerConsolePrinter printer) {
    String debugPort = Integer.toString(client.getDebuggerListenPort());
    int pid = client.getClientData().getPid();
    Logger.getInstance(ConnectJavaDebuggerTask.class).info(String.format(Locale.US, "Attempting to connect debugger to port %1$s [client %2$d]", debugPort, pid));
    // create a new process handler
    RemoteConnection connection = new RemoteConnection(true, "localhost", debugPort, false);
    RemoteDebugProcessHandler debugProcessHandler = new RemoteDebugProcessHandler(project);
    // switch the launch status and console printers to point to the new process handler
    // this is required, esp. for AndroidTestListener which holds a
    // reference to the launch status and printers, and those should
    // be updated to point to the new process handlers,
    // otherwise test results will not be forwarded appropriately
    ProcessHandler oldProcessHandler = launchStatus.getProcessHandler();
    launchStatus.setProcessHandler(debugProcessHandler);
    printer.setProcessHandler(debugProcessHandler);
    // Detach old process handler after the launch status
    // has been updated to point to the new process handler.
    oldProcessHandler.detachProcess();
    AndroidDebugState debugState = new AndroidDebugState(project, debugProcessHandler, connection, currentLaunchInfo.consoleProvider);
    RunContentDescriptor oldDescriptor;
    AndroidSessionInfo oldSession = oldProcessHandler.getUserData(AndroidSessionInfo.KEY);
    if (oldSession != null) {
        oldDescriptor = oldSession.getDescriptor();
    } else {
        // This is the first time we are attaching the debugger; get it from the environment instead.
        oldDescriptor = currentLaunchInfo.env.getContentToReuse();
    }
    RunContentDescriptor debugDescriptor;
    try {
        // @formatter:off
        ExecutionEnvironment debugEnv = new ExecutionEnvironmentBuilder(currentLaunchInfo.env).executor(currentLaunchInfo.executor).runner(currentLaunchInfo.runner).contentToReuse(oldDescriptor).build();
        debugDescriptor = DebuggerPanelsManager.getInstance(project).attachVirtualMachine(debugEnv, debugState, connection, false);
    // @formatter:on
    } catch (ExecutionException e) {
        printer.stderr("ExecutionException: " + e.getMessage() + '.');
        return null;
    }
    // Based on the above try block we shouldn't get here unless we have assigned to debugDescriptor
    assert debugDescriptor != null;
    // re-run the collected text from the old process handler to the new
    // TODO: is there a race between messages received once the debugger has been connected,
    // and these messages that are printed out?
    final AndroidProcessText oldText = AndroidProcessText.get(oldProcessHandler);
    if (oldText != null) {
        oldText.printTo(debugProcessHandler);
    }
    RunProfile runProfile = currentLaunchInfo.env.getRunProfile();
    int uniqueId = runProfile instanceof AndroidRunConfigurationBase ? ((AndroidRunConfigurationBase) runProfile).getUniqueID() : -1;
    AndroidSessionInfo value = new AndroidSessionInfo(debugProcessHandler, debugDescriptor, uniqueId, currentLaunchInfo.executor.getId(), currentLaunchInfo.executor.getActionName(), false);
    debugProcessHandler.putUserData(AndroidSessionInfo.KEY, value);
    debugProcessHandler.putUserData(AndroidSessionInfo.ANDROID_DEBUG_CLIENT, client);
    debugProcessHandler.putUserData(AndroidSessionInfo.ANDROID_DEVICE_API_LEVEL, client.getDevice().getVersion());
    return debugProcessHandler;
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) RunContentDescriptor(com.intellij.execution.ui.RunContentDescriptor) AndroidRunConfigurationBase(com.android.tools.idea.run.AndroidRunConfigurationBase) ConnectJavaDebuggerTask(com.android.tools.idea.run.tasks.ConnectJavaDebuggerTask) AndroidSessionInfo(com.android.tools.idea.run.AndroidSessionInfo) RunProfile(com.intellij.execution.configurations.RunProfile) RemoteDebugProcessHandler(com.intellij.debugger.engine.RemoteDebugProcessHandler) AndroidProcessText(com.android.tools.idea.run.AndroidProcessText) AndroidDebugState(com.android.tools.idea.run.AndroidDebugState) RemoteDebugProcessHandler(com.intellij.debugger.engine.RemoteDebugProcessHandler) ProcessHandler(com.intellij.execution.process.ProcessHandler) RemoteConnection(com.intellij.execution.configurations.RemoteConnection) ExecutionEnvironmentBuilder(com.intellij.execution.runners.ExecutionEnvironmentBuilder) ExecutionException(com.intellij.execution.ExecutionException)

Aggregations

ExecutionEnvironment (com.intellij.execution.runners.ExecutionEnvironment)63 ProcessHandler (com.intellij.execution.process.ProcessHandler)17 Project (com.intellij.openapi.project.Project)16 ExecutionException (com.intellij.execution.ExecutionException)15 NotNull (org.jetbrains.annotations.NotNull)14 ExecutionEnvironmentBuilder (com.intellij.execution.runners.ExecutionEnvironmentBuilder)13 RunContentDescriptor (com.intellij.execution.ui.RunContentDescriptor)13 Nullable (org.jetbrains.annotations.Nullable)13 Executor (com.intellij.execution.Executor)12 ProcessEvent (com.intellij.execution.process.ProcessEvent)12 ProgramRunner (com.intellij.execution.runners.ProgramRunner)12 ProcessAdapter (com.intellij.execution.process.ProcessAdapter)11 RunProfile (com.intellij.execution.configurations.RunProfile)10 DefaultRunExecutor (com.intellij.execution.executors.DefaultRunExecutor)10 Key (com.intellij.openapi.util.Key)10 DefaultDebugExecutor (com.intellij.execution.executors.DefaultDebugExecutor)8 IOException (java.io.IOException)8 Module (com.intellij.openapi.module.Module)7 Ref (com.intellij.openapi.util.Ref)7 RunnerAndConfigurationSettings (com.intellij.execution.RunnerAndConfigurationSettings)6