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

Commit bfb50ca

Browse files
committed
#360 Ensuring disabled tasks are created as disabled
1 parent 16ae2ef commit bfb50ca

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

src/main/java/com/marklogic/mgmt/api/API.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,14 @@ public Role getRole() {
364364
}
365365

366366
public Task task(String taskId) {
367+
return task(taskId, Group.DEFAULT_GROUP_NAME);
368+
}
369+
370+
public Task task(String taskId, String groupId) {
367371
Task t = new Task(this, taskId);
368-
return taskId != null && t.exists() ? getResource(taskId, new TaskManager(getManageClient()), Task.class) : t;
372+
return taskId != null && t.exists() ?
373+
getResource(taskId, new TaskManager(getManageClient()), Task.class, "group-id", groupId) :
374+
t;
369375
}
370376

371377
public Task getTask() {

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,23 @@ public boolean exists(String resourceNameOrId, String... resourceUrlParams) {
135135
@Override
136136
protected SaveReceipt createNewResource(String payload, String resourceId) {
137137
final String taskPath = payloadParser.getPayloadFieldValue(payload, "task-path", false);
138-
return super.createNewResource(payload, taskPath);
138+
139+
SaveReceipt receipt = super.createNewResource(payload, taskPath);
140+
updateNewTaskIfItShouldBeDisabled(payload, taskPath);
141+
return receipt;
142+
}
143+
144+
/**
145+
* This accounts for a bug in the Manage API where when a new task is created and it has task-enabled=false, the
146+
* task isn't actually disabled. So an update call is made to the task right after it's created.
147+
*
148+
* @param payload
149+
* @param taskPath
150+
* @return
151+
*/
152+
protected SaveReceipt updateNewTaskIfItShouldBeDisabled(String payload, String taskPath) {
153+
String enabled = payloadParser.getPayloadFieldValue(payload, "task-enabled", false);
154+
return "false".equals(enabled) ? super.updateResource(payload, taskPath) : null;
139155
}
140156

141157
public List<String> getTaskPaths() {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.marklogic.appdeployer.command.tasks;
2+
3+
import com.marklogic.appdeployer.AbstractAppDeployerTest;
4+
import com.marklogic.mgmt.api.API;
5+
import com.marklogic.mgmt.api.task.Task;
6+
import com.marklogic.mgmt.resource.tasks.TaskManager;
7+
import org.junit.After;
8+
import org.junit.Test;
9+
10+
import java.io.File;
11+
12+
public class DeployDisabledTaskTest extends AbstractAppDeployerTest {
13+
14+
private final static String TASK_PATH = "/this/should/be/disabled.xqy";
15+
16+
@After
17+
public void teardown() {
18+
undeploySampleApp();
19+
assertFalse(new TaskManager(manageClient).exists(TASK_PATH));
20+
}
21+
22+
@Test
23+
public void test() {
24+
appConfig.getFirstConfigDir().setBaseDir(new File("src/test/resources/sample-app/tasks-disabled"));
25+
initializeAppDeployer(new DeployScheduledTasksCommand());
26+
27+
TaskManager mgr = new TaskManager(manageClient);
28+
assertFalse(mgr.exists(TASK_PATH));
29+
30+
deploySampleApp();
31+
32+
Task task = new API(manageClient).task(mgr.getTaskIdForTaskPath(TASK_PATH));
33+
assertFalse("Due to a bug in the Manage API, when a task is first created, task-enabled is ignored " +
34+
"and the task is always enabled. To work around this, if the payload has task-enabled set to false, " +
35+
"then a second call should be made to ensure it gets set to false", task.getTaskEnabled());
36+
}
37+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"task-enabled": false,
3+
"task-path": "/this/should/be/disabled.xqy",
4+
"task-root": "/",
5+
"task-type": "weekly",
6+
"task-period": 2,
7+
"task-day": [
8+
"tuesday"
9+
],
10+
"task-start-time": "12:00:00",
11+
"task-database": "Documents",
12+
"task-modules": "",
13+
"task-user": "nobody"
14+
}

0 commit comments

Comments
 (0)