Community Blog Detailed Asynchronous Tasks: Task Status and Lifecycle Management

Detailed Asynchronous Tasks: Task Status and Lifecycle Management

This article introduces the task running status and run-time management.

By Jianyi (Alibaba Cloud Serverless Senior Development Engineer)


There is a very important concept in the task system: task status and lifecycle management. The state of segmentation contributes to having a clearer understanding of what happened to the system during use to facilitate targeted operations according to business conditions.

Alibaba Cloud Function Compute (FC) Serverless Task provides a variety of queryable states and the point in time for the transition between states. Function Compute also provides the concept of task lifecycle management at the function execution level. Users can determine the execution logic when the system initializes and reclaims a series of actions to implement complete run time lifecycle management.

This article will introduce the task running status and run-time management.

Task Status

Task Status Management

After a user submits a task and receives a successful submission, the task has entered the system's lifecycle management process.

Status Description
Enqueued User-triggered Task asynchronous messages have entered the internal queue and are awaiting processing.
Dequeued Task asynchronous messages have been dequeued from the Function Compute backend service and are waiting to be triggered.
Running The task is being called.
Succeeded Task invocation is successful.
Failed Task call execution failed.
Stopped The task call is terminated.
Stopping The task call is trying to stop.
Expired You have configured a validity period for the asynchronous invocation. The invocation is discarded and not triggered due to expiration.
Invalid The invocation is invalid and not triggered due to specific reasons. For example, the function is deleted.
Retrying The task call is being retried due to an execution error.

The status change of a task is managed by an internal state machine, and the status is revealed to support real-time queries. The entire state transition diagram is listed below:

Figure 1

1.  After a task is triggered, the task is enqueued. It changes to the Enqueued status and returns that the task is triggered successfully.

2.  The task is dequeued in the backend service of Function Compute, and the task becomes Dequeued.

3.  The backend checks the task configuration. If:

  • The validity period of an asynchronous message is configured. The difference between the message dequeue time and the enqueue time has exceeded the validity period, the task is discarded and changes to the Expired state. Mission terminated.
  • If the function corresponding to the task has been deleted or an error occurs when creating an instance, the message is discarded and the task changes to the Invalid status.

4.  After the check, the task enters the Running state. At this time, the task has triggered the actual execution.

5.  After the task is executed, it will change to the following according to the return.

  • Retrying: The number of retries is configured. The default value is 3. If the task fails, the task enters the Retrying state and then changes to the Running status.
  • Failed: The task failed, and the number of retries exceeded. In this case, the task status is changed to Failed.
  • Succeeded: The task is successfully executed.

6.  If the user triggers Cancel during the entire status forwarding process, the task will change to the Stopping status first and try to stop the task execution. When the task is stopped, the task enters the Stopped status.

Task Run Time Management and Lifecycle

When the task status enters Running, the actual execution of the task is handed over to the Function Compute run time.

In terms of security, Function Compute will isolate different accounts by VM. Functions under the same account may run in the same VM. There is a client in the VM that manages the container to trigger the execution of the function and collect the execution results.

A user's running instance has several different states:

Figure 2

Function Compute provides interfaces for all the preceding instance status changes and supports the user-side configuration of the corresponding logic:

  1. Create → Execute Request Phase: It supports the Initializer function and supports initialization of instances. You can configure operations (such as global variables and connection pool initialization).
  2. Executing → Pause Instance after Execution: It supports the PreFreeze interface and supports user-side custom logic before the function Pause instance.
  3. Executing → External Cancel: Function Compute forces the user instance to be restarted. The PreStop interface is supported before the restart. Users can configure the logic related to graceful stop to support custom behavior when canceling.
  4. Pause → Destroy the Instance after Completion: When no request is made for a while, Function Compute will destroy the instance. In this case, the PreStop interface is called. You can configure the behavior of destroying containers (such as disabling the connection pool).

Figure 3

Stop Operations of the Task

The current phase Function Compute supports stopping a single task.

When a user operation is stopped, you can configure the PreStop interface to perform a series of resource reclamation tasks before the operation is stopped. The stop operation user can use the SDK or the console to call the operation. Let’s take Go as an example. The pseudo code for stopping one-time execution is listed below:

import fc "github.com/aliyun/fc-go-sdk"

func CancelJob() {
    stopInput := fc.NewStopStatefulAsyncInvocationInput("ServiceName", "FunctionName", "TaskUUID")
    output, err := fcClient.StopStatefulAsyncInvocation(stopInput)


Serverless Task provides the status details of each task and persists these details in real-time. Users can query this status information in real-time according to their needs and perform corresponding operations according to the execution and business conditions.

During the running phase of a task, Function Compute provides relevant interfaces during the state transition of all instances to support user-defined logic before and after task execution. Combined with the PreStop function and the Cancel function, users can easily implement elegant operations of tasks.

0 0 0
Share on

Alibaba Cloud Serverless

100 posts | 7 followers

You may also like


Alibaba Cloud Serverless

100 posts | 7 followers

Related Products