Function Compute invokes your functions in instances, which provide secure and isolated runtime environments. Function Compute provides hooks based on lifecycles of instances. Hooks can be used to address pain points such as metric data delay or loss when you migrate traditional applications to serverless architectures. This topic describes lifecycles of function instances, how to configure the lifecycle hooks for function instances, and how to query logs of hooks.
Instance lifecycles
As shown in the preceding figure, the lifecycle of a Function Compute instance include the Create, Invoke, and Destroy phases.
Create
During this phase, Function Compute perform the following tasks in sequence:
Create an instance.
Initialize the runtime.
Run the Init hook.
If you use provisioned instances, Function Compute allocates function instances and performs the
Create
process immediately after you configure provisioned instances. Function invocations may not be initiated right away. Therefore, a long time interval may exist before runtime initialization, Init hook running, and invocations.If you use on-demand instances, Function Compute immediately performs the
Create
process when requests are submitted. If an Init hook is configured for the function, theInvoke
process is performed after the Init hook is executed.For a function instance, the Init hook configured for the function is executed only once. If the execution fails, another function instance is allocated to execute the
Init
process. The instance whoseInit
hook failed is destroyed.Invoke
By default, instances that run in standard runtimes of Function Compute can execute only one request at a time. Instances that run in custom runtimes and Custom Container runtimes execute multiple requests at a time. You can configure a value greater than 1 to the instance concurrency to allow one instance to execute multiple requests at a time.
Destroy
This phase is triggered if a function instance does not receive any invocations for a period of time. During this phase, Function Compute executes the PreStop hook first. You can perform cleanup tasks in PreStop hooks.
Instance freezing mechanism
Function Compute freezes an instance when no invocations are initiated for a period of time and unfreezes the instance when new requests are submitted.Function Compute The following figure shows the process.
An instance is frozen in the following scenarios:
The instance is initialized and no invocations are made yet.
The instance is invoked to process requests and subsequent requests are not immediately submitted.
After invocations are complete, Function Compute freezes the function instances. In this case, the background processes, threads, and coroutines in the program cannot continue to run, and asynchronous logs may fail to write.
Provisioned instances for which the idle mode is not enabled are not frozen when they are not processing requests.
Limits
All runtimes support Init and PreStop hooks.
The input parameter of a PreStop hook does not have the event parameter.
A PreStop hook have no return values. The logic that is appended to the PreStop hook to return values does not take effect.
If you use a Java runtime, you must update fc-java-core to 1.4.0 or later. Otherwise, you cannot use the PreStop hook.
When a function returns responses, Function Compute freezes the function instance. You cannot assume that all asynchronous processes, threads, and coroutines are successfully executed when responses are returned. You cannot assume that asynchronous logs are refreshed either.
Prerequisites
A function is created. For more information, see Create a function.
Configure lifecycle hooks
The billing for PreFreeze or PreStop calls is the same as that for InvokeFunction calls. For more information, see Billing overview.
Configure hooks in the Function Compute console
When you use the Function Compute console to create a function, you cannot configure PreStop hooks. You can configure a PreStop hook when you update the function.
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the function details page, click the Configurations tab. In the left-side navigation pane, click Lifecycle. Then, click Edit.
In the lifecycle panel, configure the lifecycle hooks and timeout periods and click OK.
NoteYou must configure a hook and timeout period for each extension function. The format of a hook is [File name].[Extension function name]. For example, if you set the PreStop hook to index.preStop when you create a function in a Python runtime, the file name is index.py and the name of the PreStop function is preStop.
After you configure a hook, you must implement the corresponding function in code execution.
Click the Code tab. In the code editor, enter the code of the extension function.
For example, if you set the PreStop hook to
index.preStop
, you must implement the preStop function. For more information about how to implement lifecycle hooks in different runtimes of Function Compute, see Lifecycle hooks of function instances.NoteThe online IDE is supported by PHP, Python, Node.js, and custom runtimes but not supported by runtimes of compiled languages such as Java, Go, .NET, or custom container runtimes.
Click Deploy in the upper part of the code editor, and then click Test Function.
Lifecycle hooks for function instances
All runtimes in the Function Compute support Init and PreStop hooks. The following table describes the methods to implement the lifecycle hooks of instances in different runtimes.
Runtime | Description | Reference |
Node.js | Implement lifecycle hooks for instances in Node.js runtimes. | |
Python | Implement lifecycle hooks for instances in Python runtimes. | |
PHP | Implement lifecycle hooks for instances in PHP runtimes. | |
Java | Implement lifecycle hooks for instances in Java runtimes. | |
C# | Implement lifecycle hooks for instances in C# runtimes. | |
Go | Implement lifecycle hooks for instances in Go runtimes. | |
Custom Runtime | Implement lifecycle hooks for instances in custom runtimes. | |
Custom Container | Implement lifecycle hooks for instances in Custom Container runtimes. |
Query logs related to hooks
After you configure an instance lifecycle hook and execute the code of the hook, you can query the logs of the hook.
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the function details page, click the Logs tab. On the Requests tab, find the request that you want to use and click Advanced Logs in the Actions column.
You can use the copied instance ID to query the start and end logs of all lifecycle hooks. You can also use the instance ID together with a hook keyword to query the start and end logs of a specified hook, such as
c-62833f38-20f1629801fa4bd***** and PreStop
.You can also query the request logs based on the request ID in the start logs and the end logs. If the user log does not contain a request ID, you can click the icon to obtain the context log.
Billing rules
You are not charged for the number of invocations for instance lifecycle hooks. The billing of other billable items is the same those in the invocation phase. The following figure shows the billable phases.
For more information about billing, see Billing overview.