ML 8中如何通过代码检测任务服务器任务完成状态?
Absolutely, you can fully automate your batch workflow in MarkLogic 8—no more manual checking of the task server queue! Here's a step-by-step solution to detect when tasks finish and chain your dependent batches programmatically:
1. First, Create a Helper Function to Check Queue Status
You can query the task server's status to get the current queue size using MarkLogic's built-in admin functions. This function will return true when the queue is empty:
import module namespace task = "http://marklogic.com/xdmp/task" at "/MarkLogic/task.xqy"; declare function local:is-task-queue-empty() as xs:boolean { (: Get the first task server on your host (adjust if you have multiple) :) let $task-server := xdmp:servers()[xdmp:server-kind(.) = "taskserver"][1] let $server-status := xdmp:server-status(xdmp:server-id($task-server)) (: Extract the current queue size from the status :) let $queue-size := $server-status/server-status/queue-size return $queue-size = 0 };
2. Add a Wait Function to Poll Until Queue is Empty
To avoid spamming the server, we'll add a delay between checks. This function will loop until the queue clears, with optional logging to track progress:
declare function local:wait-for-empty-queue($wait-seconds as xs:integer := 10) { while (not(local:is-task-queue-empty())) { (: Wait for the specified interval :) xdmp:sleep(xs:dayTimeDuration("PT" || $wait-seconds || "S")) (: Log current queue size for visibility :) let $current-size := xdmp:server-status(xdmp:server-id(xdmp:servers()[xdmp:server-kind(.) = "taskserver"][1]))/server-status/queue-size xdmp:log("Waiting for task queue to empty... Current queue size: " || $current-size) } xdmp:log("✅ Task queue is empty! Proceeding to next batch.") };
3. Chain Your Batch Tasks
Now you can structure your workflow to submit a batch, wait for it to finish, then submit the next dependent batch. Here's a complete example:
(: Define your task functions first :) declare function local:generate-xml-file($output-dir as xs:string, $task-id as xs:integer) { (: Generate your XML content here :) let $xml-content := <document><id>{$task-id}</id><content>Generated from first batch</content></document> (: Save to D drive (note Windows path escaping with double backslashes) :) let $file-path := $output-dir || "batch1_doc_" || $task-id || ".xml" return xdmp:save($file-path, $xml-content) }; declare function local:generate-dependent-xml-file($output-dir as xs:string, $task-id as xs:integer) { (: Access files from the first batch here (adjust logic as needed) :) let $source-file := $output-dir || "batch1_doc_" || ($task-id mod 10000) || ".xml" let $source-content := if (fn:doc-available($source-file)) then fn:doc($source-file) else <default/> (: Generate dependent XML content :) let $xml-content := <dependent-document><id>{$task-id}</id><source>{$source-content}</source></dependent-document> let $file-path := $output-dir || "batch2_doc_" || $task-id || ".xml" return xdmp:save($file-path, $xml-content) }; (: --- Start the workflow --- :) (: Submit first batch of 10,000 tasks :) xdmp:log("🚀 Submitting first batch of 10,000 tasks...") for $i in 1 to 10000 return task:add-task( xdmp:function(xs:QName("local:generate-xml-file")), ("D:\\output\\", $i), <options xmlns="http://marklogic.com/xdmp/task"> <priority>normal</priority> </options> ); (: Wait for first batch to complete :) local:wait-for-empty-queue(10); (: Submit second batch of 20,000 tasks :) xdmp:log("🚀 Submitting second batch of 20,000 tasks...") for $i in 1 to 20000 return task:add-task( xdmp:function(xs:QName("local:generate-dependent-xml-file")), ("D:\\output\\", $i), <options xmlns="http://marklogic.com/xdmp/task"> <priority>normal</priority> </options> ); (: Wait again before next batch :) local:wait-for-empty-queue(10); (: Add subsequent batches here following the same pattern :)
Key Notes & Considerations
- Permissions: Make sure the user running this code has the
task-adminrole (or at least permissions to read server status and add tasks). - Task Server Selection: If you have multiple task servers, adjust the
$task-serverselection logic to target the correct one. - Wait Interval: Tweak the
$wait-secondsparameter based on how fast your tasks run—shorter intervals for fast tasks, longer ones for slower jobs. - Error Handling: You might want to add error handling (e.g., checking if files exist before processing) to make the workflow more robust.
- Logging: Check MarkLogic's server logs (usually in
C:\Program Files\MarkLogic\Data\Logson Windows) to track the workflow's progress.
内容的提问来源于stack exchange,提问作者Dixit Singla

