Author: gradually
foreword
There is a very important concept in the task system, that is, the state and life management cycle of the task. Its essence is the life cycle management of tasks. The subdivided status helps to understand more clearly what is happening in the system when using it, so that it can be operated according to the business situation in a targeted manner. Function Compute Serverless Task provides a variety of states that can be queried, and provides time points for transitions between states. At the function execution level, Function Compute also provides the concept of life cycle management of tasks. Users can determine the execution logic when the system initializes and recycles task instances according to their needs, so as to achieve complete runtime life cycle management. This article will introduce the two aspects of task running status and runtime management respectively.
Task status and life cycle management
task state management
When a user submits a task and receives a successful submission return, the task has entered the system's management process of its life cycle. The state change of a task is managed by an internal state machine, and the external state is exposed to support real-time query. The entire state transition diagram is shown below:
figure 1
- After the user triggers a task, the task is enqueued, changes to the Enqueued state, and returns the trigger success;
- The task is dequeued in the Function Compute backend service, and the task becomes the Dequeued state;
- The backend checks the task configuration. if
<!---->
- If the validity period of asynchronous messages is configured, and the difference between the time when the message is out of the queue and the time when it is in the queue exceeds the validity period, the task is discarded and changed to the Expired state. task termination;
- If the function corresponding to the task has been deleted, or there is an error in creating the instance, the message will be discarded and the task will be changed to Invalid state;
<!---->
- After checking, the task officially enters the Running state. At this point the task has triggered the actual execution;
- After the task is executed, it will change to the following states according to the return:
<!---->
- Retrying: The user has configured the number of retries (the default is 3), and the task execution fails, it will enter the retrying state, and then change to the Running state;
- Failed: The task execution failed and the number of retries exceeded. At this point, the task status will be changed to Failed;
- Succeeded: The task was executed successfully.
<!---->
- If the user triggers Cancel during the entire state transition, the task will first change to the Stopping state and try to stop the task execution. When the task stops executing successfully, the task enters the Stopped state.
Task runtime management and life cycle
When the task state enters Running, the actual execution of the task has been handed over to the runtime of Function Compute. In terms of security, Function Compute isolates different accounts according to VMs, and functions under the same account may run in the same VM. There is a client in the VM responsible for managing the container to actually 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 above instance state changes, and supports the configuration of corresponding logic on the user side.
- Create complete -> Execute request stage: Support Initializer function, support initialization instance operation. Users can configure related operations such as global variables, connection pool initialization, etc.;
- During execution -> Pause instance after execution: Supports the PreFreeze interface, and supports executing user-side custom logic before the function Pause instance;
- Executing -> External cancel: Function Compute will force restart the user instance and support the PreStop interface before restarting. Users can configure the related logic of graceful stop to support custom behavior when canceling;
- Pause -> Destroy the instance after completion: When there is no request for a period of time, Function Compute will destroy the instance. At this point, the PreStop interface will be called, and the user can configure the behavior of destroying the container (such as closing the connection pool, etc.).
image 3
task stop action
Currently, Stage Function Compute supports the operation of stopping a single task. When the user operation stops, the PreStop interface can be configured to perform a series of resource recovery work before stopping. Stop operation user can use SDK or console to call. Taking the Go language as an example, the pseudo code to stop one execution is as follows:\
import fc "github.com/aliyun/fc-go-sdk"
func CancelJob() {
stopInput := fc.NewStopStatefulAsyncInvocationInput("ServiceName", "FunctionName", "TaskUUID")
output, err := fcClient.StopStatefulAsyncInvocation(stopInput)
...
}
Summarize
Serverless Task provides status details of each task and persists these details in real time. Users can query these status information in real time as needed, and perform corresponding operations according to execution and business conditions. In the running phase of a task, Function Compute provides relevant interfaces in the state transition process of all instances, and supports user-defined logic before and after task execution. Combined with the PreStop function and the Cancel stop task function, users can easily implement the elegant operation of the task.
past review
Function Compute Asynchronous Task Capability Introduction - Task Triggering and Deduplication
1 minute Serverless build a real website
Build your first personal website in 1 minute with Serverless!
Free credit, easy to get started! Xiaobai can also build a website very quickly: no need to consider the server and website source code, we provide you with free computing resources, operation and maintenance management servers. After completing the scene experience during the event, you will have the opportunity to receive a 10 yuan voucher from Tmall Supermarket . (recommended for pc experience)
Event time: June 20-July 1, 2022 (collection during working days)
*Q&A group: Dingding search "44700570" *
Experience address: Click to read the original text or scan the QR code
Click here to go straight to the experience!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。