Warning
You're browsing the documentation for an old version of Webiny. Consider upgrading your project to Webiny 5.41.x.
Use with caution!
This feature is experimental and is subject to change in future releases.
Can I use this?

This feature is available since Webiny v5.39.0.

What you’ll learn
  • how to handle the Background Task run
  • what are available methods
  • how to log important information

Handling the Task Run
anchor

When you define the task, you need to provide the run method, as it is the method getting executed when the task is being executed. The run method receives an object parameter with the following variables, defined in the interface ITaskRunParamsexternal link :

  • context
  • input
  • response
  • isCloseToTimeout()
  • isAborted()
  • store

ThecontextObject
anchor

Via the context object, you can access whole Webiny system. The base interface of the context object is Contextexternal link, but you can pass your own, which must extend the base interface.

You can pass your own Ge interface/type of the context variable when defining the task:

TheinputObject
anchor

The input object is the input which was sent to the task when it was triggered.

By default, it is of plain object type Record<string, any>, but you can pass your own type/interface when defining the task:

TheresponseObject
anchor

The response object handles the output from the task run. Available methods are:

  • done
  • continue
  • error
  • aborted

ThedoneMethod
anchor

This method signalizes that the task has finished its job and that the Step Function will finish as well. It also signalizes that task status should be set to success. Optional message will be stored as well.

ThecontinueMethod
anchor

This method signalizes that the task has not finished its job and that the Step Function should continue running the Lambda handler.

Note that the continue method MUST receive some data, which is of same type as the input variable, as the first parameter.

Why? Because the data you send into continue is the data you will receive on the next iteration of the task run as the input variable.

The continue method accepts a second, optional, parameter via which you can set waiting time for the next task run. You can either send a seconds property, which takes a number, or a date property, which takes a Date object.

If you are setting waiting time for the `continue` method, note that the maximum waiting time is 355 days, which is almost the maximum life time for the AWS Step Function.

TheerrorMethod
anchor

This method signalizes that the task has finished its job with an error and that the Step Function will finish as well, in an error state. It also signalizes that task status should be set to error.

TheabortMethod
anchor

This method signalizes that the task was aborted by the user. Note that this is not an error, but a user triggered abort.

When you write your code, you must check if the task was aborted via the isAborted() method, and if it was, you must return the abort response. This is meant to be used while the task has some code which loops continuously, like reading a lot of records from the database, with paginating through the records.

TheisCloseToTimeoutMethod
anchor

The isCloseToTimeout method is a method which returns a boolean value, which tells you if the Lambda instance is close to the timeout. This is useful when you have a lot of code to run, or you have some idea that the code will take some time to execute, and you want to check if you have enough time to finish the code execution.

The isCloseToTimeout method accepts an optional parameter, which is a number of seconds under which will this method return true. The default is 180 seconds.

As a developer, you are responsible for checking if the task is close to the timeout, and for handling the task run accordingly.

Webiny provides you with the background task mechanism, but you must handle the task run, and timeouts, yourself.

TheisAbortedMethod
anchor

The isAborted method is a method which returns a boolean value, which tells you if the task was aborted by the user.

ThestoreObject
anchor

The store object is of ITaskManagerStore interface type, which is defined hereexternal link.

It is used for storing and retrieving data from the task run, including task logs.

Available methods on the object are:

  • getTask
  • getStatus
  • updateTask
  • updateInput
  • getInput
  • addInfoLog
  • addErrorLog

Logging
anchor

Using the addInfoLog and addErrorLog methods, you can store some important log into the database.

Because the Background Task Lambda handler can be executed multiple times, depending on the developer response, the logging has been implemented on per iteration base.

It means that each of the execution iteration gets its own record, which contain individual logs from that execution iteration.

Logging

Do not use the addInfoLog and addErrorLog methods for a lot of logging, or logging large amounts of data.

The logs are stored in the database as you send them, and if you send a lot of logs, or large logs, you will hammer the database.

Use the built-in logs only for important information, like starting or finishing a part of a task.