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

Commit 778e34f

Browse files
committed
#406 Task IDs are now determined based on task-root as well
Since /manage/v2/tasks returns task-type as well, could add that to the approach for matching too. But task-root is required by this ticket, which requires checking individual properties of each task with a matching task-database and task-path.
1 parent 543d26a commit 778e34f

File tree

4 files changed

+70
-7
lines changed

4 files changed

+70
-7
lines changed

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,28 @@ protected String getResourceId(String payload) {
5353
format("/t:tasks-default-list/t:list-items/t:list-item[t:task-path = '%s' and t:task-database = '%s']/t:idref", taskPath, taskDatabase) :
5454
format("/t:tasks-default-list/t:list-items/t:list-item[t:task-path = '%s']/t:idref", taskPath);
5555

56-
return getAsXml().getElementValue(xpath);
56+
final List<String> resourceIds = getAsXml().getElementValues(xpath);
57+
if (resourceIds == null || resourceIds.isEmpty()) {
58+
return null;
59+
}
60+
61+
// Check each matching resource ID until we fine one with the same taskRoot
62+
final String taskRoot = payloadParser.getPayloadFieldValue(payload, "task-root", false);
63+
if (taskRoot == null) {
64+
throw new RuntimeException("Unable to determine ID for task, as multiple existing tasks have the same " +
65+
"task-path and task-database, but payload is missing a task-root to determine which existing task is " +
66+
"the same root; payload: " + payload);
67+
}
68+
for (String resourceId : resourceIds) {
69+
String json = getManageClient().getJson(appendGroupId(super.getResourcesPath() + "/" + resourceId + "/properties"));
70+
String thisTaskRoot = payloadParser.getPayloadFieldValue(json, "task-root", false);
71+
if (taskRoot.equals(thisTaskRoot)) {
72+
return resourceId;
73+
}
74+
}
75+
76+
// If no matching task has the same taskRoot, then this is a new task
77+
return null;
5778
}
5879

5980
@Override
@@ -101,11 +122,14 @@ public String getTaskIdForTaskPath(String taskPathOrTaskId) {
101122
Fragment f = getAsXml();
102123
String xpath = "/t:tasks-default-list/t:list-items/t:list-item[t:task-path = '%s' or t:idref = '%s']/t:idref";
103124
xpath = String.format(xpath, taskPathOrTaskId, taskPathOrTaskId);
104-
String id = f.getElementValue(xpath);
105-
if (id == null) {
125+
List<String> resourceIds = f.getElementValues(xpath);
126+
if (resourceIds == null || resourceIds.isEmpty()) {
106127
throw new RuntimeException("Could not find a scheduled task with a task-path or task-id of: " + taskPathOrTaskId);
107128
}
108-
return id;
129+
if (resourceIds.size() == 1) {
130+
return resourceIds.get(0);
131+
}
132+
throw new RuntimeException(format("Found multiple task IDs with the same task-path of %s; IDs: %s", taskPathOrTaskId, resourceIds));
109133
}
110134

111135
/**

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
import com.marklogic.appdeployer.AbstractAppDeployerTest;
44
import com.marklogic.mgmt.resource.tasks.TaskManager;
5+
import org.junit.After;
56
import org.junit.Test;
67

78
import java.io.File;
89

910
public class DeployTasksWithSamePathTest extends AbstractAppDeployerTest {
1011

12+
@After
13+
public void after() {
14+
new TaskManager(manageClient).deleteAllScheduledTasks();
15+
}
16+
1117
@Test
1218
public void test() {
1319
appConfig.getFirstConfigDir().setBaseDir(new File("src/test/resources/sample-app/tasks-with-same-path"));
@@ -20,14 +26,23 @@ public void test() {
2026

2127
try {
2228
deploySampleApp();
23-
assertEquals("There should be 2 new tasks", initialTaskCount + 2, taskManager.getAsXml().getResourceCount());
29+
assertEquals("There should be 3 new tasks; task-4.json should overwrite task 3 because it has the same " +
30+
"database, module path, and task root as task-3.json", initialTaskCount + 3, taskManager.getAsXml().getResourceCount());
31+
32+
// Verify we get an expected error when trying to get a single task by a task-path
33+
try {
34+
taskManager.getTaskIdForTaskPath("/path/to/query.xqy");
35+
fail("Expected an error because getTaskIdForTaskPath can't return a single ID when multiple tasks exist with the same path");
36+
} catch (Exception ex) {
37+
logger.info("Caught expected error because multiple tasks exist with the same path: " + ex.getMessage());
38+
}
2439

2540
deploySampleApp();
26-
assertEquals("There should still be just 2 new tasks", initialTaskCount + 2, taskManager.getAsXml().getResourceCount());
41+
assertEquals("There should still be just 3 new tasks", initialTaskCount + 3, taskManager.getAsXml().getResourceCount());
2742
} finally {
2843
undeploySampleApp();
2944

30-
assertEquals("The 2 new tasks should have been deleted", initialTaskCount, taskManager.getAsXml().getResourceCount());
45+
assertEquals("The 3 new tasks should have been deleted", initialTaskCount, taskManager.getAsXml().getResourceCount());
3146
}
3247
}
3348
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"task-enabled":true,
3+
"task-path":"/path/to/query.xqy",
4+
"task-root":"/differentPath",
5+
"task-type":"weekly",
6+
"task-period":2,
7+
"task-day":["tuesday"],
8+
"task-start-time":"12:00:00",
9+
"task-database":"Modules",
10+
"task-modules":"",
11+
"task-user":"nobody"
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"task-enabled":true,
3+
"task-path":"/path/to/query.xqy",
4+
"task-root":"/differentPath",
5+
"task-type":"weekly",
6+
"task-period":3,
7+
"task-day":["tuesday"],
8+
"task-start-time":"12:00:00",
9+
"task-database":"Modules",
10+
"task-modules":"",
11+
"task-user":"nobody"
12+
}

0 commit comments

Comments
 (0)