0
I'm trying to get a simple log/email alert to fire in Marklogic and am following the examples in the documentation. However I cannot seem to execute an action.

My steps:   
[1] create config and insert.   
[2] create action and insert.   
[3] create rule and insert.

My alert action is as simple as xdmp:log("some message", "alert"). I created a log.xqy and loaded it into the Modules dB. When I invoke alert:invoke-matching-actions("config uri", fn:doc("/mydocs/doc.xml"), ). I expect the alert action to write to my log but it doesn't and I can't seem to meaningfully debug it.

(:_________**set up config**___________________:)    
xquery version "1.0-ml";      
import module namespace alert = "http://marklogic.com/xdmp/alert" 
      at "/MarkLogic/alert.xqy";      
let $config := alert:make-config(  
      "test-config-uri",
      "test-config-name",
      "Alerting config for test",
        `<alert:options/>`   
)      
return alert:config-insert($config);



(:_______**set up action**___________________:)      
xquery version "1.0-ml";      
import module namespace alert = "http://marklogic.com/xdmp/alert" 
      at "/MarkLogic/alert.xqy";      
let $action := alert:make-action(  
    "test-action-xdmp:log", 
    "log to ErrorLog.txt",
    xdmp:modules-database(),
    xdmp:modules-root(), 
    "/modules/alert/log.xqy",
`<alert:options>`content modified`</alert:options>`           
)      
return  alert:action-insert("test-config-uri", $action);



(:_____**create rule**____________________:)      
xquery version "1.0-ml";      
import module namespace alert = "http://marklogic.com/xdmp/alert" 
      at "/MarkLogic/alert.xqy";      
let $rule := alert:make-rule(  
    "test-rule-name", 
    "test-rule-name-desc",
    0, 
    cts:word-query("Radiohead"),
    "test-action-xdmp:log",
    `<alert:options/>`   
)      
return  alert:rule-insert("test-config-uri", $rule);




(:_______**run rule against content**____________________:)      
xquery version "1.0-ml";      
import module namespace alert = "http://marklogic.com/xdmp/alert" 
  at "/MarkLogic/alert.xqy";      
alert:invoke-matching-actions("test-config-uri", 
      <doc>Radiohead</doc>, <options/>);




(:_______**log.xqy**_______________________________:)      
xquery version "1.0-ml";      
let $msg := "Content was modified. New update alert. "      
let $level := "alert"      
return xdmp:log($msg, $level);
Dee
  • 41
  • 3
  • for all of the steps you list above, please include the code and configuration samples. Please also include the document database name as well as the configured triggers database and the module database you are using for the triggers. There are quite a few moving pieces here - being clear upfront about all of the above will help others help you more easily. – David Ennis -CleverLlamas.com Dec 28 '17 at 00:01
  • content dB: Music (created with default specs) and using default Modules and Triggers dB's. – Dee Dec 28 '17 at 00:32
  • Please format it all into your post. It is not easy to follow in the comments. – David Ennis -CleverLlamas.com Dec 28 '17 at 01:13

1 Answers1

0

I ran the following from QConsole, which worked fine for me. Make sure log.xqy is created properly at your end. It must be a text file in the modules database associated with the app server with which you run the alert:invoke-matching-actions function..

(:_________**set up config**___________________:)    
xquery version "1.0-ml";
import module namespace alert = "http://marklogic.com/xdmp/alert" 
      at "/MarkLogic/alert.xqy";      
if (empty(alert:config-get("test-config-uri"))) then
  let $config := alert:make-config(  
      "test-config-uri",
      "test-config-name",
      "Alerting config for test",
      <alert:options/>
  )     
  return alert:config-insert($config)
else ()

;

(:_______**set up action**___________________:)      
xquery version "1.0-ml";      
import module namespace alert = "http://marklogic.com/xdmp/alert" 
      at "/MarkLogic/alert.xqy";
if (empty(alert:get-actions("test-config-uri", "test-action-xdmp:log"))) then
  let $action := alert:make-action(  
    "test-action-xdmp:log", 
    "log to ErrorLog.txt",
    xdmp:modules-database(),
    xdmp:modules-root(), 
    "/modules/alert/log.xqy",
    <alert:options>content modified</alert:options>           
  )      
  return  alert:action-insert("test-config-uri", $action)
else ()

;

(:_____**create rule**____________________:)      
xquery version "1.0-ml";      
import module namespace alert = "http://marklogic.com/xdmp/alert" 
      at "/MarkLogic/alert.xqy";
if (empty(alert:get-all-rules("test-config-uri", cts:word-query("test-rule-name")))) then
  let $rule := alert:make-rule(  
    "test-rule-name", 
    "test-rule-name-desc",
    0, 
    cts:word-query("Radiohead"),
    "test-action-xdmp:log",
    <alert:options/> 
  )      
  return  alert:rule-insert("test-config-uri", $rule)
else ()

;

(:_______**log.xqy**_______________________________:)
let $doc := text {'
  xquery version "1.0-ml";      
  let $msg := "Content was modified. New update alert. "      
  let $level := "alert"      
  return xdmp:log($msg, $level);
'}
return
xdmp:eval(
  '
    xquery version "1.0-ml";
    declare variable $uri external;
    declare variable $doc external;
    xdmp:document-insert($uri, $doc, xdmp:default-permissions())
  ',
  map:new((
    map:entry("uri", "/modules/alert/log.xqy"),
    map:entry("doc", $doc)
  )),
  map:entry("database", xdmp:modules-database())
)

;

(:_______**run rule against content**____________________:)      
xquery version "1.0-ml";      
import module namespace alert = "http://marklogic.com/xdmp/alert" 
  at "/MarkLogic/alert.xqy";      
alert:invoke-matching-actions("test-config-uri", 
      <doc>Radiohead</doc>, <options/>)

HTH!

grtjn
  • 20,254
  • 1
  • 24
  • 35
  • Thank you. The key is to make sure the log.xqy is in the Modules dB ! – Dee Jan 11 '18 at 18:52
  • If you use `xdmp:modules-database()` when defining the action, then yes. :) – grtjn Jan 11 '18 at 19:01
  • Correct :). Took a while to get it all hooked up but i have the built-in Alert Pipeline processing the action. I wish Marklogic had a module with alert options for publishing to Kafka. I have a question out there for it as well. Ideally the 'Alert' notification action will publish to a Kafka topic similar to the sms/email.xqy modules that come with it. – Dee Jan 11 '18 at 19:20
  • Actually that's exactly what i did ! Forgot to post a reply here. – Dee Jan 17 '18 at 00:35
  • xquery version "1.0-ml"; let $content := {cts:word-query("Kaboom")} let $model-node := Kaboom let $rquery := cts:reverse-query($model-node) return xdmp:http-post("http://localhost:8082/topics/topicName", {'{"records":[{"value": {"alert" : "Content found" } }]}'} application/vnd.kafka.json.v1+json ) – Dee Jan 17 '18 at 00:39