Search in sources :

Example 1 with StreamCopyThread

use of hudson.util.StreamCopyThread in project hudson-2.x by hudson.

the class Channels method forProcess.

public static Channel forProcess(String name, ExecutorService execService, final Process proc, OutputStream header) throws IOException {
    final Thread thread = new StreamCopyThread(name + " stderr", proc.getErrorStream(), header);
    thread.start();
    return new Channel(name, execService, proc.getInputStream(), proc.getOutputStream(), header) {

        /**
             * Kill the process when the channel is severed.
             */
        @Override
        protected synchronized void terminate(IOException e) {
            super.terminate(e);
            proc.destroy();
        // the stderr copier should exit by itself
        }

        @Override
        public synchronized void close() throws IOException {
            super.close();
            // wait for Maven to complete
            try {
                proc.waitFor();
                thread.join();
            } catch (InterruptedException e) {
                // process the interrupt later
                Thread.currentThread().interrupt();
            }
        }
    };
}
Also used : StreamCopyThread(hudson.util.StreamCopyThread) Channel(hudson.remoting.Channel) IOException(java.io.IOException) StreamCopyThread(hudson.util.StreamCopyThread)

Example 2 with StreamCopyThread

use of hudson.util.StreamCopyThread in project hudson-2.x by hudson.

the class WindowsRemoteLauncher method launch.

public Proc launch(ProcStarter ps) throws IOException {
    maskedPrintCommandLine(ps.cmds(), ps.masks(), ps.pwd());
    // TODO: environment variable handling
    String name = ps.cmds().toString();
    final Process proc;
    try {
        proc = launcher.launch(buildCommandLine(ps), ps.pwd().getRemote());
    } catch (JIException e) {
        throw new IOException2(e);
    } catch (InterruptedException e) {
        throw new IOException2(e);
    }
    final Thread t1 = new StreamCopyThread("stdout copier: " + name, proc.getInputStream(), ps.stdout(), false);
    t1.start();
    final Thread t2 = new StreamCopyThread("stdin copier: " + name, ps.stdin(), proc.getOutputStream(), true);
    t2.start();
    return new Proc() {

        public boolean isAlive() throws IOException, InterruptedException {
            try {
                proc.exitValue();
                return false;
            } catch (IllegalThreadStateException e) {
                return true;
            }
        }

        public void kill() throws IOException, InterruptedException {
            t1.interrupt();
            t2.interrupt();
            proc.destroy();
        }

        public int join() throws IOException, InterruptedException {
            try {
                t1.join();
                t2.join();
                return proc.waitFor();
            } finally {
                proc.destroy();
            }
        }
    };
}
Also used : Proc(hudson.Proc) StreamCopyThread(hudson.util.StreamCopyThread) JIException(org.jinterop.dcom.common.JIException) IOException2(hudson.util.IOException2) StreamCopyThread(hudson.util.StreamCopyThread)

Example 3 with StreamCopyThread

use of hudson.util.StreamCopyThread in project hudson-2.x by hudson.

the class CommandLauncher method launch.

@Override
public void launch(SlaveComputer computer, final TaskListener listener) {
    EnvVars _cookie = null;
    Process _proc = null;
    try {
        listener.getLogger().println(hudson.model.Messages.Slave_Launching(getTimestamp()));
        if (getCommand().trim().length() == 0) {
            listener.getLogger().println(Messages.CommandLauncher_NoLaunchCommand());
            return;
        }
        listener.getLogger().println("$ " + getCommand());
        ProcessBuilder pb = new ProcessBuilder(Util.tokenize(getCommand()));
        final EnvVars cookie = _cookie = EnvVars.createCookie();
        pb.environment().putAll(cookie);
        {
            // system defined variables
            String rootUrl = Hudson.getInstance().getRootUrl();
            if (rootUrl != null) {
                pb.environment().put("HUDSON_URL", rootUrl);
                pb.environment().put("SLAVEJAR_URL", rootUrl + "/jnlpJars/slave.jar");
            }
        }
        if (env != null) {
            pb.environment().putAll(env);
        }
        final Process proc = _proc = pb.start();
        // capture error information from stderr. this will terminate itself
        // when the process is killed.
        new StreamCopyThread("stderr copier for remote agent on " + computer.getDisplayName(), proc.getErrorStream(), listener.getLogger()).start();
        computer.setChannel(proc.getInputStream(), proc.getOutputStream(), listener.getLogger(), new Channel.Listener() {

            @Override
            public void onClosed(Channel channel, IOException cause) {
                try {
                    int exitCode = proc.exitValue();
                    if (exitCode != 0) {
                        listener.error("Process terminated with exit code " + exitCode);
                    }
                } catch (IllegalThreadStateException e) {
                // hasn't terminated yet
                }
                try {
                    ProcessTree.get().killAll(proc, cookie);
                } catch (InterruptedException e) {
                    LOGGER.log(Level.INFO, "interrupted", e);
                }
            }
        });
        LOGGER.info("slave agent launched for " + computer.getDisplayName());
    } catch (InterruptedException e) {
        e.printStackTrace(listener.error(Messages.ComputerLauncher_abortedLaunch()));
    } catch (RuntimeException e) {
        e.printStackTrace(listener.error(Messages.ComputerLauncher_unexpectedError()));
    } catch (Error e) {
        e.printStackTrace(listener.error(Messages.ComputerLauncher_unexpectedError()));
    } catch (IOException e) {
        Util.displayIOException(e, listener);
        String msg = Util.getWin32ErrorMessage(e);
        if (msg == null) {
            msg = "";
        } else {
            msg = " : " + msg;
        }
        msg = hudson.model.Messages.Slave_UnableToLaunch(computer.getDisplayName(), msg);
        LOGGER.log(Level.SEVERE, msg, e);
        e.printStackTrace(listener.error(msg));
        if (_proc != null)
            try {
                ProcessTree.get().killAll(_proc, _cookie);
            } catch (InterruptedException x) {
                x.printStackTrace(listener.error(Messages.ComputerLauncher_abortedLaunch()));
            }
    }
}
Also used : StreamCopyThread(hudson.util.StreamCopyThread) Channel(hudson.remoting.Channel) IOException(java.io.IOException) EnvVars(hudson.EnvVars)

Aggregations

StreamCopyThread (hudson.util.StreamCopyThread)3 Channel (hudson.remoting.Channel)2 IOException (java.io.IOException)2 EnvVars (hudson.EnvVars)1 Proc (hudson.Proc)1 IOException2 (hudson.util.IOException2)1 JIException (org.jinterop.dcom.common.JIException)1