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

Commit d835dda

Browse files
committed
#367 A scheduled task is now deleted and recreated instead of being updated
This works around Manage API behavior where if any property besides task-enabled is modified, the Manage API throws an error.
1 parent b1a7a8a commit d835dda

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

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

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package com.marklogic.mgmt.resource.tasks;
22

33
import com.marklogic.mgmt.ManageClient;
4+
import com.marklogic.mgmt.PayloadParser;
45
import com.marklogic.mgmt.SaveReceipt;
6+
import com.marklogic.mgmt.api.API;
7+
import com.marklogic.mgmt.api.task.Task;
58
import com.marklogic.mgmt.resource.AbstractResourceManager;
69
import com.marklogic.mgmt.resource.requests.RequestManager;
710
import com.marklogic.rest.util.Fragment;
811

12+
import java.net.URI;
913
import java.util.ArrayList;
1014
import java.util.List;
1115

@@ -137,7 +141,7 @@ protected SaveReceipt createNewResource(String payload, String resourceId) {
137141
final String taskPath = payloadParser.getPayloadFieldValue(payload, "task-path", false);
138142

139143
SaveReceipt receipt = super.createNewResource(payload, taskPath);
140-
updateNewTaskIfItShouldBeDisabled(payload, taskPath);
144+
updateNewTaskIfItShouldBeDisabled(payload, receipt);
141145
return receipt;
142146
}
143147

@@ -146,12 +150,50 @@ protected SaveReceipt createNewResource(String payload, String resourceId) {
146150
* task isn't actually disabled. So an update call is made to the task right after it's created.
147151
*
148152
* @param payload
149-
* @param taskPath
150-
* @return
153+
* @param receipt
151154
*/
152-
protected SaveReceipt updateNewTaskIfItShouldBeDisabled(String payload, String taskPath) {
155+
protected void updateNewTaskIfItShouldBeDisabled(String payload, SaveReceipt receipt) {
153156
String enabled = payloadParser.getPayloadFieldValue(payload, "task-enabled", false);
154-
return "false".equals(enabled) ? super.updateResource(payload, taskPath) : null;
157+
if ("false".equalsIgnoreCase(enabled)) {
158+
// We don't reuse updateResource here since that first deletes the task
159+
URI uri = receipt.getResponse().getHeaders().getLocation();
160+
// Expecting a path of "/manage/(version)/tasks/(taskId)"
161+
String[] tokens = uri.getPath().split("/");
162+
final String taskId = tokens[tokens.length - 1];
163+
164+
Task task = new Task(new API(getManageClient()), taskId);
165+
task.setTaskEnabled(false);
166+
167+
String path = getPropertiesPath(taskId);
168+
path = appendParamsAndValuesToPath(path, getUpdateResourceParams(payload));
169+
logger.info("Updating new scheduled task so it is disabled; task ID: " + taskId);
170+
putPayload(getManageClient(), path, task.getJson());
171+
}
172+
}
173+
174+
/**
175+
* Per ticket #367, when a task is updated, it is first deleted. This is to workaround Manage API behavior which
176+
* does not allow for any property besides "task-enabled" to be updated on a scheduled task. But it's often handy
177+
* during a deployment to update some other property of a scheduled task. Unfortunately, that throws an error.
178+
*
179+
* So to work around that, when a scheduled task is updated, it's first deleted. Then the task is created, which
180+
* includes making another call to disable the task if task-enabled is set to false.
181+
*
182+
* @param payload
183+
* @param resourceId
184+
* @return
185+
*/
186+
@Override
187+
public SaveReceipt updateResource(String payload, String resourceId) {
188+
logger.info("Deleting scheduled task first since updates are not allowed except for task-enabled; task ID: " + resourceId);
189+
deleteByIdField(resourceId);
190+
191+
PayloadParser parser = new PayloadParser();
192+
payload = parser.excludeProperties(payload, "task-id");
193+
String taskPath = parser.getPayloadFieldValue(payload, "task-path");
194+
SaveReceipt receipt = super.createNewResource(payload, taskPath);
195+
updateNewTaskIfItShouldBeDisabled(payload, receipt);
196+
return receipt;
155197
}
156198

157199
public List<String> getTaskPaths() {

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
public class DeployDisabledTaskTest extends AbstractAppDeployerTest {
1313

1414
private final static String TASK_PATH = "/this/should/be/disabled.xqy";
15-
15+
1616
@After
1717
public void teardown() {
1818
undeploySampleApp();
@@ -33,5 +33,14 @@ public void test() {
3333
assertFalse("Due to a bug in the Manage API, when a task is first created, task-enabled is ignored " +
3434
"and the task is always enabled. To work around this, if the payload has task-enabled set to false, " +
3535
"then a second call should be made to ensure it gets set to false", task.getTaskEnabled());
36+
37+
// For ticket #367, make sure that the scheduled task can be updated without throwing an error.
38+
// This should result in a delete call followed by the task being created again
39+
task.setTaskModules("Modules");
40+
mgr.save(task.getJson());
41+
42+
task = new API(manageClient).task(mgr.getTaskIdForTaskPath(TASK_PATH));
43+
assertEquals("Modules", task.getTaskModules());
44+
assertFalse("The task should still be disabled", task.getTaskEnabled());
3645
}
3746
}

0 commit comments

Comments
 (0)