Changes

Jump to navigation Jump to search
240 bytes removed ,  18:39, 1 December 2023
m
Text replacement - "syntaxhighlight" to "pre"
A job can be registered as ''service'' by supplying the {{Code|service}} option to {{Function||job:eval}}:
<syntaxhighlight pre lang="'xquery"'>
(: register job as service; will be run every day at 1 am :)
job:eval('db:drop("tmp")', (), map { 'id':'cleanup', 'start':'01:00:00', 'interval':'P1D', 'service': true() }),
(: unregister job :)
job:remove('cleanup', map { 'service': true() })
</syntaxhighlightpre>
'''Some more notes:'''
|
* Cache query result. The returned ID can be used to pick up the result with {{Function||job:result}}:
<syntaxhighlight pre lang="'xquery"'>
job:eval("1+3", (), map { 'cache': true() })
</syntaxhighlightpre>
* A happy birthday mail will be sent at the given date:
<syntaxhighlight pre lang="'xquery"'>
job:eval("import module namespace mail='mail'; mail:send('Happy birthday!')",
(), map { 'start': '2018-09-01T06:00:00' })}}
</syntaxhighlightpre>
* The following [[RESTXQ]] functions can be called to execute a query at 2 am every day. An ID will be returned by the first function, which can be used to stop the scheduler via the second function:
<syntaxhighlight pre lang="'xquery"'>
declare %rest:POST("{$query}") %rest:path('/start-scheduling') function local:start($query) {
job:eval($query, (), map { 'start': '02:00:00', 'interval': 'P1D' })
job:remove($id)
};
</syntaxhighlightpre>
* Query execution is scheduled for every second, and for 10 seconds in total. As the query itself will take 1.5 seconds, it will only be executed every second time:
<syntaxhighlight pre lang="'xquery"'>
job:eval("prof:sleep(1500)", (), map { 'interval': 'PT1S', 'end': 'PT10S' })
</syntaxhighlightpre>
* The query in the specified file will be evaluated once:
<syntaxhighlight pre lang="'xquery"'>
job:eval(xs:anyURI('cleanup.xq'))
</syntaxhighlightpre>
* The following expression, if stored in a file, will be evaluated every 5 seconds:
<syntaxhighlight pre lang="'xquery"'>
job:eval(
static-base-uri(),
map { 'start': 'PT5S' }
)
</syntaxhighlightpre>
|}
|
* The following [[RESTXQ]] function will either return the result of a previously started job or raise an error:
<syntaxhighlight pre lang="'xquery"'>
declare %rest:path('/result/{$id}') function local:result($id) {
job:result($id)
};
</syntaxhighlightpre>
* The following query demonstrates how the results of an executed query can be returned within the same query (see below why you should avoid this pattern in practice):
<syntaxhighlight pre lang="'xquery"'>
let $query := job:eval('(1 to 10000000)[. = 1]', map { }, map { 'cache': true() })
return (
job:result($query)
)
</syntaxhighlightpre>
Queries of this kind can cause deadlocks! If the original query and the new query perform updates on the same database, the second query will only be run after the first one has been executed, and the first query will wait for the second query forever. You should resort to {{Function|XQuery|xquery:fork-join}} if you want to have full control on parallel query execution.
|}
| '''Examples'''
| <code>job:list-details()</code> returns information on the currently running job and possibly others:
<syntaxhighlight pre lang="xml">
<job id="job1" type="XQuery" state="running" user="admin" duration="PT0.001S">
XQUERY job:list-details()
</job>
</syntaxhighlightpre>
|}
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu