{{Announce|Updated with Version 10:}} Renamed from ''Jobs Module'' to ''Job Module''. The namespace URI has been updated as well.
This [[Module Library|XQuery Module]] provides functions for registering new query jobs and orchestrating existing jobs. Jobs can be queries, commands, operations performed by a database client, and HTTP requests.
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 :)
* 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) {
* 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:
* 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>
|}
==job:result==
{{Announce|Version 10:}} options argument added.
{| width='100%'
|
* 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.
|}
==job:remove==
{{Announce|Updated with Version 10:}} Renamed from {{Code|jobs:stop}}.
{| width='100%'
| '''Examples'''
| <code>job:list-details()</code> returns information on the currently running job and possibly others: