Search code examples
rubyxquerymarklogicroxy

Delete scheduled task in Marklogic


I'm trying to delete a scheduled task in Marklogic every time my function is called. It should go through the tasks, and delete the task if it matches the correct task path type and time. However I'm getting a <h1>500 Internal Server Error</h1> <dl> <dt>XDMP-UNEXPECTED: (err:XPST0003) Unexpected token syntax error, unexpected QName_</dt> <dd></dd> <dt>in /eval, at 10:20 [1.0-ml]</dt> The code should delete the task if it is there, and then add a new task on deployment, thus deleting the task if it was in the scheduled tasks, then creating a new one. Here is my code:

      def delete_cron
            r = execute_query_camel_rest(%Q{
            xquery version "1.0-ml";
            import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";
            declare namespace group = "http://marklogic.com/xdmp/group";

            let $taskPath := "/my/task/path.xqy" (: Replace with .xqy file :)

            let $tasks :=
                for admin:group-get-scheduled-tasks($config, admin:group-get-id($config, "Default"))
                where $task/group:task-path = $taskPath and $task/group:task-type = minutely and $task/group:task-period = 1
            return $task

        let $deleteTask := admin:group-delete-scheduled-task($config,  admin:group-get-id($config, "Default"), $task)
        return admin:save-configuration($deleteTask)

  },
           {
              :db_name => @properties["ml.database-name"]
           }
  )

I want that delete function to be called before my add task function like so..

def add_cron
        delete_heartbeat_cron
            r = execute_query_camel_rest(%Q{
            xquery version "1.0-ml";
            import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";
            declare namespace group = "http://marklogic.com/xdmp/group";

Solution

  • I have successfully deleted the task by specifying certain properties it has and calling the admin:group-delete-scheduled-task.

             let $deleteTask :=
             for $task in $tasks
                where $task/group:task-period = 1 and $task/group:task-type = "minutely" and $task/group:task-path = $taskPath
             return $task
    
            let $config := admin:group-delete-scheduled-task($config,  admin:group-get-id($config, "Default"), $deleteTask)
            return admin:save-configuration($config)
    

    I had to change my for loop, and added the variable $deleteTask.