0
votes

I attempted to follow a tutorial on Marklogic for creating our own CPF. I pretty much copy and pasted all the codes and most of the functionalities worked aside from xdmp:log. The CPF would end it's task upon reaching it. The documentation from marklogic can be found here Getting Started with a Simple CPF Application

In case I did something wrong I will post my codes here and what I did.

=======Documents Used=======

add-last-updated.xqy => added into Modules DB

xquery version "1.0-ml";
import module namespace cpf="http://marklogic.com/cpf" 
  at "/MarkLogic/cpf/cpf.xqy";
declare variable $cpf:document-uri as xs:string external;
declare variable $cpf:transition as node() external;
if (cpf:check-transition($cpf:document-uri,$cpf:transition)) then try {
  let $doc := fn:doc($cpf:document-uri)
  return
      xdmp:node-insert-child(
        $doc/book,
        <last-updated>{fn:current-dateTime()}</last-updated>
      ),
  xdmp:log( "add last-updated ran OK" ),
  cpf:success($cpf:document-uri, $cpf:transition, ())
} catch ($e) {
  cpf:failure($cpf:document-uri, $cpf:transition, $e, ())
}
else ()

add-copyright.xqy => added into Modules DB

xquery version "1.0-ml";
import module namespace cpf = "http://marklogic.com/cpf" 
  at "/MarkLogic/cpf/cpf.xqy";
declare variable $cpf:document-uri as xs:string external;
declare variable $cpf:transition as node() external;
if (cpf:check-transition($cpf:document-uri,$cpf:transition)) then try {
  let $doc := fn:doc( $cpf:document-uri )
  return
      xdmp:node-insert-child( 
        $doc/book,
        <copyright>
          <year>2010</year>
          <holder>The Publisher</holder>
        </copyright>),
      xdmp:log( "add copyright ran OK" ),
      cpf:success( $cpf:document-uri, $cpf:transition, () )
}
catch ($e) {
  cpf:failure( $cpf:document-uri, $cpf:transition, $e, () )
}
else ()

copyright.xml => added into Pipeline and attached to "Default Documents" Domain

<pipeline xmlns="http://marklogic.com/cpf/pipelines">
  <pipeline-name>Copyright Pipeline</pipeline-name>
  <pipeline-description>Pipeline to test CPF</pipeline-description>
  <success-action>
    <module>/MarkLogic/cpf/actions/success-action.xqy</module>
  </success-action>
  <failure-action>
    <module>/MarkLogic/cpf/actions/failure-action.xqy</module>
  </failure-action>
  <state-transition>
    <annotation>
      When a document containing ‘book' as a root element is created, 
      add a ‘copyright' statement.
    </annotation>
    <state>http://marklogic.com/states/initial</state>
    <on-success>http://marklogic.com/states/done</on-success>
    <on-failure>http://marklogic.com/states/error</on-failure>
    <execute>
      <condition>
        <module>/MarkLogic/cpf/actions/namespace-condition.xqy</module>
        <options xmlns="/MarkLogic/cpf/actions/namespace-condition.xqy">
          <root-element>book</root-element>
          <namespace/>
        </options>
      </condition>
      <action>
        <module>add-copyright.xqy</module>
      </action>
    </execute>
  </state-transition>
  <state-transition>
    <annotation>
      When a document containing ‘book' as a root element is updated, 
      add a ‘last-updated' element
    </annotation>
    <state>http://marklogic.com/states/updated</state>
    <on-success>http://marklogic.com/states/done</on-success>
    <on-failure>http://marklogic.com/states/error</on-failure>
    <execute>
      <condition>
        <module>/MarkLogic/cpf/actions/namespace-condition.xqy</module>
        <options xmlns="/MarkLogic/cpf/actions/namespace-condition.xqy">
          <root-element>book</root-element>
          <namespace/>
        </options>
      </condition>
      <action>
        <module>add-last-updated.xqy</module>
      </action>
    </execute>
  </state-transition>
</pipeline>

======Tested Using======

xquery version "1.0-ml";
let $contents :=   
  <book>
    <bookTitle>All About George</bookTitle>
    <chapter1>
      <chapterTitle>Curious George</chapterTitle>
      <para>
        George Washington crossed the Delaware to see what was on the other side.
      </para>
    </chapter1>
  </book>
return
  xdmp:document-insert("/content.xml", $contents)

In general the code works and the Document is edited outputting upon the first insertion as shown below, but the logs found in MarkLogic/Data/Logs/8000_ErrorLog were not updated. Additionally, when I attempted to bring xdmp:log above xdmp:node-insert-child the CPF stops prematurely and the node does not get edited. I would greatly appreciate any advise to help with this issue.

<book>
  <bookTitle>All About George</bookTitle>
  <chapter1>
    <chapterTitle>Curious George</chapterTitle>
    <para>
      George Washington crossed the Delaware to see what was on the other side.
    </para>
  </chapter1>
  <copyright>
    <year>2010</year>
    <holder>The Publisher</holder>
  </copyright>
</book>

========Update========

I followed mholstege recommendation and removed my "return" and "let" statement as well as trailing the logs as grtjn suggested. From there I realized the logs was being pushed into "TaskServer_Error" logs. A silly issue on my part and thanks for the assistance.

1
I think you need to look in a different ErrorLog, probably the TaskServer_ErrorLog.txt. I typically do something like tail -f *_ErrorLog.txt to follow all error logs in one go.. - grtjn

1 Answers

4
votes

The way you have formatted the code suggest to me you think the xdmp:log and cpf:success lines fall under the return, but that is not the case: only the first expression after the return is. So you are probably getting an undefined variable static error on this module when you reorder the lines. You don't really need the let and return: you could just put use doc($document-uri)/book directly in the xdmp:node-replace call. Then you could reorder the sequence at will. If you want the xdmp:node-replace and the xdmp:log to be in the scope of the $doc variable, wrap parentheses around that sequence of expressions.