This topic describes the states of asynchronous tasks and how to manage asynchronous tasks in the Function Compute console. Then, you can understand how to create, start, stop, and view asynchronous tasks.
Manage a task in the Function Compute console
Create an asynchronous task when you create a function
Log on to the Function Compute console. In the left-side navigation pane, click Tasks.
In the top navigation bar, select a region. On the Task Execution Functions page, click Create Function.
On the Create Function page, configure related parameters, configure a trigger, and then click Create.
NoteFor information about how to create a function, see the "Create a function" section of the Manage functions topic.
You can create a trigger when you create a function on the Create Function page. You can also create a trigger on the Triggers tab for an existing function.
If you use the preceding method to create a function, the Task Mode feature of Asynchronous Policy is automatically enabled. All asynchronous invocation requests to the function are processed in the task mode.
After the function is created, the function is displayed on the Function Name list of the Task Execution Functions page.
Enable the asynchronous task mode for an existing function
You can enable the asynchronous task mode for an existing function. For more information, see the "Create a function" section of the Manage functions topic.
Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
In the top navigation bar, select a region. On the Services page, click the desired service.
Click the function. On the function details page, click the Asynchronous Configurations tab. In the Asynchronous Policy section, click Modify.
In the Modify the Policy for Asynchronous Mode panel, set Task Mode to Enable and click OK.
Start or stop a task
Log on to the Function Compute console. In the left-side navigation pane, click Tasks.
- In the top navigation bar, select a region.
On the Task Execution Functions page, click the desired function.
On the Asynchronous Tasks tab, click Submit Task. In the dialog box that appears, select a task ID generation method and a task execution time, and then click OK.
You can also click the icon on the right side of the Submit Task parameter and select Configure Task Parameters from the drop-down list. An event function uses event and an HTTP function uses HTTP parameters to pass input parameters to the function to simulate task submission.
After you submit the task, refresh the page. You can view the task that is running or completed. You can log on to the instance, stop the task, re-execute the task, and view logs.
NoteIn asynchronous tasks, HTTP functions cannot be re-executed.
Call the Function Compute API or use the Function Compute SDK to manage a task
Create an asynchronous task
You can call the PutFunctionAsyncInvokeConfig operation and set the asynchronous invocation mode to task mode. Add the following configuration to AsyncConfig. After this configuration is added, all asynchronous invocations of the function change to the task mode.
{
"statefulInvocation": true
}
After you set the asynchronous invocation mode to task mode, you can still invoke functions in synchronous mode. However, the asynchronous task mode takes effect only for asynchronous invocations.
Enable the task mode
You can call the InvokeFunction operation to trigger an asynchronous invocation to start the task mode. You can add the HTTP header Stateful-Async-Invocation-Id
when you call the operation to specify a task ID. For more information, see Task deduplication.
View a task
After you submit a task, you can call the GetStatefulAsyncInvocation operation to query the execution status or execution records of the task.
You can call the ListStatefulAsyncInvocations operation to query the list of tasks that meet the specified conditions based on keywords or conditions.
Stop a task
After you submit a task, you can stop the task based on your business requirements. You can call the StopStatefulAsyncInvocation operation to stop a task. When you stop a task, you must enter its ID. The task ID is specified when you submit the task. The task ID can also be the ID that you obtained when you call the ListStatefulAsyncInvocations operation to query running tasks.
States of an asynchronous task
For each task invocation, Function Compute records the information about the state changes of tasks and allows you to query the states of a task in real time. You can view the states of a task by using the Function Compute SDK or the Function Compute console. A task can be in one of the following states:
State | Description |
Enqueued | The asynchronous invocation is enqueued and pending to be executed. |
Processed | The asynchronous invocation is dequeued and pending to be triggered. |
Running | The invocation is being executed, and your instance has started to run the task code. |
Succeeded | The invocation succeeds. |
Failed | The invocation fails. |
Stopped | You stopped the invocation, and the task is successfully terminated. |
Stopping | You stopped the task, and the task is being terminated. |
Expired | You configured a validity period for the asynchronous invocation. The invocation expired and was discarded. |
Invalid | The invocation becomes invalid and is not triggered due to specific reasons. For example, the function is deleted. |
Retrying | The asynchronous invocation is being retried due to an execution error. When the system is ready to retry the task, the state of the task changes to Running. |
You can call the GetStatefulAsyncInvocation operation to obtain the details of a task. You can also call the ListStatefulAsyncInvocations operation to filter tasks that are in a specified state.