Skip to content

Commit

Permalink
[MEXEC-mojohausGH-128] - <timeout> configuration parameter
Browse files Browse the repository at this point in the history
Fixes mojohaus#128.

A new `<timeout>` configuration parameter (default: 0, which means
"none") allows to explicitly terminate a process running for longer that the given time in
minutes, and will fail the build.

Signed-off-by: Markus KARG <[email protected]>
  • Loading branch information
mkarg committed Apr 16, 2019
1 parent 3296fc8 commit f4a0194
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/it/projects/mexec-gh-128/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
invoker.goals = clean package
invoker.buildResult = failure
invoker.debug = true
41 changes: 41 additions & 0 deletions src/it/projects/mexec-gh-128/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.codehaus.mojo.exec.it</groupId>
<artifactId>parent</artifactId>
<version>0.1</version>
</parent>

<artifactId>mexec-gh-128</artifactId>
<version>0.0.1-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>@pom.version@</version>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<timeout>1000</timeout>
<executable>${JAVA_HOME}/bin/java</executable>
<arguments>
<argument>-cp</argument>
<argument>target/classes</argument>
<argument>Main</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
15 changes: 15 additions & 0 deletions src/it/projects/mexec-gh-128/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import java.io.FileWriter;
import java.io.Writer;
import java.net.URL;

public class Main
{
public static void main( String[] args ) throws Exception
{
/*
* Intentionally hangs for "very long" time to simulate a completely stuck executable,
* but let the time be so short that the IT would break "quickly" in any case.
*/
Thread.sleep( 10000 );
}
}
29 changes: 29 additions & 0 deletions src/it/projects/mexec-gh-128/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.io.*
import java.util.*

t = new IntegrationBase()

def buildLog = new File( basedir, "build.log" )

t.checkExistenceAndContentOfAFile(buildLog, [
"[ERROR] Timeout. Process runs longer than 1000 ms."
])
43 changes: 36 additions & 7 deletions src/main/java/org/codehaus/mojo/exec/ExecMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.ExecuteResultHandler;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.exec.Executor;
import org.apache.commons.exec.OS;
import org.apache.commons.exec.ProcessDestroyer;
Expand Down Expand Up @@ -103,6 +104,21 @@ public class ExecMojo
@Parameter( property = "exec.executable" )
private String executable;

/**
* <p>
* Timeout in full milliseconds, default is {@code 0}.
* <p>
* <p>
* When set to a value larger than zero, the executable is forcefully
* terminated if it did not finish within this time, and the build will
* fail.
* </p>
*
* @since 3.0.0
*/
@Parameter( property = "exec.timeout", defaultValue = "0" )
private int timeout;

/**
* <p>
* The toolchain. If omitted, <code>"jdk"</code> is assumed.
Expand Down Expand Up @@ -148,7 +164,7 @@ public class ExecMojo
*/
@Parameter( readonly = true, required = true, defaultValue = "${basedir}" )
private File basedir;

/**
* @since 3.0.0
*/
Expand Down Expand Up @@ -226,7 +242,7 @@ public class ExecMojo
private boolean asyncDestroyOnShutdown = true;

public static final String CLASSPATH_TOKEN = "%classpath";

public static final String MODULEPATH_TOKEN = "%modulepath";

/**
Expand Down Expand Up @@ -293,6 +309,10 @@ else if ( !StringUtils.isEmpty( argsProp ) )
commandLine.addArguments( args, false );

Executor exec = new DefaultExecutor();
if ( this.timeout > 0 )
{
exec.setWatchdog( new ExecuteWatchdog( this.timeout ) );
}
exec.setWorkingDirectory( workingDirectory );
fillSuccessCodes( exec );

Expand Down Expand Up @@ -334,8 +354,17 @@ else if ( !StringUtils.isEmpty( argsProp ) )
}
catch ( ExecuteException e )
{
getLog().error( "Command execution failed.", e );
throw new MojoExecutionException( "Command execution failed.", e );
if ( exec.getWatchdog() != null && exec.getWatchdog().killedProcess() )
{
final String message = "Timeout. Process runs longer than " + this.timeout + " ms.";
getLog().error( message );
throw new MojoExecutionException( message, e );
}
else
{
getLog().error( "Command execution failed.", e );
throw new MojoExecutionException( "Command execution failed.", e );
}
}
catch ( IOException e )
{
Expand Down Expand Up @@ -471,7 +500,7 @@ private void handleArguments( List<String> commandArguments )
throws MojoExecutionException, IOException
{
String specialArg = null;

for ( int i = 0; i < arguments.size(); i++ )
{
Object argument = arguments.get( i );
Expand Down Expand Up @@ -972,12 +1001,12 @@ private File createJar( List<String> classPath, String mainClass )

return file;
}

private void createArgFile( String filePath, List<String> lines )
throws IOException
{
final String EOL = System.getProperty( "line.separator", "\\n" );

FileWriter writer = null;
try
{
Expand Down

0 comments on commit f4a0194

Please sign in to comment.