Unable to create MarkLogic scheduled tasks from within CPF action module

こ雲淡風輕ζ 提交于 2019-12-02 00:56:05
mholstege

The fundamental issue here is that the admin configuration manipulation APIs are not transactionally protected operations, so when you run two in parallel each one sees the initial state of the configuration files, then writes their bit to add the scheduled task, and then saves it, and only one of them wins. You can force this to behave in a transactionally protected way by forcing a lock on some URI It doesn't matter what it is. It doesn't even have to be in the database. As long as everything that is doing this is locking on the same URI you are fine. xdmp:lock-for-update("my.example.uri") will do this.

The following CPF action module is now working as expected:

xquery version "1.0-ml";
import module namespace cpf = "http://marklogic.com/cpf" at "/MarkLogic/cpf/cpf.xqy";
import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";

declare variable $cpf:document-uri as xs:string external;
declare variable $cpf:transition as node() external;

declare function local:scheduleTask()
{

xdmp:lock-for-update("/sample.xml"),
if (cpf:check-transition($cpf:document-uri,$cpf:transition)) then try
{

    let $doc := fn:doc( $cpf:document-uri )
    let $releasedon := xs:string($doc/sample/execution-date/text())

    let $config := admin:get-configuration()
    let $group := admin:group-get-id($config, "Default")

    let $new-task :=
         admin:group-one-time-scheduled-task(
            "/tasks/task.xqy",
            "/",
            xs:dateTime($releasedon),
            xdmp:database("SampleDB"),
            xdmp:database("Modules"),
            xdmp:user("admin"), 
            (),
            "normal")

    let $addTask := admin:group-add-scheduled-task($config,$group, $new-task)

    return

        admin:save-configuration($addTask),
        xdmp:log(fn:concat("Task for document Uri: ", $cpf:document-uri, " created")) 

}
catch ($e) {
    cpf:failure( $cpf:document-uri, $cpf:transition, $e, () )
}
else ( )
};

local:scheduleTask()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!