Skip to content

Commit

Permalink
#38 WatchTask now reuses the XccAssetLoader in LoadModulesCommand to …
Browse files Browse the repository at this point in the history
…cut down on duplication
  • Loading branch information
rjrudin committed Sep 7, 2015
1 parent 882118b commit 6fbb405
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 37 deletions.
2 changes: 1 addition & 1 deletion examples/disconnected-project/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mlHost=localhost
mlAppName=disconnected-example
mlRestPort=8110

mlGradleDependency=com.rjrudin:ml-gradle:2.0b5
mlGradleDependency=com.rjrudin:ml-gradle:2.0b7

# Just using admin for everything in this project, as it's a sample of running disconnected, not all of ml-gradle's features
mlUsername=admin
Expand Down
2 changes: 1 addition & 1 deletion examples/flexrep-project/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mlGradleDependency=com.rjrudin:ml-gradle:2.0b6
mlGradleDependency=com.rjrudin:ml-gradle:2.0b7

mlHost=localhost
mlAppName=flexrep-example
Expand Down
7 changes: 2 additions & 5 deletions sample-project/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
maven {url "http://rjrudin.github.io/marklogic-java/releases"}
}
dependencies {
classpath "com.rjrudin:ml-gradle:2.0b6"
classpath "com.rjrudin:ml-gradle:2.0b7"
}
}

Expand Down Expand Up @@ -53,7 +53,7 @@ apply plugin: 'ml-gradle'


/*
* The Java plugin is used to compile and run JUnit tests. It also is needed for the "mlWatch" task.
* The Java plugin is used to compile and run JUnit tests.
*/
apply plugin: 'java'

Expand Down Expand Up @@ -98,9 +98,6 @@ configurations {
* using that library in tests that must invoke MarkLogic HTTP endpoints.
*/
dependencies {
// This is needed at runtime in order to run gradle mlWatch
runtime "com.rjrudin:ml-javaclient-util:2.2.1"

// Needed to compile and run the JUnit tests
testCompile "com.rjrudin:ml-junit:2.2.2"
testCompile "com.jayway.restassured:rest-assured:2.4.1"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<configuration>

<statusListener class="ch.qos.logback.core.status.NopStatusListener" />

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
Expand All @@ -12,8 +12,8 @@
<appender-ref ref="STDOUT" />
</root>

<logger name="ch.qos.logback" level="WARN" additivity="false" />

<logger name="com.marklogic.client.io" level="WARN" additivity="false" />
<logger name="com.rjrudin.marklogic" level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
</logger>

</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.rjrudin.marklogic.gradle.task
import org.gradle.api.DefaultTask

import com.marklogic.client.DatabaseClient
import com.marklogic.client.DatabaseClientFactory
import com.rjrudin.marklogic.appdeployer.AppConfig
import com.rjrudin.marklogic.appdeployer.AppDeployer
import com.rjrudin.marklogic.appdeployer.command.Command
Expand Down Expand Up @@ -36,16 +35,15 @@ class MarkLogicTask extends DefaultTask {
AdminManager getAdminManager() {
getProject().property("mlAdminManager")
}

DatabaseClient newClient() {
AppConfig config = getAppConfig()
return DatabaseClientFactory.newClient(config.host, config.restPort, config.getRestAdminUsername(), config.getRestAdminPassword(), config.authentication)
getAppConfig().newDatabaseClient()
}

void deployWithCommandListProperty(String propertyName) {
deployWithCommands(getProject().property(propertyName))
}

void deployWithCommands(List<Command> commands) {
SimpleAppDeployer deployer = new SimpleAppDeployer(getManageClient(), getAdminManager())
deployer.setCommands(commands)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
package com.rjrudin.marklogic.gradle.task.client

import org.gradle.api.tasks.JavaExec
import org.gradle.api.tasks.TaskAction

import com.rjrudin.marklogic.appdeployer.AppConfig
import com.marklogic.client.DatabaseClient
import com.rjrudin.marklogic.appdeployer.AppDeployer
import com.rjrudin.marklogic.appdeployer.command.modules.LoadModulesCommand
import com.rjrudin.marklogic.appdeployer.impl.SimpleAppDeployer
import com.rjrudin.marklogic.gradle.task.MarkLogicTask
import com.rjrudin.marklogic.modulesloader.impl.DefaultModulesLoader

class WatchTask extends JavaExec {
/**
* Runs an infinite loop, and each second, it loads any new/modified modules. Often useful to run with the Gradle "-i" flag
* so you can see which modules are loaded.
*/
class WatchTask extends MarkLogicTask {

String modulesLoaderClassName = DefaultModulesLoader.class.getName()
long sleepTime = 1000

@TaskAction
@Override
public void exec() {
setMain("com.marklogic.clientutil.modulesloader.ModulesWatcher")
setClasspath(getProject().sourceSets.main.runtimeClasspath)
public void watchModules() {
println "Getting LoadModulesCommand from mlAppDeployer so it can be reused to load modules"
AppDeployer d = getAppDeployer()
if (d instanceof SimpleAppDeployer) {
LoadModulesCommand command = d.getCommand("LoadModulesCommand")
DefaultModulesLoader loader = new DefaultModulesLoader(command.newXccAssetLoader(getCommandContext()))
List<String> paths = getAppConfig().getModulePaths()
println "Watching modules in paths: " + paths

AppConfig config = getProject().property("mlAppConfig")
def username = config.getRestAdminUsername() ? config.getRestAdminUsername() : getProject().property("mlUsername")
def password = config.getRestAdminPassword() ? config.getRestAdminPassword() : getProject().property("mlPassword")
setArgs([
config.getModulePaths().join(","),
config.getHost(),
config.getRestPort(),
username,
password,
modulesLoaderClassName
])

super.exec()
DatabaseClient client = newClient()
while (true) {
for (String path : paths) {
loader.loadModules(new File(path), client);
}
try {
Thread.sleep(sleepTime);
} catch (InterruptedException ie) {
// Ignore
}
}
}
else {
throw new RuntimeException("Could not an instance of LoadModulesCommand in mlAppDeployer, thus not able to connect to MarkLogic to load modules")
}
}
}

0 comments on commit 6fbb405

Please sign in to comment.