Using a NopProcessHandler

Answered

I'm creating a run configuration that uses internal code rather an external executable to perform its tasks. From what I can tell, I need to return a NopProcessHandler from my RunProfileState.execute() implementation for this. When I execute the run configuration, output is correctly displayed to the console, but the process hangs until it is manually terminated.  This seems to be the same problem as https://intellij-support.jetbrains.com/hc/en-us/community/posts/205966264-Process-started-through-RunConfiguration-won-t-exit, but I was hoping for some documentation or code snippets to get me pointed in the right direction.

 

/**
 * Process for executing a TcpRequestRunConfiguration.
 *
 * @param config: run configuration
 * @param environment: execution environment
 * @see <a href="https://plugins.jetbrains.com/docs/intellij/run-configurations.html#implement-a-run-configuration">Run Configurations Tutorial</a>
 */
class TcpRequestState(
    private val runConfiguration: TcpRequestRunConfiguration,
    private val environment: ExecutionEnvironment
) : RunProfileState {

    private val consoleBuilder = TextConsoleBuilderFactory.getInstance().createBuilder(environment.project)

    /**
     * Execute a run configuration.
     *
     * @param executor
     * @param programRunner
     * @return execution result
     */
    override fun execute(executor: Executor?, programRunner: ProgramRunner<*>): ExecutionResult {
        val (host, port) = runConfiguration.host.split(":", limit = 2)
        val client = TcpClient(host, port.toInt())
        client.send(runConfiguration.message.encodeToByteArray())
        val response = client.recv().decodeToString()
        val console = consoleBuilder.console
        console.print(response, ConsoleViewContentType.NORMAL_OUTPUT)
        val processHandler = NopProcessHandler().also {
            ProcessTerminatedListener.attach(it, environment.project)
        }
        return DefaultExecutionResult(console, processHandler)
    }
}
0
3 comments

try `com.intellij.execution.process.ProcessHandler#startNotify` after creation

0

I added startNotify() to the NopProcessHandler initialization block, but the process still has to be manually stopped after output is displayed in the Run Configuration result window.

  override fun execute(executor: Executor?, programRunner: ProgramRunner<*>): ExecutionResult {
        val (host, port) = runConfiguration.host.split(":", limit = 2)
        val client = TcpClient(host, port.toInt())
        client.send(runConfiguration.message.encodeToByteArray())
        val response = client.recv().decodeToString()
        val console = consoleBuilder.console
        console.print(response, ConsoleViewContentType.NORMAL_OUTPUT)
        val processHandler = NopProcessHandler().also {
            ProcessTerminatedListener.attach(it, environment.project)
            it.startNotify()  // <https://intellij-support.jetbrains.com/hc/en-us/community/posts/20577979721874/comments/20707884475794>
        }
        return DefaultExecutionResult(console, processHandler)
    }

 

0

Please try adding console.attachToProcess(processHandler);

0

Please sign in to leave a comment.