Search in sources :

Example 1 with ProcessExecutorException

use of com.axway.ats.common.process.ProcessExecutorException in project ats-framework by Axway.

the class ProcessUtils method getProcessId.

/**
     * Tries to get the ID of the process started.
     * <em>Note:</em>Currently may work only on some UNIX variants with SUN/Oracle JDK.
     * @return The ID of the process. -1 in case of error.
     */
public int getProcessId() {
    if ("java.lang.UNIXProcess".equals(theProcess.getClass().getName())) {
        try {
            Class<?> proc = theProcess.getClass();
            Field field = proc.getDeclaredField("pid");
            field.setAccessible(true);
            Object pid = field.get(theProcess);
            int pidNumber = (Integer) pid;
            log.info("The PID of the sterted process is " + pidNumber);
            return pidNumber;
        } catch (Exception e) {
            throw new ProcessExecutorException("Error retrieving process ID", e);
        }
    } else {
        return -1;
    }
}
Also used : Field(java.lang.reflect.Field) ProcessExecutorException(com.axway.ats.common.process.ProcessExecutorException) ProcessExecutorException(com.axway.ats.common.process.ProcessExecutorException)

Example 2 with ProcessExecutorException

use of com.axway.ats.common.process.ProcessExecutorException in project ats-framework by Axway.

the class ProcessUtils method killProcessAndItsChildren.

/**
     * Kill started process and makes best effort to kill its children processes.<br />
     * Warning is logged if issues is detected.<br />
     * Note that this might be invoked after some time if not sure that all child processes are already started.
     */
public void killProcessAndItsChildren() {
    if (OperatingSystemType.getCurrentOsType().isUnix()) {
        // first kill child processes because otherwise their parent ID is changed to 1
        int pid = getProcessId();
        String command = "pkill -P " + pid;
        if (log.isDebugEnabled()) {
            log.debug("Try to destroy child processes with '" + command + "'");
        }
        int exitCode = -1;
        try {
            exitCode = Runtime.getRuntime().exec(command).waitFor();
        } catch (Exception e) {
            throw new ProcessExecutorException("Could not kill the process with id '" + pid + "'", e);
        }
        // kill this process
        killProcess();
        if (exitCode != 0) {
            log.warn("Error while trying to kill subprocesses. Exit code returned from '" + command + "' command: " + exitCode);
        }
    } else if (OperatingSystemType.getCurrentOsType().isWindows()) {
        // Windows assumed
        // use org.jvnet.winp.WinProcess
        log.debug("Windows detected and will try to kill whole subtree.");
        new WinProcess(theProcess).killRecursively();
    } else {
        throw new IllegalStateException("Not supported operating system type. Report the case to ATS team");
    }
}
Also used : ProcessExecutorException(com.axway.ats.common.process.ProcessExecutorException) ProcessExecutorException(com.axway.ats.common.process.ProcessExecutorException) WinProcess(org.jvnet.winp.WinProcess)

Example 3 with ProcessExecutorException

use of com.axway.ats.common.process.ProcessExecutorException in project ats-framework by Axway.

the class ProcessExecutor method killExternalProcess.

/**
     * Killing external process(such not started by us) on a remote host. <br/>
     * The process is found by a token which is contained in the start command.
     * No regex supported.
     *
     * @param atsAgent the address of the remote ATS agent which will run the kill command
     * @param startCommandSnippet the token to search for in the process start command.
     * The minimum allowed length is 2 characters
     * @return the number of killed processes
     */
@PublicAtsApi
public static int killExternalProcess(@Validate(name = "atsAgent", type = ValidationType.STRING_SERVER_WITH_PORT) String atsAgent, @Validate(name = "startCommandSnippet", type = ValidationType.STRING_NOT_EMPTY) String startCommandSnippet) {
    // validate input parameters
    atsAgent = HostUtils.getAtsAgentIpAndPort(atsAgent);
    new Validator().validateMethodParameters(new Object[] { atsAgent, startCommandSnippet });
    try {
        return new InternalProcessOperations(atsAgent).killExternalProcess(startCommandSnippet);
    } catch (AgentException e) {
        throw new ProcessExecutorException(e);
    }
}
Also used : InternalProcessOperations(com.axway.ats.agent.components.system.operations.clients.InternalProcessOperations) AgentException(com.axway.ats.agent.core.exceptions.AgentException) ProcessExecutorException(com.axway.ats.common.process.ProcessExecutorException) Validator(com.axway.ats.core.validation.Validator) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 4 with ProcessExecutorException

use of com.axway.ats.common.process.ProcessExecutorException in project ats-framework by Axway.

the class LocalProcessExecutor method execute.

/**
     * Execute process and optionally wait for its completion
     * @param waitForCompletion true - wait to complete
     */
public void execute(boolean waitForCompletion) {
    if (!suppressLogMessages) {
        log.info("Executing '" + commandDescription + "'. We will " + (waitForCompletion ? "wait" : "not wait") + " for its completion");
    }
    try {
        if (workDirectory != null) {
            processBuilder.directory(new File(workDirectory));
        }
        this.theProcess = processBuilder.start();
        errorReaderThread = new ProcessOutputReader(caller, "ERROR OUTPUT", this.theProcess, this.theProcess.getErrorStream(), logErrorOutput, errorOutputFile, waitForCompletion);
        outputReaderThread = new ProcessOutputReader(caller, "STANDARD OUTPUT", this.theProcess, this.theProcess.getInputStream(), logStandardOutput, standardOutputFile, waitForCompletion);
        errorReaderThread.start();
        outputReaderThread.start();
        if (doNotUseStandardInput) {
            IoUtils.closeStream(theProcess.getOutputStream(), "Could not close process input stream");
        }
        if (waitForCompletion) {
            // wait until the external process finish
            theProcess.waitFor();
            if (!suppressLogMessages) {
                log.info("The execution of '" + commandDescription + "' finished with exit code " + this.theProcess.exitValue());
            }
            // tell the stream readers to stop reading
            errorReaderThread.setExternalProcessIsOver();
            outputReaderThread.setExternalProcessIsOver();
        }
    } catch (Exception e) {
        String message = "Error executing '" + commandDescription + "': " + e.getMessage();
        log.error(message);
        throw new ProcessExecutorException(message, e);
    } finally {
        if (theProcess != null) {
            // close input stream for the process since otherwise PIPEs leak
            // this code should be repositioned if we add support for writing data to the process
            IoUtils.closeStream(theProcess.getOutputStream(), "Could not close process input stream");
        }
    }
}
Also used : ProcessExecutorException(com.axway.ats.common.process.ProcessExecutorException) File(java.io.File) IOException(java.io.IOException) ProcessExecutorException(com.axway.ats.common.process.ProcessExecutorException)

Aggregations

ProcessExecutorException (com.axway.ats.common.process.ProcessExecutorException)4 InternalProcessOperations (com.axway.ats.agent.components.system.operations.clients.InternalProcessOperations)1 AgentException (com.axway.ats.agent.core.exceptions.AgentException)1 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 Validator (com.axway.ats.core.validation.Validator)1 File (java.io.File)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 WinProcess (org.jvnet.winp.WinProcess)1