Spawning processes from within hudson

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Spawning processes from within hudson

Sankaran, Nambi
Hi Kohsuke
 
I am invoking a python script from within a hudson plugin, through java's ProcessBuilder.
 
In plain java this works fine.
However, it hangs when ProcessBuilder is invoked from Hudson.
 
Is there any workaround?
 
                String[] commandArray = Util.tokenize(sb.toString());
                List<String> command = Arrays.asList(commandArray);
 
                ProcessBuilder builder = new ProcessBuilder(command);
                Process proc = builder.start();
 
               Scanner s = new Scanner(proc.getInputStream()).useDelimiter("<A href="file://\\n">\\n");
                while (s.hasNext()) {
                    PrintStream out = listener.getLogger();
                    out.println(s.nextLine());
                }
 
                try {
                    proc.waitFor();
                }catch (InterruptedException e) {
                    System.err.println("Process was interrupted");
                }
 
I followed the wiki that talkes about this issue, http://wiki.hudson-ci.org/display/HUDSON/Spawning+processes+from+build
It doesn't provide any workaround for doing this in java.
 
 
Thanks
Nambi
 
 
Reply | Threaded
Open this post in threaded view
|

Re: Spawning processes from within hudson

Kohsuke Kawaguchi
Administrator
Sankaran, Nambi wrote:
> Hi Kohsuke
>  
> I am invoking a python script from within a hudson plugin, through
> java's ProcessBuilder.
>  
> In plain java this works fine.
> However, it hangs when ProcessBuilder is invoked from Hudson.

I doubt if it's caused by Hudson. I suspect the process you forked is
behaving differently because of the different environment Hudson JVM
runs and your test case runs.

Also, your code doesn't drain stdout and stderr. That can very easily
cause a child process to hang if they start producing outputs.

> Is there any workaround?
>  
>                 String[] commandArray = Util.tokenize(sb.toString());
>                 List<String> command = Arrays.asList(commandArray);
>  
>                 ProcessBuilder builder = new ProcessBuilder(command);
>                 Process proc = builder.start();
>  
>                Scanner s = new
> Scanner(proc.getInputStream()).useDelimiter("\\n");
>                 while (s.hasNext()) {
>                     PrintStream out = listener.getLogger();
>                     out.println(s.nextLine());
>                 }
>  
>                 try {
>                     proc.waitFor();
>                 }catch (InterruptedException e) {
>                     System.err.println("Process was interrupted");
>                 }
>  
> I followed the wiki that talkes about this issue,
> http://wiki.hudson-ci.org/display/HUDSON/Spawning+processes+from+build
> It doesn't provide any workaround for doing this in java.
That page is about a process forking other processes that are going to
background. I don't think it's applicable here.

--
Kohsuke Kawaguchi
Sun Microsystems                   http://weblogs.java.net/blog/kohsuke/

smime.p7s (4K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

RE: Re: Spawning processes from within hudson

Sankaran, Nambi
Thanks Kohsuke

The program works fine, after the stdout and stderr are drained from the
process.

                String[] commandArray = Util.tokenize(sb.toString());
                List<String> command = Arrays.asList(commandArray);

                ProcessBuilder builder = new ProcessBuilder(command);
                builder.redirectErrorStream(true);
                Process proc = builder.start();


                StreamReader outReader = new StreamReader(
proc.getInputStream(), listener.getLogger());

                outReader.start();

              try {
                    proc.waitFor();
                }
                catch (InterruptedException e) {
                    System.err.println("Process was interrupted");
                }

                outReader.join();

       public class StreamReader extends Thread {
            InputStream in;
            PrintStream out;
           
            StreamReader( InputStream input, PrintStream output){
                in = input;
                out = output;
            }

            public void run(){

                try{

                    InputStreamReader isr = new InputStreamReader(in);
                    BufferedReader br = new BufferedReader(isr);
                    String line = null;
                    while( (line = br.readLine()) != null ){
                        out.println(line);
                    }

                }catch( IOException ioe){
                    ioe.printStackTrace();
                }
            }

-----Original Message-----
From: Kohsuke Kawaguchi [mailto:[hidden email]]
Sent: Wednesday, July 08, 2009 1:22 PM
To: [hidden email]
Subject: Re: Spawning processes from within hudson

Sankaran, Nambi wrote:
> Hi Kohsuke
>  
> I am invoking a python script from within a hudson plugin, through
> java's ProcessBuilder.
>  
> In plain java this works fine.
> However, it hangs when ProcessBuilder is invoked from Hudson.

I doubt if it's caused by Hudson. I suspect the process you forked is
behaving differently because of the different environment Hudson JVM
runs and your test case runs.

Also, your code doesn't drain stdout and stderr. That can very easily
cause a child process to hang if they start producing outputs.

> Is there any workaround?
>  
>                 String[] commandArray = Util.tokenize(sb.toString());
>                 List<String> command = Arrays.asList(commandArray);
>  
>                 ProcessBuilder builder = new ProcessBuilder(command);
>                 Process proc = builder.start();
>  
>                Scanner s = new
> Scanner(proc.getInputStream()).useDelimiter("\\n");
>                 while (s.hasNext()) {
>                     PrintStream out = listener.getLogger();
>                     out.println(s.nextLine());
>                 }
>  
>                 try {
>                     proc.waitFor();
>                 }catch (InterruptedException e) {
>                     System.err.println("Process was interrupted");
>                 }
>  
> I followed the wiki that talkes about this issue,
> http://wiki.hudson-ci.org/display/HUDSON/Spawning+processes+from+build
> It doesn't provide any workaround for doing this in java.

That page is about a process forking other processes that are going to
background. I don't think it's applicable here.

--
Kohsuke Kawaguchi
Sun Microsystems                   http://weblogs.java.net/blog/kohsuke/

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]