Changes

Jump to navigation Jump to search
80 bytes removed ,  18:39, 1 December 2023
m
Text replacement - "syntaxhighlight" to "pre"
{{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 organizing scheduled, queued, running registering new query jobs and cached orchestrating existing jobs. Jobs can be queries, commands, queries, operations performed by a database client, or and HTTP requests.
=Conventions=
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:stopremove('cleanup', map { 'service': true() })</syntaxhighlightpre>
'''Some more notes:'''
=Executing Jobs=
There are cases in which a client does not, or cannot, wait until a request is fully processed. The client may be a browser, which sends an HTTP request to the server to start another time-consuming query job. The functions in this section allow you to register a new query job from a running queryjobs and access existing ones. Jobs can be executed immediately (i.e., as soon as the a [[Transaction Management#Concurrency Control|Concurrency Controlfree slot is available]] allows it) or scheduled for repeated execution. Each registered job gets a job ID, and the ID can be used to retrieve a query result, stop a job, or wait for its termination.
==job:eval==
{| width='100%'
|- valign="top"
| width='120' | '''SignaturesSignature'''|{{Func|job:eval|$query as xs:anyAtomicType|xs:string}}<br /pre>{{Func|job:eval|( $query as xs:anyAtomicType, $bindings as map(*)?|xs :string}= map { }<br />{{Func|job:eval|$query as xs:anyAtomicType, $bindings options as map(*)?, $options as := map(*{ })?|as xs:string}}<br /pre>
|- valign="top"
| '''Summary'''
|Schedules the evaluation of a new query job for the supplied {{Code|$query}} (of type {{Code|xs:string}}, or of type {{Code|xs:anyURI}}, pointing if points to a resource), and returns a query job ID. The query job will be queueduntil a free slot is available, and the query result will optionally can be cached. Queries can be updating. Variables , and variables and the context value can be declared via {{Code|$bindings}} (see {{Function|XQuery|xquery:eval}} for more details). The following {{Code|$options}} can be supplied:
* {{Code|cache}}: indicates if the query result will be cached or ignored (default: {{Code|false}}):
** The result will be cached in main-memory until it is fetched via {{Function||job:result}}, or until {{Option|CACHETIMEOUT}} is exceeded.
|
* 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' })
};
declare %rest:path('/stop-scheduling/{$id}') function local:stop($id) {
job:stopremove($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>
|}
{| width='100%'
|- valign="top"
| width='120' | '''SignaturesSignature'''|{{Func|<pre>job:result|( $id as xs:string|, $options as map(*)? := map { }) as item()*}}</pre>
|- valign="top"
| '''Summary'''
|Returns the cached result of a job with the specified job {{Code|$id}}:
* If the original job has raised an error, the cached error will be raised instead.
* Results can only be retrieved once. After retrieval, the The cached result or error will be droppedafter it has been retrieved.* If the result has already not been retrieved, cached or if it has not been cacheddropped, an empty sequence is returned.The following {{Code|$options}} can be supplied:* {{Code|keep}}: Keep the cached result or error after retrieval. 
|- valign="top"
| '''Examples'''
|
* 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:stopremove==
{| width='100%'
|- valign="top"
| width='120' | '''SignaturesSignature'''|{{Func|job:stop|$id as xs:string|empty-sequence()}}<br/pre>{{Func|job:stop|remove( $id as xs:string, $options as map(*)?| := map { }) as empty-sequence()}}</pre>
|- valign="top"
| '''Summary'''
|Triggers the cancelation of a job with the specified {{Code|$id}}, drops the cached result of a query, or cancels a scheduled jobor removes a cached result. Unknown IDs are ignored. All jobs are gracefully stopped; it is up to the process to decide when it is safe to shut down. The following {{Code|$options}} can be supplied:
* {{Code|service}}: additionally removes the job from the [[#Services|job services]] list.
|- valign="top"
| '''Examples'''
| * <code>job:list()[. != job:current()] ! job:stopremove(.)</code> stops and discards all jobs except for the current one.* <code>job:remove(job:current())</code> interrupts the current job.
|}
{| width='100%'
|- valign="top"
| width='120' | '''SignaturesSignature'''|{{Func|<pre>job:wait|( $id as xs:string|) as empty-sequence()}}</pre>
|- valign="top"
| '''Summary'''
{| width='100%'
|- valign="top"
| width='120' | '''SignaturesSignature'''|{{Func|<pre>job:current||() as xs:string}}</pre>
|- valign="top"
| '''Summary'''
{| width='100%'
|- valign="top"
| width='120' | '''SignaturesSignature'''|{{Func|<pre>job:list||() as xs:string*}}</pre>
|- valign="top"
| '''Summary'''
{| width='100%'
|- valign="top"
| width='120' | '''SignaturesSignature'''|{{Func|job:list-details||element(job)*}}<br/pre>{{Func|job:list-details|( $id as xs:string| := ()) as element(job)*}}</pre>
|- valign="top"
| '''Summary'''
| '''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>
|}
==job:bindings==
 
{{Announce|Introduced with Version 10.}}
{| width='100%'
|- valign="top"
| width='120' | '''SignaturesSignature'''|{{Func|<pre>job:bindings|( $id as xs:string|) as map(*)}}</pre>
|- valign="top"
| '''Summary'''
{| width='100%'
|- valign="top"
| width='120' | '''SignaturesSignature'''|{{Func|<pre>job:finished|( $id as xs:string|) as xs:boolean}}</pre>
|- valign="top"
| '''Summary'''
{| width='100%'
|- valign="top"
| width='120' | '''SignaturesSignature'''|{{Func|<pre>job:services||() as element(job)*}}</pre>
|- valign="top"
| '''Summary'''
;Version 10.0
* Updated: Renamed from ''Jobs Module'' to ''Job Module''. The namespace URI has been updated as well.
* Updated: {{Function||job:remove}} renamed from {{Code|jobs:stop}}.
* Updated: {{Function||job:result}}: options argument added.
* Added: {{Function||job:bindings}}
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu