Changes

Jump to navigation Jump to search
170 bytes added ,  16:33, 27 February 2020
no edit summary
A job can be registered as ''service'' by supplying the {{Code|service}} option to {{Function|Jobs|jobs:eval}}:
<pre classsyntaxhighlight lang="brush:xquery">
(: register job as service; will be run every day at 1 am :)
jobs:eval('db:drop("tmp")', (), map { 'id':'cleanup', 'start':'01:00:00', 'interval':'P1D', 'service': true() }),
(: unregister job :)
jobs:stop('cleanup', map { 'service': true() })
</presyntaxhighlight>
'''Some more notes:'''
| '''Examples'''
| <code>jobs:list-details()</code> returns information on the currently running job and possibly others:
<pre classsyntaxhighlight lang="brush:xml">
<job id="job1" type="XQuery" state="running" user="admin" duration="PT0.001S">
XQUERY jobs:list-details()
</job>
</presyntaxhighlight>
|}
|
* Cache query result. The returned id can be used to pick up the result with [[#jobs:result|jobs:result]]:
<pre classsyntaxhighlight lang='brush:"xquery'">
jobs:eval("1+3", (), map { 'cache': true() })
</presyntaxhighlight>
* A happy birthday mail will be sent at the given date:
<pre classsyntaxhighlight lang="brush:xquery">
jobs:eval("import module namespace mail='mail'; mail:send('Happy birthday!')",
(), map { 'start': '2018-09-01T06:00:00' })}}
</presyntaxhighlight>
* 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:
<pre classsyntaxhighlight lang='brush:"xquery'">
declare %rest:POST("{$query}") %rest:path('/start-scheduling') function local:start($query) {
jobs:eval($query, (), map { 'start': '02:00:00', 'interval': 'P1D' })
jobs:stop($id)
};
</presyntaxhighlight>
* 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:
<pre classsyntaxhighlight lang="brush:xquery">
jobs:eval("prof:sleep(1500)", (), map { 'interval': 'PT1S', 'end': 'PT10S' })
</presyntaxhighlight>
* The query in the specified file will be evaluated once:
<pre classsyntaxhighlight lang='brush:"xquery'">
jobs:eval(xs:anyURI('cleanup.xq'))
</presyntaxhighlight>
* The following expression, if stored in a file, will be evaluated every 5 seconds:
<pre classsyntaxhighlight lang="brush:xquery">
jobs:eval(
static-base-uri(),
map { 'start': 'PT5S' }
)
</presyntaxhighlight>
|}
|
* The following [[RESTXQ]] function will either return the result of a previously started job or raise an error:
<pre classsyntaxhighlight lang='brush:"xquery'">
declare %rest:path('/result/{$id}') function local:result($id) {
jobs:result($id)
};
</presyntaxhighlight>
* 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):
<pre classsyntaxhighlight lang='brush:"xquery'">
let $query := jobs:eval('(1 to 10000000)[. = 1]', map { }, map { 'cache': true() })
return (
jobs:result($query)
)
</presyntaxhighlight>
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 [[XQuery Module#xquery:fork-join|xquery:fork-join]] if you want to have full control on parallel query execution.
|}
Bureaucrats, editor, reviewer, Administrators
12,227

edits

Navigation menu