|
11 | 11 | /** |
12 | 12 | * A scheduled task doesn't have a name, and the ID is generated by ML, so this class assumes that task-path will be |
13 | 13 | * 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. |
18 | 17 | */ |
19 | 18 | public class TaskManager extends AbstractResourceManager { |
20 | 19 |
|
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 | + } |
110 | 117 |
|
111 | 118 | public void waitForTasksToComplete(String group, int retryInSeconds) { |
112 | 119 | Fragment servers = getManageClient().getXml("/manage/v2/task-servers"); |
@@ -135,11 +142,11 @@ public void waitForTasksToComplete(String group, int retryInSeconds) { |
135 | 142 | } |
136 | 143 | } |
137 | 144 |
|
138 | | - public void setGroupName(String groupName) { |
139 | | - this.groupName = groupName; |
140 | | - } |
| 145 | + public void setGroupName(String groupName) { |
| 146 | + this.groupName = groupName; |
| 147 | + } |
141 | 148 |
|
142 | | - public String getGroupName() { |
143 | | - return groupName; |
144 | | - } |
| 149 | + public String getGroupName() { |
| 150 | + return groupName; |
| 151 | + } |
145 | 152 | } |
0 commit comments