Chapter 5. The Task Server

MarkLogic’s Task Server provides a way to schedule work asynchronously. By default, up to 16 tasks will run at the same time on the standard queue, along with 16 tasks on the high-priority queue. Recipes in this chapter show how to remove tasks from the queue, which normally only happens if the queue is wiped by a MarkLogic restart.

Cancel Active Tasks on the Task Server

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 or higher

xquery version "1.0-ml";

declare namespace hs="http://marklogic.com/xdmp/status/host";
declare namespace ss="http://marklogic.com/xdmp/status/server";

let $max-task-duration := xs:dayTimeDuration("PT7M")
let $min-retries := 2
(: set $user-ids to desired sequence of ids :)
let $user-ids := xdmp:get-current-userid()
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 ...

Get MarkLogic Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.