Job Functions
This 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.
If you want to evaluate queries as part of the same query, the XQuery Functions are usually the better choice.
Conventions
All functions are in the http://basex.org/modules/job namespace, to which the job prefix is statically bound. Errors will be bound to the same prefix.
Synchronous Execution
job:execute
Updated: Raise deadlock error if execution would deadlock the calling query.
Updated: A function item can be invoked instead of a query.
| Signature | job:execute( $query as (xs:string|xs:anyURI|fn(*)), $bindings as (map((xs:string|xs:QName), item()*)|array(*))? := {}) as item()*admin | ||
|---|---|---|---|
| Summary | Registers a job for the specified $query (of type xs:string, or of type xs:anyURI if it points to a resource), waits for its execution and returns its result or a raised error. The query can be updating, and variables and the context value can be declared via $bindings (see xquery:eval for more details).
Instead of a query, a function item can be supplied, which is invoked with the arguments of the The function is helpful for scripting. It can be used to run multiple XQuery expressions one after another. Deadlocks: A It is safe to use the function if the calling query does not lock the databases addressed by the registered query. See Transaction Management for further details. Updating function items: An updating function item always raises this error. The locks of a function item are known to the calling query, which therefore write-locks the databases the job will update before the job is registered. Supply an updating query as string instead. | ||
| Errors |
| ||
| Examples | Creates a database and adds an XQuery value to the database.Executes eight scripts in a row and binds the script number to a variable. Reports back errors. |
Asynchronous Execution
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 new query jobs and access existing ones. Jobs may be executed immediately (i.e., as soon as a free slot is available) 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
Updated: Support for Cron syntax and daylight saving changes added.
Updated: Options added for permissions, timeouts and memory limits.
Updated: A function item can be invoked instead of a query.
| Signature | job:eval( $query as (xs:string|xs:anyURI|fn(*)), $bindings as (map((xs:string|xs:QName), item()*)|array(*))? := {}, $options as map(*)? := {}) as xs:stringadmin | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Summary | Schedules the evaluation of a new query job for the supplied $query (of type xs:string, or of type xs:anyURI if it points to a resource), and returns a job ID. The job will be queued until a free slot is available, and the query result can be cached. The query can be updating, and variables and the context value can be declared via $bindings (see xquery:eval for more details).
Instead of a query, a function item can be supplied, which is invoked with the arguments of the $options are available:
| ||||||||||||||||||||||||||||||||||||||||||
| Errors |
| ||||||||||||||||||||||||||||||||||||||||||
| Examples | Cache query result. The returned ID can be used to pick up the result with job:result.Invokes a function item with a single argument.A happy birthday mail will be sent at the given date.The RESTXQ functions can be called to execute a query at 02:00 daily. An ID will be returned by the first function, which can be used to stop the scheduler via the second function.The query will be evaluated at 6am on every weekday. See Cron Syntax for the supported patterns.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.The following expression, if stored in a file, will be evaluated every 5 seconds. |
job:result
| Signature | job:result( $id as xs:string, $options as map(*)? := {}) as item()*admin | ||||||
|---|---|---|---|---|---|---|---|
| Summary | Returns the cached result of a job with the specified job $id:
The following
| ||||||
| Examples | The following RESTXQ function will either return the result of a previously started job or raise an error. |
job:info
Added: New function.
| Signature | job:info( $id as xs:string) as map(*)?admin |
|---|---|
| Summary | Returns the query information of a job with the specified job $id:
The returned map holds the information that is output if the |
| Examples | Waits for the job and returns the total time of the query. |
job:remove
| Signature | job:remove( $id as xs:string, $options as map(*)? := {}) as empty-sequence()admin | ||||||
|---|---|---|---|---|---|---|---|
| Summary | Triggers the cancelation of a job with the specified $id, cancels a scheduled job or 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 $options are available:
| ||||||
| Examples | Stops and discards all jobs except for the current one.Interrupts the current job. |
job:wait
| Signature | job:wait( $id as xs:string) as empty-sequence()admin | ||
|---|---|---|---|
| Summary | Waits for the completion of a job with the specified $id:
| ||
| Errors |
|
Services
A job can be registered as service by supplying the service option to job:eval:
(: register job as service; will be run every day at 1 am :)
job:eval(
'db:drop("tmp")',
options := { 'id':'cleanup', 'start':'01:00:00', 'interval':'P1D', 'service': true() }
),
(: list registered services :)
job:services(),
(: result: <job base-uri="..." id="cleanup" interval="P1D" start="01:00:00">db:drop("tmp")</job> :)
(: unregister job :)
job:remove('cleanup', { 'service': true() })
Notes:
- All job services will be scheduled for evaluation when the BaseX server or BaseX HTTP server is started.
- If a job service is outdated (e.g. because a supplied end time has been exceeded), it will be removed from the jobs file at startup time.
- The job definitions are stored in a
jobs.xmlfile in the database directory. It can also be edited manually. - Each service requires an
id, and no two services may share one: the ID is the handle for unregistering a service. Entries of older versions that have no ID are still evaluated, but they can only be removed from the jobs file manually. - Only the options that were supplied are stored; the remaining ones are reapplied with their default values when the file is read.
View Jobs
job:current
| Signature | job:current() as xs:string |
|---|---|
| Summary | Returns the ID of the current job. |
job:list
| Signature | job:list() as xs:string*admin |
|---|---|
| Summary | Returns the IDs of all jobs that are currently registered. The list includes scheduled, queued, running, stopped, and finished jobs with cached results. |
| Examples | Returns the same job ID as job:current if no other job is registered. |
job:list-details
Added: cron attribute.
| Signature | job:list-details( $id as xs:string := ()) as element(job)*admin |
|---|---|
| Summary | Returns information on all jobs that are currently registered, or on a job with the specified $id (or an empty sequence if this job is not found). The list includes scheduled, queued, running jobs, and cached jobs. A string representation of the job, or its URI, will be returned as a value. The returned elements have additional attributes:
|
job:next
Added: New function.
| Signature | job:next( $cron as xs:string, $count as xs:integer := 1) as xs:dateTime* | ||||
|---|---|---|---|---|---|
| Summary | Returns the next $count points in time at which the cron expression $cron will be triggered. No job is registered, and no query is evaluated: the function only reports when a job with this expression would be run.
Fewer than The results are relative to | ||||
| Errors |
| ||||
| Examples | Returns the next three weekday mornings, e.g. 2026-07-23T08:00:00+02:00, 2026-07-24T08:00:00+02:00, 2026-07-27T08:00:00+02:00. The weekend is skipped.Returns an empty sequence: February 30 does not exist. |
job:bindings
| Signature | job:bindings( $id as xs:string) as map(*)admin |
|---|---|
| Summary | Returns the variable bindings of an existing job with the specified $id. If a function item is invoked, its arguments are returned, using the parameter names of the function. If no variables have been bound to this job, an empty map is returned.
|
job:finished
| Signature | job:finished( $id as xs:string) as xs:booleanadmin |
|---|---|
| Summary | Indicates if the evaluation of an already running job with the specified $id has finished. As the IDs of finished jobs will usually be discarded, unless caching is enabled, the function will also return true for unknown jobs.
|
job:services
| Signature | job:services() as element(job)*admin | ||
|---|---|---|---|
| Summary | Returns a list of all jobs that have been persistently registered as Services. | ||
| Errors |
|
Errors
| Code | Description |
|---|---|
cron | The specified cron expression is invalid, or it will never match a date. |
deadlock | Execution of the job would deadlock the calling query. |
function | A function item cannot be registered as service or scheduled for later execution. |
id | The specified ID is invalid or has already been assigned. |
options | The specified options are conflicting. |
overflow | Too many queries are registered, or too many query results are cached. |
permission | The current user does not have the permissions requested for the job. |
range | A specified time or duration is out of range. |
self | The current job cannot be addressed. |
service | A service has no ID, the ID is already assigned to another service, an option is not allowed for services, or registered services cannot be parsed, added or removed. |
Changelog
Version 13.0- Added:
job:info - Added:
job:next - Updated:
job:eval: a service must have a uniqueid;memory,timeoutandpermissionare rejected. - Updated:
job:eval,job:list-details: Cron syntax added. - Updated:
job:eval:permission,timeoutandmemoryoptions added. - Updated:
job:eval: Support for daylight saving changes. - Updated:
job:execute: Raisedeadlockerror if execution would deadlock the calling query. - Updated:
job:eval,job:execute: a function item can be invoked instead of a query.
- Added:
job:execute
- Added:
job:bindings - Updated: Renamed from Jobs Module to Job Module. The namespace URI has been updated as well.
- Updated:
job:removerenamed fromjobs:stop. - Updated:
job:result: options argument added.
- Updated:
job:result: return empty sequence if no result is cached.
- Updated:
job:eval: integers added as valid start and end times.
- Updated:
job:eval: option added for writing log entries. - Updated:
job:list-details: interval added.
- Removed:
job:invoke(merged withjob:eval)
- Updated:
job:list-details: registration time added.
- Added:
job:invoke, Services
- Updated:
job:eval:idoption added.
- Added: New module added.