arrow-down
    1. Widgets
  1. IAM
    1. Virtual apps
    1. Projects
    2. Layers
        1. AttachmentUploader
        2. Checkbox
        3. Combobox
        4. ContentSelector
        5. ContentTypeFilter
        6. CustomSelector
        7. Date
        8. DateTime
        9. Double
        10. GeoPoint
        11. HtmlArea
        12. ImageSelector
        13. Long
        14. MediaSelector
        15. Radiobutton
        16. Tag
        17. TextArea
        18. TextLine
        19. Time
      1. Field set
      2. Item set
      3. Option set
      4. Mixins
      5. Localization
    3. Content Types
    4. X-data
    5. Macros
    6. Custom styles
    7. Sites
      1. Regions
      2. Part component
      3. Layout component
      4. Text component
      5. Fragments
      6. Filtering
      7. Component indexing
      8. Visual editor
    8. Page templates
    1. Sandboxes
    2. Code
    3. Building
    4. Configuration
    1. Globals
    2. HTTP
    3. Controllers
    4. Filters
    5. Events
    6. Websocket
    7. Error handler
    8. ID provider
    9. Tasks
    10. Localization
    11. Mappings
    12. Components
    13. Processors
    14. Contributions
    15. Templating
    16. Main controller
    17. Java bridge
      1. Webapp Engine
        1. Image service
        2. Component service
      2. Admin Engine
      3. Asset service
      4. HTTP service
      5. IDprovider service
    1. Task engine
    2. Management Endpoint
    3. Statistics Endpoint
    1. Nodes and repos
    2. Properties
    3. Indexing
    4. Branches
    5. Queries (NoQL)
    6. Queries (DSL)
    7. Filters
    8. Aggregations
    9. Highlighting
    10. Editors
    1. Strategies
    2. Distributions
    3. Docker image
    4. Vhosts
    5. Configuration
    6. Backup & restore
    7. Systemd
    8. Clustering
    1. Admin API
    2. Application API
    3. Auditlog API
    4. Authentication API
    5. Cluster API
    6. Common API
    7. Content API
    8. Context API
    9. Event API
    10. Export API
    11. Grid API
    12. I18N API
    13. IO API
    14. Mail API
    15. Node API
    16. Portal API
    17. Project API
    18. Repo API
    19. Scheduler API
    20. Schema API
    21. Tasks API
    22. Value API
    23. VHost API
    24. Websocket API
  2. Audit Logs
    1. Upgrade Notes
    2. Upgrading Apps

Tasks

Contents

The XP framework also supports execution of asynchronous (potentially long running) background tasks.

Tasks may simply be executed as inline functions, or as named tasks. Named tasks are defined by creating a folder i.e. src/main/resources/tasks/<taskname> in your project.

Descriptor

Named tasks requires a descriptor file. The descriptor must be placed in task folder as follows: src/main/resources/tasks/<taskname>/<taskname>.xml.

Sample descriptor
<task>
  <description>Background job</description>
  <form>
    <input type="Long" name="count">
      <label>Number of items to process</label>
      <default>42</default>
      <occurrences minimum="1" maximum="1"/>
    </input>
  </form>
</task>

When parameters are passed to the task controller, they will be validated according to the descriptor schema.

The form element in the descriptor XML is optional, and may be left empty if the task does not accept any parameters.

Controller

Named tasks require a controller that represents the actual code to be executed. The controller must be placed in the task folder as src/main/resources/tasks/<taskname>/<taskname>.js.

A named task controller must export a run function, which will be invoked by the task engine. once a task is executed.

The run function will receive the task parameters as a JSON object.

taskId is provided as a second argument of the function XP XP 7.13.0 7.13.0

Example task controller
var taskLib = require('/lib/xp/task');

exports.run = function (params, taskId) {

    var count = params.count || 42;
    taskLib.progress({info: 'Initializing task ' + taskId});

    for (var i = 0; i < count; i++) {
        taskLib.progress({
            info: 'Processing item ' + (i + 1),
            current: i,
            total: count
        });
        processItem(i);
    }

    taskLib.progress({info: 'Task completed ' + taskId});
};

Execution

Named tasks may be executed by any controller in the system. Applications may even run tasks in other applications.

Below is an example of how to run a specific task.

Sample execution of named task
var taskLib = require('/lib/xp/task');

var taskId = taskLib.submitTask({
        descriptor: `mytask`,
        config: {
            count: 10
        }
    }
);

Distributable

XP XP 7.6.0 7.6.0

Named tasks may get distributed over other cluster nodes. Cluster node can be configured to accept or decline distributable tasks execution. If multiple cluster nodes can accept a task, only one cluster node will be selected to execute it. If none of the nodes can accept a distributable task, an error will be thrown.

Distributable task gets executed with the same context (user, repository/branch) it was originally submitted.

Make sure your Named task does not require a specific node to run on (i.e. it must not check for isMaster). If you need such check, use inline function task instead.
Nodes that do not accept distributable tasks execution are still capable to submit them.
For fine-grained control which nodes should be capable to execute distributable tasks use Application filter config or local apps. Or disable execution of named tasks entirely on some nodes by Task config.

API

Tasks are capable of producing status reports during execution. This is in particular useful for long running tasks where feedback is crucial.

Additionally, tasks produce a range of pre-defined events throughout their life cycle.

For more details on tasks, visit the task API documentation.


Contents

Contents

AI-powered search

Juke AI