Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 7024b3a

Browse files
committed
#117 Now have a command for waiting for tasks to finish
1 parent 953d8dd commit 7024b3a

File tree

6 files changed

+97
-8
lines changed

6 files changed

+97
-8
lines changed

build.gradle

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ repositories {
1515
}
1616

1717
dependencies {
18-
compile 'com.marklogic:ml-javaclient-util:2.9.0'
18+
compile 'com.marklogic:ml-javaclient-util:2.10.0'
1919
compile 'jaxen:jaxen:1.1.6'
2020
compile 'org.apache.httpcomponents:httpclient:4.3.5'
2121
compile 'org.springframework:spring-web:4.1.5.RELEASE'
@@ -64,9 +64,3 @@ if (project.hasProperty("myBintrayUser")) {
6464
}
6565
}
6666
}
67-
68-
69-
70-
task wrapper(type: Wrapper) {
71-
gradleVersion = '2.12'
72-
}

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-3.1-bin.zip
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.marklogic.appdeployer.command.tasks;
2+
3+
import com.marklogic.appdeployer.command.AbstractCommand;
4+
import com.marklogic.appdeployer.command.CommandContext;
5+
import com.marklogic.mgmt.tasks.TaskManager;
6+
7+
public class WaitForTaskServerCommand extends AbstractCommand {
8+
9+
private String groupName;
10+
private int retryInSeconds;
11+
12+
@Override
13+
public void execute(CommandContext context) {
14+
TaskManager mgr = new TaskManager(context.getManageClient());
15+
String group = groupName != null ? groupName : context.getAppConfig().getGroupName();
16+
int retry = retryInSeconds > 0 ? retryInSeconds : 1;
17+
mgr.waitForTasksToComplete(group, retry);
18+
}
19+
20+
public String getGroupName() {
21+
return groupName;
22+
}
23+
24+
public void setGroupName(String groupName) {
25+
this.groupName = groupName;
26+
}
27+
28+
public int getRetryInSeconds() {
29+
return retryInSeconds;
30+
}
31+
32+
public void setRetryInSeconds(int retryInSeconds) {
33+
this.retryInSeconds = retryInSeconds;
34+
}
35+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.marklogic.mgmt.requests;
2+
3+
import com.marklogic.mgmt.AbstractManager;
4+
import com.marklogic.mgmt.AbstractResourceManager;
5+
import com.marklogic.mgmt.ManageClient;
6+
import com.marklogic.rest.util.Fragment;
7+
import org.jdom2.Element;
8+
9+
import java.util.List;
10+
11+
public class RequestManager extends AbstractManager {
12+
13+
private ManageClient client;
14+
15+
public RequestManager(ManageClient client) {
16+
this.client = client;
17+
}
18+
19+
public Fragment getRequests() {
20+
return client.getXml("/manage/v2/requests");
21+
}
22+
23+
public int getRequestCountForRelationId(String id) {
24+
return getRequestsForRelationId(id).size();
25+
}
26+
27+
public List<Element> getRequestsForRelationId(String id) {
28+
return getRequests().getElements(format("//req:list-items/req:list-item[req:relation-id = '%s']", id));
29+
}
30+
}

src/main/java/com/marklogic/mgmt/tasks/TaskManager.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.marklogic.mgmt.AbstractResourceManager;
77
import com.marklogic.mgmt.ManageClient;
8+
import com.marklogic.mgmt.requests.RequestManager;
89
import com.marklogic.rest.util.Fragment;
910

1011
/**
@@ -107,6 +108,33 @@ public void deleteAllScheduledTasks() {
107108
}
108109
}
109110

111+
public void waitForTasksToComplete(String group, int retryInSeconds) {
112+
Fragment servers = getManageClient().getXml("/manage/v2/task-servers");
113+
String taskServerId = servers.getElementValue(format("//ts:list-item[ts:groupnameref = '%s']/ts:idref", group));
114+
if (taskServerId == null) {
115+
logger.warn(format("Could not find task server ID for group %s, so not waiting for tasks to complete", group));
116+
return;
117+
}
118+
RequestManager mgr = new RequestManager(getManageClient());
119+
if (logger.isInfoEnabled()) {
120+
logger.info("Waiting for tasks to complete on task server");
121+
}
122+
int count = mgr.getRequestCountForRelationId(taskServerId);
123+
while (count > 0) {
124+
if (logger.isDebugEnabled()) {
125+
logger.debug("Waiting for tasks to complete on task server, count: " + count);
126+
}
127+
try {
128+
Thread.sleep(retryInSeconds * 1000);
129+
} catch (InterruptedException e) {
130+
}
131+
count = mgr.getRequestCountForRelationId(taskServerId);
132+
}
133+
if (logger.isInfoEnabled()) {
134+
logger.info("Finished waiting for tasks to complete on task server");
135+
}
136+
}
137+
110138
public void setGroupName(String groupName) {
111139
this.groupName = groupName;
112140
}

src/main/java/com/marklogic/rest/util/Fragment.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public Fragment(String xml, Namespace... namespaces) {
3737
list.add(Namespace.getNamespace("msec", "http://marklogic.com/manage/security"));
3838
list.add(Namespace.getNamespace("sec", "http://marklogic.com/xdmp/security"));
3939
list.add(Namespace.getNamespace("arp", "http://marklogic.com/manage/alert-rule/properties"));
40+
list.add(Namespace.getNamespace("ts", "http://marklogic.com/manage/task-server"));
41+
list.add(Namespace.getNamespace("req", "http://marklogic.com/manage/requests"));
4042
for (Namespace n : namespaces) {
4143
list.add(n);
4244
}

0 commit comments

Comments
 (0)