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

Commit 6daecf9

Browse files
author
Rob Rudin
committed
#137 Now processing child directories for scheduled tasks
1 parent fa72ff9 commit 6daecf9

File tree

7 files changed

+234
-120
lines changed

7 files changed

+234
-120
lines changed

src/main/java/com/marklogic/appdeployer/command/AbstractResourceCommand.java

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,22 @@ public abstract class AbstractResourceCommand extends AbstractUndoableCommand {
2323
@Override
2424
public void execute(CommandContext context) {
2525
for (File resourceDir : getResourceDirs(context)) {
26-
if (resourceDir.exists()) {
27-
ResourceManager mgr = getResourceManager(context);
26+
processExecuteOnResourceDir(context, resourceDir);
27+
}
28+
}
29+
30+
protected void processExecuteOnResourceDir(CommandContext context, File resourceDir) {
31+
if (resourceDir.exists()) {
32+
ResourceManager mgr = getResourceManager(context);
33+
if (logger.isInfoEnabled()) {
34+
logger.info("Processing files in directory: " + resourceDir.getAbsolutePath());
35+
}
36+
for (File f : listFilesInDirectory(resourceDir)) {
2837
if (logger.isInfoEnabled()) {
29-
logger.info("Processing files in directory: " + resourceDir.getAbsolutePath());
30-
}
31-
for (File f : listFilesInDirectory(resourceDir)) {
32-
if (logger.isInfoEnabled()) {
33-
logger.info("Processing file: " + f.getAbsolutePath());
34-
}
35-
SaveReceipt receipt = saveResource(mgr, context, f);
36-
afterResourceSaved(mgr, context, f, receipt);
38+
logger.info("Processing file: " + f.getAbsolutePath());
3739
}
40+
SaveReceipt receipt = saveResource(mgr, context, f);
41+
afterResourceSaved(mgr, context, f, receipt);
3842
}
3943
}
4044
}
@@ -56,18 +60,22 @@ protected void afterResourceSaved(ResourceManager mgr, CommandContext context, F
5660
public void undo(CommandContext context) {
5761
if (deleteResourcesOnUndo) {
5862
for (File resourceDir : getResourceDirs(context)) {
59-
if (resourceDir.exists()) {
60-
if (logger.isInfoEnabled()) {
61-
logger.info("Processing files in directory: " + resourceDir.getAbsolutePath());
62-
}
63-
final ResourceManager mgr = getResourceManager(context);
64-
for (File f : listFilesInDirectory(resourceDir)) {
65-
if (logger.isInfoEnabled()) {
66-
logger.info("Processing file: " + f.getAbsolutePath());
67-
}
68-
deleteResource(mgr, context, f);
69-
}
63+
processUndoOnResourceDir(context, resourceDir);
64+
}
65+
}
66+
}
67+
68+
protected void processUndoOnResourceDir(CommandContext context, File resourceDir) {
69+
if (resourceDir.exists()) {
70+
if (logger.isInfoEnabled()) {
71+
logger.info("Processing files in directory: " + resourceDir.getAbsolutePath());
72+
}
73+
final ResourceManager mgr = getResourceManager(context);
74+
for (File f : listFilesInDirectory(resourceDir)) {
75+
if (logger.isInfoEnabled()) {
76+
logger.info("Processing file: " + f.getAbsolutePath());
7077
}
78+
deleteResource(mgr, context, f);
7179
}
7280
}
7381
}

src/main/java/com/marklogic/appdeployer/command/tasks/DeployScheduledTasksCommand.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.marklogic.appdeployer.command.tasks;
22

33
import java.io.File;
4+
import java.io.FileFilter;
45

56
import com.marklogic.appdeployer.command.AbstractResourceCommand;
67
import com.marklogic.appdeployer.command.CommandContext;
@@ -31,7 +32,56 @@ protected ResourceManager getResourceManager(CommandContext context) {
3132
return mgr;
3233
}
3334

35+
/**
36+
* We do some extra work here to process each child directory, where the name of the child directory is assumed to
37+
* be a MarkLogic group name. This requires changing the state of this command momentarily, as we change the
38+
* groupName of this command for each child directory, and then restore it to the original groupName.
39+
*
40+
* @param context
41+
*/
42+
@Override
43+
public void execute(CommandContext context) {
44+
super.execute(context);
45+
46+
String originalGroupName = this.groupName;
47+
for (File resourceDir : getResourceDirs(context)) {
48+
for (File dir : resourceDir.listFiles(new IsDirectoryFilter())) {
49+
setGroupName(dir.getName());
50+
processExecuteOnResourceDir(context, dir);
51+
}
52+
}
53+
setGroupName(originalGroupName);
54+
}
55+
56+
/**
57+
* Just like on execute, we do some extra work here to delete any tasks in child directories. This requires
58+
* changing the state of this command momentarily, as we change the groupName of this command for each child
59+
* directory, and then restore it to the original groupName.
60+
*
61+
* @param context
62+
*/
63+
@Override
64+
public void undo(CommandContext context) {
65+
super.undo(context);
66+
67+
String originalGroupName = this.groupName;
68+
for (File resourceDir : getResourceDirs(context)) {
69+
for (File dir : resourceDir.listFiles(new IsDirectoryFilter())) {
70+
setGroupName(dir.getName());
71+
processUndoOnResourceDir(context, dir);
72+
}
73+
}
74+
setGroupName(originalGroupName);
75+
}
76+
3477
public void setGroupName(String groupName) {
3578
this.groupName = groupName;
3679
}
3780
}
81+
82+
class IsDirectoryFilter implements FileFilter {
83+
@Override
84+
public boolean accept(File pathname) {
85+
return pathname.isDirectory();
86+
}
87+
}

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

Lines changed: 106 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -11,102 +11,109 @@
1111
/**
1212
* A scheduled task doesn't have a name, and the ID is generated by ML, so this class assumes that task-path will be
1313
* unique and can thus be used as a way to find an existing task.
14-
* <p>
15-
* This class also assumes that the group is "Default" by default, which can be overridden. The docs at
16-
* http://docs.marklogic.com/REST/POST/manage/v2/tasks suggest that the payload can contain "group-name" to specify the
17-
* group, but as of ML 8.0-3, it doesn't work.
14+
*
15+
* Note that the "groupName" property of this class corresponds to the "group-id" querystring parameter. It's called
16+
* "groupName" because "group-id" is misleading - it's a name, not an ID.
1817
*/
1918
public class TaskManager extends AbstractResourceManager {
2019

21-
private String groupName = "Default";
22-
23-
public TaskManager(ManageClient client) {
24-
super(client);
25-
}
26-
27-
public TaskManager(ManageClient client, String groupName) {
28-
super(client);
29-
this.groupName = groupName;
30-
}
31-
32-
@Override
33-
public String getResourcePath(String resourceNameOrId, String... resourceUrlParams) {
34-
return getResourcesPath() + "/" + getTaskIdForTaskPath(resourceNameOrId);
35-
}
36-
37-
@Override
38-
protected String[] getUpdateResourceParams(String payload) {
39-
List<String> params = new ArrayList<>();
40-
params.add("group-id");
41-
params.add(groupName);
42-
return params.toArray(new String[] {});
43-
}
44-
45-
@Override
46-
protected String getCreateResourcePath(String payload) {
47-
return getResourcesPath() + "?group-id=" + groupName;
48-
}
49-
50-
@Override
51-
protected String getIdFieldName() {
52-
return "task-path";
53-
}
54-
55-
public String getTaskIdForTaskPath(String taskPath) {
56-
Fragment f = getAsXml();
57-
String xpath = "/node()/*[local-name(.) = 'list-items']/node()"
58-
+ "[*[local-name(.) = 'task-path'] = '%s']/*[local-name(.) = 'idref']";
59-
xpath = String.format(xpath, taskPath);
60-
String id = f.getElementValue(xpath);
61-
if (id == null) {
62-
throw new RuntimeException("Could not find a scheduled task with a task-path of: " + taskPath);
63-
}
64-
return id;
65-
}
66-
67-
@Override
68-
public boolean exists(String resourceNameOrId, String... resourceUrlParams) {
69-
Fragment f = getAsXml();
70-
return f.elementExists(format(
71-
"/node()/*[local-name(.) = 'list-items']/node()[*[local-name(.) = 'task-path'] = '%s']",
72-
resourceNameOrId));
73-
}
74-
75-
public void disableAllTasks() {
76-
for (String id : getAsXml().getListItemIdRefs()) {
77-
disableTask(id);
78-
}
79-
}
80-
81-
public void enableAllTasks() {
82-
for (String id : getAsXml().getListItemIdRefs()) {
83-
enableTask(id);
84-
}
85-
}
86-
87-
public void disableTask(String taskId) {
88-
String json = format("{\"task-id\":\"%s\", \"task-enabled\":false}", taskId);
89-
String path = getResourcesPath() + "/" + taskId + "/properties";
90-
path = appendParamsAndValuesToPath(path, getUpdateResourceParams(json));
91-
putPayload(getManageClient(), path, json);
92-
}
93-
94-
public void enableTask(String taskId) {
95-
String json = format("{\"task-id\":\"%s\", \"task-enabled\":true}", taskId);
96-
String path = getResourcesPath() + "/" + taskId + "/properties";
97-
path = appendParamsAndValuesToPath(path, getUpdateResourceParams(json));
98-
putPayload(getManageClient(), path, json);
99-
}
100-
101-
public void deleteAllTasks() {
102-
deleteAllScheduledTasks();
103-
}
104-
105-
public void deleteAllScheduledTasks() {
106-
for (String id : getAsXml().getListItemIdRefs()) {
107-
deleteAtPath(getResourcesPath() + "/" + id + "?group-id=" + groupName);
108-
}
109-
}
20+
private String groupName = "Default";
21+
22+
public TaskManager(ManageClient client) {
23+
super(client);
24+
}
25+
26+
public TaskManager(ManageClient client, String groupName) {
27+
super(client);
28+
this.groupName = groupName;
29+
}
30+
31+
@Override
32+
public String getResourcesPath() {
33+
return appendGroupId(super.getResourcesPath());
34+
}
35+
36+
protected String appendGroupId(String path) {
37+
if (groupName != null) {
38+
if (path.contains("?")) {
39+
return path + "&group-id=" + groupName;
40+
}
41+
return path + "?group-id=" + groupName;
42+
}
43+
return path;
44+
}
45+
46+
@Override
47+
public String getResourcePath(String resourceNameOrId, String... resourceUrlParams) {
48+
return super.getResourcesPath() + "/" + getTaskIdForTaskPath(resourceNameOrId);
49+
}
50+
51+
@Override
52+
protected String[] getUpdateResourceParams(String payload) {
53+
List<String> params = new ArrayList<>();
54+
params.add("group-id");
55+
params.add(groupName);
56+
return params.toArray(new String[]{});
57+
}
58+
59+
@Override
60+
protected String getIdFieldName() {
61+
return "task-path";
62+
}
63+
64+
public String getTaskIdForTaskPath(String taskPath) {
65+
Fragment f = getAsXml();
66+
String xpath = "/node()/*[local-name(.) = 'list-items']/node()"
67+
+ "[*[local-name(.) = 'task-path'] = '%s']/*[local-name(.) = 'idref']";
68+
xpath = String.format(xpath, taskPath);
69+
String id = f.getElementValue(xpath);
70+
if (id == null) {
71+
throw new RuntimeException("Could not find a scheduled task with a task-path of: " + taskPath);
72+
}
73+
return id;
74+
}
75+
76+
@Override
77+
public boolean exists(String resourceNameOrId, String... resourceUrlParams) {
78+
Fragment f = getAsXml();
79+
return f.elementExists(format(
80+
"/node()/*[local-name(.) = 'list-items']/node()[*[local-name(.) = 'task-path'] = '%s']",
81+
resourceNameOrId));
82+
}
83+
84+
public void disableAllTasks() {
85+
for (String id : getAsXml().getListItemIdRefs()) {
86+
disableTask(id);
87+
}
88+
}
89+
90+
public void enableAllTasks() {
91+
for (String id : getAsXml().getListItemIdRefs()) {
92+
enableTask(id);
93+
}
94+
}
95+
96+
public void disableTask(String taskId) {
97+
String json = format("{\"task-id\":\"%s\", \"task-enabled\":false}", taskId);
98+
String path = appendGroupId(super.getResourcesPath() + "/" + taskId + "/properties");
99+
putPayload(getManageClient(), path, json);
100+
}
101+
102+
public void enableTask(String taskId) {
103+
String json = format("{\"task-id\":\"%s\", \"task-enabled\":true}", taskId);
104+
String path = appendGroupId(super.getResourcesPath() + "/" + taskId + "/properties");
105+
putPayload(getManageClient(), path, json);
106+
}
107+
108+
public void deleteAllTasks() {
109+
deleteAllScheduledTasks();
110+
}
111+
112+
public void deleteAllScheduledTasks() {
113+
for (String id : getAsXml().getListItemIdRefs()) {
114+
deleteAtPath(appendGroupId(super.getResourcesPath() + "/" + id));
115+
}
116+
}
110117

111118
public void waitForTasksToComplete(String group, int retryInSeconds) {
112119
Fragment servers = getManageClient().getXml("/manage/v2/task-servers");
@@ -135,11 +142,11 @@ public void waitForTasksToComplete(String group, int retryInSeconds) {
135142
}
136143
}
137144

138-
public void setGroupName(String groupName) {
139-
this.groupName = groupName;
140-
}
145+
public void setGroupName(String groupName) {
146+
this.groupName = groupName;
147+
}
141148

142-
public String getGroupName() {
143-
return groupName;
144-
}
149+
public String getGroupName() {
150+
return groupName;
151+
}
145152
}

src/test/java/com/marklogic/appdeployer/command/tasks/ManageScheduledTasksTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.marklogic.appdeployer.command.tasks;
22

3+
import com.marklogic.appdeployer.command.groups.DeployGroupsCommand;
34
import org.junit.Test;
45

56
import com.marklogic.appdeployer.command.AbstractManageResourceTest;
@@ -8,6 +9,8 @@
89
import com.marklogic.mgmt.tasks.TaskManager;
910
import com.marklogic.rest.util.ResourcesFragment;
1011

12+
import java.io.File;
13+
1114
public class ManageScheduledTasksTest extends AbstractManageResourceTest {
1215

1316
@Override
@@ -37,4 +40,22 @@ public void deleteAll() {
3740
ResourcesFragment frag = mgr.getAsXml();
3841
assertEquals("All of the scheduled tasks should have been deleted", 0, frag.getListItemIdRefs().size());
3942
}
43+
44+
@Test
45+
public void associateChildDirectoryWithGroup() {
46+
appConfig.getConfigDir().setBaseDir(new File("src/test/resources/sample-app/tasks-in-child-dir"));
47+
initializeAppDeployer(new DeployGroupsCommand(), new DeployScheduledTasksCommand());
48+
try {
49+
appDeployer.deploy(appConfig);
50+
51+
TaskManager taskManager = new TaskManager(manageClient, "sampleAppGroup1");
52+
ResourcesFragment tasks = taskManager.getAsXml();
53+
assertEquals("Should have 1 task for our test group", 1, tasks.getResourceCount());
54+
String taskId = tasks.getListItemIdRefs().get(0);
55+
assertEquals("/path/to/other-query.xqy", tasks.getListItemValue(taskId, "task-path"));
56+
} finally {
57+
undeploySampleApp();
58+
}
59+
}
60+
4061
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"group-name": "sampleAppGroup1",
3+
"metering-enabled": false
4+
}

0 commit comments

Comments
 (0)