11package com .marklogic .mgmt .resource .tasks ;
22
33import com .marklogic .mgmt .ManageClient ;
4+ import com .marklogic .mgmt .PayloadParser ;
45import com .marklogic .mgmt .SaveReceipt ;
6+ import com .marklogic .mgmt .api .API ;
7+ import com .marklogic .mgmt .api .task .Task ;
58import com .marklogic .mgmt .resource .AbstractResourceManager ;
69import com .marklogic .mgmt .resource .requests .RequestManager ;
710import com .marklogic .rest .util .Fragment ;
811
12+ import java .net .URI ;
913import java .util .ArrayList ;
1014import 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 () {
0 commit comments