java/code/src/com/redhat/rhn/manager/satellite/SystemCommandThreadedExecutor.java | 159 ++++++++++ java/code/src/com/redhat/rhn/taskomatic/task/RhnJavaJob.java | 21 - java/spacewalk-java.spec | 6 rel-eng/packages/spacewalk-java | 2 4 files changed, 172 insertions(+), 16 deletions(-)
New commits: commit 71563f4e75b8454f90aa4c2e134c30599684a90b Author: Michael Mraka michael.mraka@redhat.com Date: Wed Apr 3 10:50:21 2013 +0200
Automatic commit of package [spacewalk-java] release [1.10.36-1].
diff --git a/java/spacewalk-java.spec b/java/spacewalk-java.spec index 56eca36..64ad047 100644 --- a/java/spacewalk-java.spec +++ b/java/spacewalk-java.spec @@ -28,7 +28,7 @@ Name: spacewalk-java Summary: Spacewalk Java site packages Group: Applications/Internet License: GPLv2 -Version: 1.10.35 +Version: 1.10.36 Release: 1%{?dist} URL: https://fedorahosted.org/spacewalk Source0: https://fedorahosted.org/releases/s/p/spacewalk/%%7Bname%7D-%%7Bversion%7D.t... @@ -678,6 +678,10 @@ fi %{jardir}/postgresql-jdbc.jar
%changelog +* Wed Apr 03 2013 Michael Mraka michael.mraka@redhat.com 1.10.36-1 +- 820612 - implemented executor with paralled stdout and stderr reading +- looking for dtd files locally insted of redirecting for them + * Thu Mar 28 2013 Jan Pazdziora 1.10.35-1 - Fixing checkstyle.
diff --git a/rel-eng/packages/spacewalk-java b/rel-eng/packages/spacewalk-java index 1bb5e98..6d3abef 100644 --- a/rel-eng/packages/spacewalk-java +++ b/rel-eng/packages/spacewalk-java @@ -1 +1 @@ -1.10.35-1 java/ +1.10.36-1 java/
commit 1e7b903cbcb421a2e4eb74a0590618a71e0a263d Author: Michael Mraka michael.mraka@redhat.com Date: Tue Apr 2 13:44:16 2013 +0200
820612 - implemented executor with paralled stdout and stderr reading
diff --git a/java/code/src/com/redhat/rhn/manager/satellite/SystemCommandThreadedExecutor.java b/java/code/src/com/redhat/rhn/manager/satellite/SystemCommandThreadedExecutor.java new file mode 100644 index 0000000..1417353 --- /dev/null +++ b/java/code/src/com/redhat/rhn/manager/satellite/SystemCommandThreadedExecutor.java @@ -0,0 +1,159 @@ +/** + * Copyright (c) 2013 Red Hat, Inc. + * + * This software is licensed to you under the GNU General Public License, + * version 2 (GPLv2). There is NO WARRANTY for this software, express or + * implied, including the implied warranties of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 + * along with this software; if not, see + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. + * + * Red Hat trademarks are not licensed under GPLv2. No permission is + * granted to use or replicate Red Hat trademarks that are incorporated + * in this software or its documentation. + */ +package com.redhat.rhn.manager.satellite; + +import org.apache.log4j.Logger; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Arrays; + +/** + * SystemCommandThreadedExecutor - implementation of the Executor interface that + * will take in the list of arguments and call Runtime.exec(). + */ +public class SystemCommandThreadedExecutor implements Executor { + + private boolean logError; + + private class StreamThread extends Thread { + private InputStream inputStream; + private boolean logError; + private Logger logger; + public StreamThread(InputStream in, boolean err, Logger log) { + inputStream = in; + logError = err; + logger = log; + } + public void run() { + StringBuffer sb = new StringBuffer(); + try { + BufferedReader input = new BufferedReader( + new InputStreamReader(inputStream)); + + String line; + while ((line = input.readLine()) != null) { + sb.append(line); + sb.append('\n'); + } + } + catch (IOException e) { + logger.warn("Error reading from process ", e); + } + if (sb.length() > 0) { + if (logError) { + logger.error(sb.toString()); + } + else { + logger.info(sb.toString()); + } + } + } + } + + /** + * Logger for this class + */ + private final Logger logger; + + /** + * Constructor + * @param log Desired logger + */ + public SystemCommandThreadedExecutor(Logger log) { + logError = true; + logger = log; + } + + /** + * Whether to log errors as an ERROR within log4j + * Even if this is set to false, the error will still be logged + * with DEBUG priority + * @param toLog true to log as an error + */ + public void setLogError(boolean toLog) { + logError = toLog; + } + + + /** + * {@inheritDoc} + */ + public int execute(String[] args) { + if (logger.isDebugEnabled()) { + logger.debug("execute(String[] args=" + Arrays.asList(args) + ") - start"); + } + + int retval; + Runtime r = Runtime.getRuntime(); + try { + if (logger.isDebugEnabled()) { + logger.debug("execute() - Calling r.exec .."); + } + Process p = r.exec(args); + + Thread inStream = new StreamThread( + p.getInputStream(), false, logger); + Thread errStream = new StreamThread( + p.getErrorStream(), logError, logger); + + inStream.start(); + errStream.start(); + + try { + if (logger.isDebugEnabled()) { + logger.debug("execute() - Calling p.waitfor .."); + } + retval = p.waitFor(); + inStream.join(); + errStream.join(); + } + catch (InterruptedException e) { + throw new RuntimeException( + "InterruptedException while trying to exec: " + e); + } + } + catch (IOException ioe) { + logger.error("execute(String[])", ioe); + + String message = ""; + for (int i = 0; i < args.length; i++) { + message = message + args[i] + " "; + } + logger.error("IOException while trying to exec: " + message, ioe); + throw new RuntimeException( + "IOException while trying to exec: " + message, ioe); + } + + return retval; + } + + /** + * {@inheritDoc} + */ + public String getLastCommandOutput() { + return ""; + } + + /** + * {@inheritDoc} + */ + public String getLastCommandErrorMessage() { + return ""; + } + +} diff --git a/java/code/src/com/redhat/rhn/taskomatic/task/RhnJavaJob.java b/java/code/src/com/redhat/rhn/taskomatic/task/RhnJavaJob.java index 7209bb0..084376d 100644 --- a/java/code/src/com/redhat/rhn/taskomatic/task/RhnJavaJob.java +++ b/java/code/src/com/redhat/rhn/taskomatic/task/RhnJavaJob.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2010 Red Hat, Inc. + * Copyright (c) 2010--2013 Red Hat, Inc. * * This software is licensed to you under the GNU General Public License, * version 2 (GPLv2). There is NO WARRANTY for this software, express or @@ -15,7 +15,7 @@ package com.redhat.rhn.taskomatic.task;
import com.redhat.rhn.common.hibernate.HibernateFactory; -import com.redhat.rhn.manager.satellite.SystemCommandExecutor; +import com.redhat.rhn.manager.satellite.SystemCommandThreadedExecutor; import com.redhat.rhn.taskomatic.TaskoRun;
import org.apache.log4j.FileAppender; @@ -26,6 +26,8 @@ import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException;
import java.io.IOException; +import java.util.Arrays; +
/** @@ -96,22 +98,13 @@ public abstract class RhnJavaJob implements RhnJob { protected void executeExtCmd(String[] args) throws JobExecutionException {
- SystemCommandExecutor ce = new SystemCommandExecutor(); + SystemCommandThreadedExecutor ce = new SystemCommandThreadedExecutor(getLogger()); int exitCode = ce.execute(args);
- String cmdOutput = ce.getLastCommandOutput(); - String cmdError = ce.getLastCommandErrorMessage(); - if (!"".equals(cmdOutput)) { - log.info(cmdOutput); - } - if (!"".equals(cmdError)) { - log.error(cmdError); - } - if (exitCode != 0) { - // use stderr, unless it is empty, then use stdout throw new JobExecutionException( - cmdError.isEmpty() ? cmdOutput : cmdError); + "Command '" + Arrays.asList(args) + "' exited with error code " + + exitCode); } } }
spacewalk-commits@lists.fedorahosted.org