Skip to content

Commit

Permalink
#630 implement stop postgresql
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Apr 28, 2019
1 parent 61a4a3b commit 7001290
Showing 1 changed file with 37 additions and 11 deletions.
48 changes: 37 additions & 11 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import com.opentable.db.postgres.embedded.EmbeddedPostgres
import com.opentable.db.postgres.embedded.PgBinaryResolver
import org.apache.commons.io.FileUtils
import org.apache.commons.io.IOUtils
import org.apache.commons.lang3.SystemUtils
import org.apache.tools.ant.filters.ReplaceTokens
import org.springframework.jdbc.core.JdbcTemplate

Expand All @@ -15,7 +18,6 @@ import static java.lang.String.format
buildscript {

dependencies {
//classpath 'ru.yandex.qatools.embed:postgresql-embedded:2.9'
classpath 'com.opentable.components:otj-pg-embedded:0.13.1'
classpath 'org.postgresql:postgresql:42.2.5'
classpath "org.springframework:spring-jdbc:$springVersion"
Expand Down Expand Up @@ -146,7 +148,6 @@ dependencies {
compile 'org.imgscalr:imgscalr-lib:4.2'
compile 'org.aspectj:aspectjweaver:1.9.2'

//testCompile "ru.yandex.qatools.embed:postgresql-embedded:2.9" //we leave it to the v2.9 for now, since some user had issues on Windows
testCompile 'com.opentable.components:otj-pg-embedded:0.13.1'

compileOnly "javax.servlet:javax.servlet-api:4.0.1"
Expand Down Expand Up @@ -359,18 +360,43 @@ task startEmbeddedPgSQL {
}
}

// code imported from EmbeddedPostgres :D
String pgBin(File pgDirBase, String binaryName) {
final pgDir = pgDirBase.listFiles(new FileFilter() {
@Override
boolean accept(File file) {
return file.getName().startsWith("PG-") && file.isDirectory()
}
})[0]
final String extension = SystemUtils.IS_OS_WINDOWS ? ".exe" : ""
return new File(pgDir, "bin/" + binaryName + extension).getPath()
}

void system(String... command) {
final ProcessBuilder builder = new ProcessBuilder(command)
final Process process = builder.start()
if (0 != process.waitFor()) {
throw new IllegalStateException(String.format("Process %s failed%n%s", Arrays.asList(command), IOUtils.toString(process.getErrorStream())))
}
}

void pgCtl(File binDir, File dataDir, String action) {
system(pgBin(binDir, "pg_ctl"), "-D", dataDir.getPath(), action, "-m", "fast", "-t", "5", "-w")
}
//

task stopEmbeddedPgSQL {
doLast {
def descriptor = Paths.get(".", "alfio-itest", "pgsql-descriptor");
def descriptor = Paths.get(".", "alfio-itest", "pgsql-descriptor")
def r = Files.readAllLines(descriptor)
def dataDir = r.get(0)
def binariesDir = r.get(1)

System.out.println("dataDir is " + dataDir)
System.out.println("binariesDir is " + binariesDir)
//Files.deleteIfExists(descriptor)
//Runtime.runtime.exec("kill -9 " + pid)
//System.out.println("Killed pgsql with pid " + pid)
def dataDir = new File(r.get(0))
def binariesDir = new File(r.get(1))

pgCtl(binariesDir, dataDir, "stop")
Files.deleteIfExists(descriptor)
FileUtils.deleteDirectory(dataDir)
FileUtils.deleteDirectory(binariesDir)
System.out.println("Stopped postgresql")
}
}

Expand Down

0 comments on commit 7001290

Please sign in to comment.