psxpaul / gradle-execfork-plugin

A gradle plugin that lets you define Exec and JavaExec tasks that run in the background
Apache License 2.0
61 stars 20 forks source link

Forked process doesn't get killed when pressing CTRL+C #1

Open edgraaff opened 7 years ago

edgraaff commented 7 years ago

First of all, thanks for this plugin!

I'm trying to get it working for running a Spring Boot application (gradle bootRun) and running watchify parallel. So far I have got this:

buildscript {
  ext {
    springBootVersion = '1.5.1.RELEASE'
  }

  repositories {
    mavenCentral()
  }

  dependencies {
    classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
    classpath 'com.github.psxpaul:gradle-execfork-plugin:0.1.4'
  }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'gradle-execfork-plugin'

repositories {
  mavenCentral()
}

dependencies {
  compile "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"
  compile "org.springframework.boot:spring-boot-devtools:${springBootVersion}"
}

task npmInstall(type: Exec) {
  workingDir 'frontend'
  commandLine 'npm', 'install'
}

task watchifyDaemon(type: com.github.psxpaul.task.ExecFork, dependsOn: npmInstall) {
  commandLine = 'npm'
  args = [ 'run', 'watchify' ]
  workingDir = "$projectDir/frontend"
  stopAfter = bootRun
}

task buildJs(type:Exec, dependsOn: npmInstall) {
  workingDir 'frontend'
  commandLine 'npm', 'run', 'build'
}

bootRun {
  addResources = true
}

bootRun.dependsOn watchifyDaemon
jar.dependsOn buildJs

The frontend folder within the project is a npm project. The build and watchify commands compile JS to src/main/static. So far so good; gradle build invokes buildJs (which doesn't have to run in parallel) and gradle bootRun calls watchifyDaemon. This bootRun task, however, never ends since it starts a Tomcat until the developer presses CTRL+C. It appears that the watchifyDaemon task doesn't get killed along.

Any idea? Thanks in advance!

psxpaul commented 7 years ago

Thanks for the feedback, it's nice to see that others are using this plugin.

I just pushed version 0.1.5 that might fix this for you, if you'd like to give it a try. I added the plugin to the official gradle plugin repo as well, so now you can use the plugins block to apply the plugin:

plugins {
  id 'com.github.psxpaul.execfork' version '0.1.5'
}
edgraaff commented 7 years ago

Thanks for your quick support! Unfortunately it didn't fix; when stopping the Spring Boot 'bootRun' task by hitting CTRL+C (or if one would hit Stop in Eclipse/IDEA), the node process is still running. It is a great improvement that you have pushed it to the official repo since it now takes less code to apply your plugin!

Normally I would try to fix it myself, Java and Groovy is daily matter but Gradle plugins are completely new to me. Nevertheless I will give it a go to trace what exactly is not causing the forked process to stop.

psxpaul commented 7 years ago

How long did you wait for the process to stop? In my testing, I noticed it generally took a few seconds to kill the spawned process. I found a few references that indicate that this is the case when the gradle daemon is used:

https://stackoverflow.com/questions/22671927/shutdown-hook-of-java-process-doesnt-trigger-in-gradle-daemon https://github.com/gradle/gradle/issues/1128 https://github.com/gradle/gradle/issues/1109

It also seems to not pipe stdout to the console in this case.

edgraaff commented 7 years ago

You're right! When I set org.gradle.daemon=false in ~/.gradle/gradle.properties, stdout does appear and the forked process does get stopped when pressing ctrl+C.

With daemon enabled, it doesn't seem to stop though, not even after a minute. Disabling the daemon is not ideal, but at least it works for now.