From cf884d12aaf0d0655d929305a5f468ef1879ba48 Mon Sep 17 00:00:00 2001 From: Pete Wagner Date: Sun, 27 Apr 2014 15:55:10 -0400 Subject: [PATCH 1/2] LockFile parameter As a user, I want to specify lockFile, so that I can spawn multiple processes in the same directory. --- .../com/wiredforcode/gradle/spawn/KillProcessTask.groovy | 5 +++-- .../com/wiredforcode/gradle/spawn/SpawnProcessTask.groovy | 5 +++-- .../wiredforcode/gradle/spawn/KillProcessTaskSpec.groovy | 4 +--- .../wiredforcode/gradle/spawn/SpawnProcessTaskSpec.groovy | 6 ++---- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/main/groovy/com/wiredforcode/gradle/spawn/KillProcessTask.groovy b/src/main/groovy/com/wiredforcode/gradle/spawn/KillProcessTask.groovy index 750be30..85555d7 100644 --- a/src/main/groovy/com/wiredforcode/gradle/spawn/KillProcessTask.groovy +++ b/src/main/groovy/com/wiredforcode/gradle/spawn/KillProcessTask.groovy @@ -5,13 +5,14 @@ import org.gradle.api.tasks.TaskAction class KillProcessTask extends DefaultTask { - public static final String LOCK_FILE = '.pid.lock' + public static final String DEFAULT_LOCK_FILE = '.pid.lock' String directory = '.' + String lockFile = DEFAULT_LOCK_FILE @TaskAction void kill() { - def pidFile = new File(directory, LOCK_FILE) + def pidFile = new File(directory, lockFile) if(!pidFile.exists()) throw new GradleException("No server running!") def pid = pidFile.text diff --git a/src/main/groovy/com/wiredforcode/gradle/spawn/SpawnProcessTask.groovy b/src/main/groovy/com/wiredforcode/gradle/spawn/SpawnProcessTask.groovy index dc72451..95ef4a3 100644 --- a/src/main/groovy/com/wiredforcode/gradle/spawn/SpawnProcessTask.groovy +++ b/src/main/groovy/com/wiredforcode/gradle/spawn/SpawnProcessTask.groovy @@ -6,12 +6,13 @@ import org.gradle.api.tasks.TaskAction class SpawnProcessTask extends DefaultTask { - public static final String LOCK_FILE = '.pid.lock' + public static final String DEFAULT_LOCK_FILE = '.pid.lock' public static final String PID_FIELD = 'pid' String command String ready String directory = '.' + String lockFile = DEFAULT_LOCK_FILE SpawnProcessTask(){ description = "Spawn a new server process in the background." @@ -23,7 +24,7 @@ class SpawnProcessTask extends DefaultTask { throw new GradleException("Ensure that mandatory fields command and ready are set.") } - def pidFile = new File(directory, LOCK_FILE) + def pidFile = new File(directory, lockFile) if(pidFile.exists()) throw new GradleException("Server already running!") Process process = buildProcess(directory, command) diff --git a/src/test/groovy/com/wiredforcode/gradle/spawn/KillProcessTaskSpec.groovy b/src/test/groovy/com/wiredforcode/gradle/spawn/KillProcessTaskSpec.groovy index 75cda20..2dc6b5f 100644 --- a/src/test/groovy/com/wiredforcode/gradle/spawn/KillProcessTaskSpec.groovy +++ b/src/test/groovy/com/wiredforcode/gradle/spawn/KillProcessTaskSpec.groovy @@ -5,8 +5,6 @@ import org.gradle.api.Project import org.gradle.testfixtures.ProjectBuilder import spock.lang.Specification -import static com.wiredforcode.gradle.spawn.SpawnProcessTask.LOCK_FILE - class KillProcessTaskSpec extends Specification { static final SPAWN_PROCESS_TASK_NAME = "spawnProcess" @@ -48,7 +46,7 @@ class KillProcessTaskSpec extends Specification { when: spawnTask.spawn() - def lockFile = new File(directoryPath, LOCK_FILE) + def lockFile = new File(directoryPath, spawnTask.lockFile) then: lockFile.exists() diff --git a/src/test/groovy/com/wiredforcode/gradle/spawn/SpawnProcessTaskSpec.groovy b/src/test/groovy/com/wiredforcode/gradle/spawn/SpawnProcessTaskSpec.groovy index 6646b99..d5b3398 100644 --- a/src/test/groovy/com/wiredforcode/gradle/spawn/SpawnProcessTaskSpec.groovy +++ b/src/test/groovy/com/wiredforcode/gradle/spawn/SpawnProcessTaskSpec.groovy @@ -5,8 +5,6 @@ import org.gradle.api.Project import org.gradle.testfixtures.ProjectBuilder import spock.lang.Specification -import static com.wiredforcode.gradle.spawn.SpawnProcessTask.LOCK_FILE - class SpawnProcessTaskSpec extends Specification { static final SPAWN_PROCESS_TASK_NAME = 'spawnProcess' @@ -51,7 +49,7 @@ class SpawnProcessTaskSpec extends Specification { task.spawn() then: - new File(directory, LOCK_FILE).exists() + new File(directory, task.lockFile).exists() } void "should check if pid file already exists"() { @@ -59,7 +57,7 @@ class SpawnProcessTaskSpec extends Specification { task.directory = directory.toString() and: - new File(directory, LOCK_FILE).createNewFile() + new File(directory, task.lockFile).createNewFile() when: task.spawn() From 740d59f5f9d8a3d35680b1717ec811a2091bf399 Mon Sep 17 00:00:00 2001 From: Pete Wagner Date: Thu, 11 Dec 2014 08:20:28 -0500 Subject: [PATCH 2/2] Start a runaway thread to slurp stdout This prevents the spawned application from filling the buffer (and blocking). It is not pretty, but it solves my problem. --- .../gradle/spawn/SpawnProcessTask.groovy | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/groovy/com/wiredforcode/gradle/spawn/SpawnProcessTask.groovy b/src/main/groovy/com/wiredforcode/gradle/spawn/SpawnProcessTask.groovy index 95ef4a3..f5dd466 100644 --- a/src/main/groovy/com/wiredforcode/gradle/spawn/SpawnProcessTask.groovy +++ b/src/main/groovy/com/wiredforcode/gradle/spawn/SpawnProcessTask.groovy @@ -30,12 +30,21 @@ class SpawnProcessTask extends DefaultTask { Process process = buildProcess(directory, command) int pid = extractPidFromProcess(process) stampLockFile(pidFile, pid) - waitFor(process) + + def reader = new BufferedReader(new InputStreamReader(process.getInputStream())) + waitFor(reader) + def slurpThread = new Thread() { + void run() { + def line + while ((line = reader.readLine()) != null) { + } + } + } + slurpThread.start() } - private waitFor(Process process) { + private waitFor(def reader) { def line - def reader = new BufferedReader(new InputStreamReader(process.getInputStream())) while ((line = reader.readLine()) != null) { logger.quiet line if (line.contains(ready)) {