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..f5dd466 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,18 +24,27 @@ 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) 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)) { 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()