Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Housekeeping. Added some validation for the daemon timeout.

  • Loading branch information...
commit b758a3c9fafb08d0f88a993303cbb5650fe6a5cd 1 parent 1c69cb5
@szczepiq szczepiq authored
View
6 subprojects/launcher/src/main/java/org/gradle/launcher/daemon/DaemonMain.java
@@ -77,10 +77,6 @@ private static void redirectOutputsAndInput(StartParameter startParameter) throw
}
private static int getIdleTimeout(StartParameter startParameter) {
- //TODO SF - very simple/no validation
- String timeoutProperty = startParameter.getSystemPropertiesArgs().get(DaemonTimeout.TIMEOUT_PROPERTY);
- int idleTimeout = (timeoutProperty != null)? Integer.parseInt(timeoutProperty) : 3 * 60 * 60 * 1000;
- return idleTimeout;
+ return new DaemonTimeout(startParameter.getSystemPropertiesArgs()).getIdleTimeout();
}
-
}
View
34 subprojects/launcher/src/main/java/org/gradle/launcher/daemon/DaemonTimeout.java
@@ -15,6 +15,9 @@
*/
package org.gradle.launcher.daemon;
+import org.gradle.api.GradleException;
+
+import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -22,9 +25,12 @@
* @author: Szczepan Faber, created at: 8/29/11
*/
public class DaemonTimeout {
- public static final String TIMEOUT_PROPERTY = "org.gradle.daemon.idletimeout";
- private int idleTimeout;
+ private static final String TIMEOUT_PROPERTY = "org.gradle.daemon.idletimeout";
+ private final int idleTimeout;
+ /**
+ * parses input vm params and looks for the timeout property. If not found the default is used.
+ */
public DaemonTimeout(String vmParams, int defaultIdleTimeout) {
String p = readProperty(vmParams);
if (p != null) {
@@ -34,6 +40,21 @@ public DaemonTimeout(String vmParams, int defaultIdleTimeout) {
}
}
+ /**
+ * throws exception if timeout property is not in the properties or when it is not a valid int
+ */
+ public DaemonTimeout(Map<String, String> sysProperties) {
+ String timeoutProperty = sysProperties.get(TIMEOUT_PROPERTY);
+ if (timeoutProperty == null) {
+ throw new GradleException("System property: " + TIMEOUT_PROPERTY + " not found. It needs to be provided.");
+ }
+ try {
+ idleTimeout = Integer.parseInt(timeoutProperty);
+ } catch (Exception e) {
+ throw new GradleException(String.format("Unable to parse %s sys property. The value should be an int but is: %s", TIMEOUT_PROPERTY, timeoutProperty));
+ }
+ }
+
private String readProperty(String vmParams) {
String idleDaemonTimeoutArg = null;
if (vmParams != null) {
@@ -45,7 +66,14 @@ private String readProperty(String vmParams) {
return idleDaemonTimeoutArg;
}
- public String toArg() {
+ public int getIdleTimeout() {
+ return idleTimeout;
+ }
+
+ /**
+ * returns sys arg in proper console format
+ */
+ public String toSysArg() {
return "-D" + TIMEOUT_PROPERTY + "=" + idleTimeout;
}
}
View
2  subprojects/launcher/src/main/java/org/gradle/launcher/daemon/ExternalDaemonConnector.java
@@ -70,7 +70,7 @@ protected void startDaemon() {
//TODO SF daemon server should use all gradle opts (that requires more digging & windows validation) but it is a part of a different story
//for now I only pass the idle timeout
DaemonTimeout timeout = new DaemonTimeout(System.getenv("GRADLE_OPTS"), idleTimeout);
- daemonArgs.add(timeout.toArg());
+ daemonArgs.add(timeout.toSysArg());
DaemonStartAction daemon = new DaemonStartAction();
daemon.args(daemonArgs);
View
31 subprojects/launcher/src/test/groovy/org/gradle/launcher/daemon/DaemonTimeoutTest.groovy
@@ -16,17 +16,38 @@
package org.gradle.launcher.daemon
import spock.lang.Specification
+import org.gradle.api.GradleException
/**
* @author: Szczepan Faber, created at: 8/29/11
*/
class DaemonTimeoutTest extends Specification {
- def "knows timeout"() {
+ def "turns timeout into system property string"() {
expect:
- "-Dorg.gradle.daemon.idletimeout=1000" == new DaemonTimeout(null, 1000).toArg()
- "-Dorg.gradle.daemon.idletimeout=1000" == new DaemonTimeout("-Dfoo=bar", 1000).toArg()
- "-Dorg.gradle.daemon.idletimeout=1000" == new DaemonTimeout("-Dorg.gradle.daemon.idletimeout=foo", 1000).toArg()
- "-Dorg.gradle.daemon.idletimeout=2000" == new DaemonTimeout("-Dorg.gradle.daemon.idletimeout=2000", 1000).toArg()
+ "-Dorg.gradle.daemon.idletimeout=1000" == new DaemonTimeout(null, 1000).toSysArg()
+ "-Dorg.gradle.daemon.idletimeout=1000" == new DaemonTimeout("-Dfoo=bar", 1000).toSysArg()
+ "-Dorg.gradle.daemon.idletimeout=1000" == new DaemonTimeout("-Dorg.gradle.daemon.idletimeout=foo", 1000).toSysArg()
+ "-Dorg.gradle.daemon.idletimeout=2000" == new DaemonTimeout("-Dorg.gradle.daemon.idletimeout=2000", 1000).toSysArg()
+ }
+
+ def "reads and validates timeout"() {
+ expect:
+ 4000 == new DaemonTimeout(['org.gradle.daemon.idletimeout': '4000']).idleTimeout
+
+ when:
+ new DaemonTimeout(['org.gradle.daemon.idletimeout': 'asdf']).idleTimeout
+
+ then:
+ def ex = thrown(GradleException)
+ ex.message.contains 'org.gradle.daemon.idletimeout'
+ ex.message.contains 'asdf'
+
+ when:
+ new DaemonTimeout([:]).idleTimeout
+
+ then:
+ ex = thrown(GradleException)
+ ex.message.contains 'org.gradle.daemon.idletimeout'
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.