Search in sources :

Example 1 with CancelMonitorThread

use of com.genericworkflownodes.knime.execution.impl.CancelMonitorThread in project GenericKnimeNodes by genericworkflownodes.

the class DynamicGenericNodeModel method executeTool.

/**
 * Executes the tool underlying this node.
 *
 * @param executor
 *            The fully configured {@link IToolExecutor}.
 *
 * @param execContext
 *            The {@link ExecutionContext} of the node.
 *
 * @throws Exception
 */
private void executeTool(IToolExecutor executor, final ExecutionContext execContext) throws ExecutionFailedException {
    final AsynchronousToolExecutor asyncExecutor = new AsynchronousToolExecutor(executor);
    asyncExecutor.invoke();
    // create one thread that will periodically check if the user has
    // cancelled the execution of the node
    // if this monitor thread detects that a cancel was requested, then it
    // will invoke the kill method
    // of the asyncExecutor
    final CancelMonitorThread monitorThread = new CancelMonitorThread(asyncExecutor, execContext);
    monitorThread.start();
    // wait until the execution completes
    asyncExecutor.waitUntilFinished();
    // also wait for the monitor thread to die
    monitorThread.waitUntilFinished();
    int retcode = -1;
    try {
        retcode = asyncExecutor.getReturnCode();
    } catch (ExecutionException ex) {
        // it means that the task threw an exception, assume retcode == -1
        throw new ExecutionFailedException(m_nodeConfig.getName(), ex);
    } catch (InterruptedException iex) {
        throw new ExecutionFailedException(m_nodeConfig.getName(), iex);
    }
    LOGGER.debug("COMMAND:  " + executor.getCommand());
    LOGGER.debug("STDOUT:  " + executor.getToolOutput());
    LOGGER.debug("STDERR:  " + executor.getToolErrorOutput());
    LOGGER.debug("RETCODE: " + retcode);
    if (retcode != 0) {
        LOGGER.error("Failing process stdout: " + executor.getToolOutput());
        LOGGER.error("Failing process stderr: " + executor.getToolErrorOutput());
        // process failed, so we will send the stdout/stderr messages into
        // the dialogs
        setFailedExternalOutput(executor.getToolOutput());
        setFailedExternalErrorOutput(executor.getToolErrorOutput());
        throw new ExecutionFailedException(m_nodeConfig.getName());
    }
    // finally fill the stdout/stderr messages into the dialogs
    setExternalOutput(executor.getToolOutput());
    setExternalErrorOutput(executor.getToolErrorOutput());
}
Also used : CancelMonitorThread(com.genericworkflownodes.knime.execution.impl.CancelMonitorThread) ExecutionFailedException(com.genericworkflownodes.knime.generic_node.ExecutionFailedException) AsynchronousToolExecutor(com.genericworkflownodes.knime.execution.AsynchronousToolExecutor) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with CancelMonitorThread

use of com.genericworkflownodes.knime.execution.impl.CancelMonitorThread in project GenericKnimeNodes by genericworkflownodes.

the class GenericKnimeNodeModel method executeTool.

/**
 * Executes the tool underlying this node.
 *
 * @param executor
 *            The fully configured {@link IToolExecutor}.
 *
 * @param execContext
 *            The {@link ExecutionContext} of the node.
 *
 * @throws Exception
 */
private void executeTool(IToolExecutor executor, final ExecutionContext execContext) throws ExecutionFailedException {
    final AsynchronousToolExecutor asyncExecutor = new AsynchronousToolExecutor(executor);
    asyncExecutor.invoke();
    // create one thread that will periodically check if the user has
    // cancelled the execution of the node
    // if this monitor thread detects that a cancel was requested, then it
    // will invoke the kill method
    // of the asyncExecutor
    final CancelMonitorThread monitorThread = new CancelMonitorThread(asyncExecutor, execContext);
    monitorThread.start();
    // wait until the execution completes
    asyncExecutor.waitUntilFinished();
    // also wait for the monitor thread to die
    monitorThread.waitUntilFinished();
    int retcode = -1;
    try {
        retcode = asyncExecutor.getReturnCode();
    } catch (ExecutionException ex) {
        // it means that the task threw an exception, assume retcode == -1
        throw new ExecutionFailedException(m_nodeConfig.getName(), ex);
    } catch (InterruptedException iex) {
        throw new ExecutionFailedException(m_nodeConfig.getName(), iex);
    }
    LOGGER.debug("COMMAND:  " + executor.getCommand());
    LOGGER.debug("STDOUT:  " + executor.getToolOutput());
    LOGGER.debug("STDERR:  " + executor.getToolErrorOutput());
    LOGGER.debug("RETCODE: " + retcode);
    if (retcode != 0) {
        LOGGER.error("Failing process stdout: " + executor.getToolOutput());
        LOGGER.error("Failing process stderr: " + executor.getToolErrorOutput());
        // process failed, so we will send the stdout/stderr messages into
        // the dialogs
        setFailedExternalOutput(executor.getToolOutput());
        setFailedExternalErrorOutput(executor.getToolErrorOutput());
        throw new ExecutionFailedException(m_nodeConfig.getName());
    }
    // finally fill the stdout/stderr messages into the dialogs
    setExternalOutput(executor.getToolOutput());
    setExternalErrorOutput(executor.getToolErrorOutput());
}
Also used : CancelMonitorThread(com.genericworkflownodes.knime.execution.impl.CancelMonitorThread) AsynchronousToolExecutor(com.genericworkflownodes.knime.execution.AsynchronousToolExecutor) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

AsynchronousToolExecutor (com.genericworkflownodes.knime.execution.AsynchronousToolExecutor)2 CancelMonitorThread (com.genericworkflownodes.knime.execution.impl.CancelMonitorThread)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutionFailedException (com.genericworkflownodes.knime.generic_node.ExecutionFailedException)1