Skip to content

Commit

Permalink
added a safer shutdown mechanism, added color to all messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir Kibbar committed Jun 1, 2015
1 parent a41737a commit 98a0cf1
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 18 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ an ElasticSearch gradle plugin for integration tests with ElasticSearch
maven { url "http://dl.bintray.com/amirk/maven" }
}
dependencies {
classpath("ajk.gradle.elastic:gradle-elastic-plugin:0.0.1")
classpath("ajk.gradle.elastic:gradle-elastic-plugin:0.0.2")
}
}
Expand Down Expand Up @@ -42,13 +42,17 @@ an ElasticSearch gradle plugin for integration tests with ElasticSearch
}
doLast {
stopElastic {}
stopElastic {
httpPort = 9200
}
}
}
gradle.taskGraph.afterTask { Task task, TaskState taskState ->
if (task.name == "integrationTests") {
stopElastic {}
stopElastic {
httpPort = 9200
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {
}

group = "ajk.gradle.elastic"
version = "0.0.1"
version = "0.0.2"

String repoName = 'bilberry'

Expand Down
2 changes: 1 addition & 1 deletion src/main/groovy/ajk/gradle/elastic/ElasticActions.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ElasticActions {
String linuxUrl = "https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-${version}.tar.gz"
String winUrl = "https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-${version}.zip"
String elasticPackage = isFamily(FAMILY_WINDOWS) ? winUrl : linuxUrl
File elasticFile = new File("$toolsDir/gradle/tools/elastic-${version}.zip")
File elasticFile = new File("$toolsDir/elastic-${version}.zip")

DownloadAction elasticDownload = new DownloadAction()
elasticDownload.dest(elasticFile)
Expand Down
7 changes: 7 additions & 0 deletions src/main/groovy/ajk/gradle/elastic/ElasticPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import org.gradle.api.Plugin
import org.gradle.api.Project

class ElasticPlugin implements Plugin<Project> {
static final String ESC = "${(char) 27}"
static final String CYAN = "${ESC}[36m"
static final String GREEN = "${ESC}[32m"
static final String YELLOW = "${ESC}[33m"
static final String RED = "${ESC}[31m"
static final String NORMAL = "${ESC}[0m"

@Override
void apply(Project project) {
project.extensions.create('startElastic', StartElasticExtension, project)
Expand Down
17 changes: 10 additions & 7 deletions src/main/groovy/ajk/gradle/elastic/StartElasticAction.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import org.gradle.api.Project
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional

import static ajk.gradle.elastic.ElasticPlugin.CYAN
import static ajk.gradle.elastic.ElasticPlugin.NORMAL
import static ajk.gradle.elastic.ElasticPlugin.RED
import static org.apache.tools.ant.taskdefs.condition.Os.FAMILY_WINDOWS
import static org.apache.tools.ant.taskdefs.condition.Os.isFamily

Expand Down Expand Up @@ -55,16 +58,16 @@ class StartElasticAction {
transportPort = transportPort ?: 9300
dataDir = dataDir ?: new File("$project.buildDir/elastic")
logsDir = logsDir ?: new File("$dataDir/logs")
println "* elastic: starting ElasticSearch at $elastic.home using http port $httpPort and tcp transport port $transportPort"
println "* elastic: ElasticSearch data directory: $dataDir"
println "* elastic: ElasticSearch logs directory: $logsDir"
println "${CYAN}* elastic:$NORMAL starting ElasticSearch at $elastic.home using http port $httpPort and tcp transport port $transportPort"
println "${CYAN}* elastic:$NORMAL ElasticSearch data directory: $dataDir"
println "${CYAN}* elastic:$NORMAL ElasticSearch logs directory: $logsDir"

ant.delete(failonerror: true, dir: dataDir)
ant.delete(failonerror: true, dir: logsDir)
logsDir.mkdirs()
dataDir.mkdirs()

File esScript = new File("$home/bin/elasticsearch${isFamily(FAMILY_WINDOWS) ? '.bat' : ''}")
File esScript = new File("${elastic.home}/bin/elasticsearch${isFamily(FAMILY_WINDOWS) ? '.bat' : ''}")

[
esScript.absolutePath,
Expand All @@ -81,7 +84,7 @@ class StartElasticAction {
"ES_MIN_MEM=128m"
], elastic.home)

println "* elastic: waiting for ElasticSearch to start"
println "${CYAN}* elastic:$NORMAL waiting for ElasticSearch to start"
ant.waitfor(maxwait: 2, maxwaitunit: "minute", timeoutproperty: "elasticTimeout") {
and {
socket(server: "localhost", port: transportPort)
Expand All @@ -90,10 +93,10 @@ class StartElasticAction {
}

if (ant.properties['elasticTimeout'] != null) {
println "* elastic: could not start ElasticSearch"
println "${RED}* elastic:$NORMAL could not start ElasticSearch"
throw new RuntimeException("failed to start ElasticSearch")
} else {
println "* elastic: ElasticSearch is now up"
println "${CYAN}* elastic:$NORMAL ElasticSearch is now up"
}
}
}
33 changes: 27 additions & 6 deletions src/main/groovy/ajk/gradle/elastic/StopElasticAction.groovy
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package ajk.gradle.elastic

import org.gradle.api.Project
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional

import static org.apache.http.client.fluent.Request.Get

import static ajk.gradle.elastic.ElasticPlugin.CYAN
import static ajk.gradle.elastic.ElasticPlugin.NORMAL
import static ajk.gradle.elastic.ElasticPlugin.YELLOW
import static ajk.gradle.elastic.ElasticPlugin.getRED
import static org.apache.http.client.fluent.Executor.newInstance
import org.gradle.api.Project
import static org.apache.http.client.fluent.Request.Post

class StopElasticAction {
@Input
Expand All @@ -20,8 +23,26 @@ class StopElasticAction {
}

void execute() {
println "* elastic: stopping ElasticSearch"

newInstance().execute(Get("http://localhost:${httpPort ?: 9200}/_shutdown"))
println "${CYAN}* elastic:$NORMAL stopping ElasticSearch"

try {
newInstance().execute(Post("http://localhost:${httpPort ?: 9200}/_shutdown"))

println "${CYAN}* elastic:$NORMAL waiting for ElasticSearch to shutdown"
ant.waitfor(maxwait: 2, maxwaitunit: "minute", timeoutproperty: "elasticTimeout") {
not {
ant.http(url: "http://localhost:$httpPort")
}
}

if (ant.properties['elasticTimeout'] != null) {
println "${RED}* elastic:$NORMAL could not stop ElasticSearch"
throw new RuntimeException("failed to stop ElasticSearch")
} else {
println "${CYAN}* elastic:$NORMAL ElasticSearch is now down"
}
} catch (ConnectException e) {
println "${CYAN}* elastic:$YELLOW warning - unable to stop elastic on http port ${httpPort ?: 9200}, ${e.message}$NORMAL"
}
}
}

0 comments on commit 98a0cf1

Please sign in to comment.