Skip to content
This repository was archived by the owner on Apr 23, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -51,15 +49,15 @@ 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"() {
given:
task.directory = directory.toString()

and:
new File(directory, LOCK_FILE).createNewFile()
new File(directory, task.lockFile).createNewFile()

when:
task.spawn()
Expand Down