Problem

There are active tasks being executed on the Task Server, and you’d like to cancel some or all of them.

Solution

Applies to MarkLogic versions 7+

xquery version "1.0-ml";
  
declare namespace hs = "https://marklogic.com/xdmp/status/host";
declare namespace ss = "https://marklogic.com/xdmp/status/server";
  
let $max-task-duration := xs:dayTimeDuration("PT7M")
let $min-retries := 2
let $user-ids := xdmp:get-current-userid() (: replace with desired sequence of ids :)
let $compare-time := fn:current-dateTime() - $max-task-duration
for $host as xs:unsignedLong in xdmp:hosts()
let $task-server-id := xdmp:host-status($host)//hs:task-server-id
let $requests := xdmp:server-status($host, $task-server-id)//ss:request-status[
  not(xs:boolean(ss:canceled))
  (: and ss:user = 7071164300007443533 :)
  (: and ss:retry-count >= $min-retries :)
  and ss:request-text = "/some/module/uri.xqy"
  and ss:start-time < $compare-time
]
return (
  text{"There are currently", fn:count($requests), "matching requests on host", xdmp:host-name($host)},
  for $request in $requests
  let $request-id as xs:unsignedLong := $request/ss:request-id
  let $start as xs:dateTime := $request/ss:start-time
  return (
    text{
      $request-id, "started:", $start, "duration:", (fn:current-dateTime() - $start),
      if (fn:true()) then (
        xdmp:request-cancel($host, $request/ss:server-id, $request-id),
        "-- cancel issued"
      )
      else ()
    }
  )
)

Required Privileges:

  • https://marklogic.com/xdmp/privileges/status
  • https://marklogic.com/xdmp/privileges/cancel-any-request
  • https://marklogic.com/xdmp/privileges/cancel-my-requests

Discussion

Under certain circumstances, it may become necessary to cancel all the tasks on the task server that match a specific pattern. This may be due to human error, such as someone performing a “re-replicate all documents in domain”, or due to specific conditions occurring within a production application. Note that this recipe only cancels tasks that are actively running. Queued tasks are not affected.

The https://marklogic.com/xdmp/privileges/status privilege is required. The user running this script must also have either https://marklogic.com/xdmp/privileges/cancel-my-requests (to cancel requests running as that user) or https://marklogic.com/xdmp/privileges/cancel-any-request (to cancel any other requests).

As written, this script uses multiple filters to only show tasks that have been executing for over 7 minutes, have already been retried twice, and are associated with a specific module. Other criteria that may be useful include the user the task is running under. Also note the usage of a fixed conditional that can be toggled to perform the xdmp:request-cancel(). This allows you to test your query criteria iteratively until you are satisfied you will only cancel the desired tasks.

Below is an example of a request record returned by xdmp:server-status. Filters can be written for any of these fields.

<request-status xmlns="https://marklogic.com/xdmp/status/server">
  <request-id>796436023172923809</request-id>
  <server-id>14409436176295478539</server-id>
  <host-id>15405276691316718307</host-id>
  <transaction-id>4091631258594104359</transaction-id>
  <canceled>false</canceled>
  <modules>11527541000394886112</modules>
  <database>17515328177061313217</database>
  <root>/</root>
  <request-kind>invoke</request-kind>
  <request-text>/</request-text>
  <update>false</update>
  <start-time>2017-03-08T15:53:22.543552Z</start-time>
  <time-limit>600</time-limit>
  <max-time-limit>3600</max-time-limit>
  <user>7071164303237443533</user>
  <trigger-depth>0</trigger-depth>
  <expanded-tree-cache-hits>0</expanded-tree-cache-hits>
  <expanded-tree-cache-misses>0</expanded-tree-cache-misses>
  <request-state>running</request-state>
  <profiling-allowed>true</profiling-allowed>
  <profiling-enabled>false</profiling-enabled>
  <debugging-allowed>true</debugging-allowed>
  <debugging-status>detached</debugging-status>
  <retry-count>0</retry-count>
</request-status>

Learn More

Cancel Active and Queued Tasks on the Task Server

Learn how to cancel requests that have queued on the Task Server without clearing the entire queue or restart the host.

Administering MarkLogic Server Course

This course provides the necessary hands-on experience and knowledge to install, deploy, configure, monitor and maintain MarkLogic.

Administrator's Guide Documentation

This guide reviews over administrative tasks for MarkLogic that have the ability to run on all platforms.

This website uses cookies.

By continuing to use this website you are giving consent to cookies being used in accordance with the MarkLogic Privacy Statement.