This topic describes how to implement lifecycle hooks for function instances in a PHP runtime.
Background
After you implement and configure a lifecycle hook for function instances, Function Compute calls the hook when the corresponding instance lifecycle event occurs. In a PHP runtime, you can configure Initializer and PreStop hooks for function instances. For more information, see Function instance lifecycles.
The billing rules for lifecycle hooks of function instances are the same as the billing rules for common invocation requests. However, the execution logs can be queried only in Function Logs, Real-time Logs, and Advanced Logs. The logs for lifecycle hooks are not displayed in the invocation request list. For more information, see View the logs of instance lifecycle hooks.
Initializer hook
Example
An Initializer hook is invoked after a function instance is started and before a handler is run. Function Compute ensures that the Initializer hook is successfully invoked only once in the lifecycle of a function instance. If the Initializer hook fails to be invoked, the system retries the Initializer hook until the Initializer hook is successfully invoked, and then runs your handler. To make sure that the Initializer hook can be repeatedly retried, configure the Initializer hook properly.
An Initializer hook contains only the $context parameter and can be used in the same way as a handler.
The following sample code provides an example of a simple Initializer hook:
<?php
function my_initializer($context) {
$logger = $GLOBALS['fcLogger'];
$logger->info("hello world");
}
?>
In the preceding sample code, my_initializer
is the method name of the Initializer hook. The method name must be the same as that in the value of the Initializer Hook parameter that is configured for your function in the Function Compute console. For example, if the value of the Initializer Hook parameter is main.my_initializer
, Function Compute loads the my_initializer
method that is defined in the main.php
file after the Initializer property is configured.
Method signature
The Initializer hook contains only the
context
parameter. The information specified by this parameter is the same as the value of thecontext
parameter specified for a handler.The
initializer
andinitializationTimeout
parameters incontext
are used for the Initializer hook. If you want to use an Initializer hook, set the two parameters to the values of the Initializer Hook and Initializer Hook Timeout Period parameters that you configure for your function. Otherwise, the values are empty and the hook does not take effect.No value is returned.
PreStop hooks
A PreStop hook is executed before a function instance is destroyed. The method signature of a PreStop hook is the same as the method signature of an Initializer hook.
The following sample code provides an example of a PreStop hook:
<?php
$counter = 0;
function preStop($context) {
$GLOBALS['fcLogger']->info("preStop ok");
}
function handler($event, $context) {
global $counter;
$counter += 2;
return $counter;
}
?>
You can query logs of the PreStop hook in the Logstore that is configured for your function. For example, you can use a statement in the following format to query all logs of the function. For more information, see Query logs related to hooks.
<funcName> AND <ServiceName> AND qualifier: <VERSION>
Configure lifecycle hooks
Use the Function Compute console
You can configure the Initializer Hook and PreStop Hook parameters on the FC tab of a function in the Function Compute console. For more information, see Configure the instance lifecycles. The format of a hook is [File name.Method name]
. Examples:
If you set the Initializer Hook parameter to
index.initialize
, theinitialize
method in theindex.php
file is loaded.If you set the PreStop Hook parameter to
index.preStop
, thepreStop
method in theindex.php
file is loaded.
Use Serverless Devs
If you use Serverless Devs to configure lifecycle hooks, you must add the Initializer hook and PreStop hook configurations to the s.yaml
file.
Initializer hook
Add instanceLifecycleConfig.initializer, including the handler and timeout fields to function.
PreStop hook
Add instanceLifecycleConfig.preStop, including the handler and timeout fields to function.
Sample code:
edition: 3.0.0
name: fcDeployApp
access: "default"
vars: # The global variables
region: "cn-hangzhou"
resources:
hello_world:
component: fc3 # The name of the component
props:
region: ${vars.region} # For information about how to use variables, visit https://docs.serverless-devs.com/serverless-devs/yaml#%E5%8F%98%E9%87%8F%E8%B5%8B%E5%80%BC.
functionName: "testphp"
description: 'this is a test'
runtime: "php7.2"
code: ./
handler: index.handler
memorySize: 128
timeout: 30
instanceLifecycleConfig: # The extension function
initializer: # The Initializer hook
handler: index.my_initializer
timeout: 60
preStop: # The PreStop hook
handler: index.preStop # The handler
timeout: 60 # The timeout period
For more information about the YAML syntax of Serverless Devs, see Common commands of Serverless Devs.
View the logs of instance lifecycle hooks
You can view the logs for lifecycle hook in Logs.
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 Test Function tab, click Test Function, and then choose .
On the Logs tab, you can view function invocation logs and Initializer logs. Example:
2024-03-04 17:57:28FC Initialize Start RequestId: 1-65e59b07-1520da26-bf73bbb91b69 2024-03-04 17:57:282024-03-04 09:57:28.192 1-65e59b07-1520da26-bf73bbb91b69 [info] initializer 2024-03-04 17:57:28FC Initialize End RequestId: 1-65e59b07-1520da26-bf73bbb91b69 2024-03-04 17:57:28FC Invoke Start RequestId: 1-65e59b07-1520da26-bf73bbb91b69 2024-03-04 17:57:28FC Invoke End RequestId: 1-65e59b07-1520da26-bf73bbb91b69
Each function instance is cached for a period of time and not immediately destroyed, you cannot view logs for PreStop hooks right away. To quickly trigger a PreStop hook, you can update the function configurations or function code. After the update is complete, you can view the logs for PreStop hooks in Function Logs. The following sample code shows an example:
2024-03-04 18:33:26FC PreStop Start RequestId: 93c93603-9fbe-4576-9458-193c8b213031 2024-03-04 18:33:262024-03-04 10:33:26.077 93c93603-9fbe-4576-9458-193c8b213031 [info] preStop 2024-03-04 18:33:26FC PreStop End RequestId: 93c93603-9fbe-4576-9458-193c8b213031
Sample program
Function Compute provides a sample MySQL program that uses Initializer and PreStop hooks. In this example, the Initializer hook is used to obtain the configurations of the MySQL database from environment variables, create a connection to the MySQL database, and then test the connectivity. The PreStop hook is used to close the MySQL connection. For more information, see php72-mysql.