Tasks
About
About Tasks
WHAT YOU'LL LEARN
- What are Tasks?
- What methods are available in a task definition?
- When to use lifecycle hooks?
Overview
Tasks allow you to run long operations asynchronously without blocking your application. You can process batches, migrate data, or perform bulk operations in the background.
To create a background task, you implement the TaskDefinition.Interface and register it using TaskDefinition.createImplementation().
Available Methods
When creating a background task, you implement the following methods:
Core Methods
run()- The main method where your task logic executes. Required.createInputValidation()- Validates the input data before the task runs. Optional.
Lifecycle Hooks
These hooks are called at different stages of task execution:
onBeforeTrigger()- Called before the task is triggered.onDone()- Called when the task completes successfully.onError()- Called when the task fails with an error.onAbort()- Called when the task is manually aborted.onMaxIterations()- Called when the task reaches the maximum iteration limit.
Task Responses
Inside the run() method, you receive a controller object that helps you control task execution. You use it to return different responses:
controller.response.done()- Task completed successfully.controller.response.continue()- Task needs more time, will resume in the next iteration.controller.response.error()- Task encountered an error and should stop.controller.response.aborted()- Task was manually stopped.
Controller Runtime Methods
The controller also provides runtime methods to check the task’s execution state:
controller.runtime.isAborted()- Returnstrueif the task was manually aborted.controller.runtime.isCloseToTimeout()- Returnstrueif the task is running out of time. Use this to returncontinue()and preserve your progress.