This topic describes how Node.js implements and applies the function instance lifecycle callback method.
Background information
After you implement and configure the function instance lifecycle callback, the Function Compute will call the corresponding callback program when the related instance lifecycle event occurs. Currently, Node.js run time supports initializer, PreFreeze, and PreStop function instance lifecycle callbacks. For more information, see Function instance lifecycle callback.
Initializer callback
Initialization callback (Initializer callback) is executed after the function instance is successfully started and before the request handler is run. The Function Compute ensures that the Initializer callback can be successfully executed at most once within an instance lifecycle. For example, after your Initializer callback fails the first execution, the system retries until it succeeds before executing your request handler.
The Initializer callback has only one context input parameter and uses the same method as the event request handler.
One of the simplest initializer callbacks is as follows.
exports.initialize = function(context, callback) {
console.log('initializer');
callback(null, "");
};
The initialize
is the name of the Initializer callback method. It must be the same as the Initializer callback program that you configured in the Function Compute console. For example, if the Initializer callback program that you configured for a function is index.initialize
, the Function Compute will load the initialize
method defined in the index.js
after configuring the Initializer property.
Method signature
- The input parameters are
context
only and provide the running context information for your FC function call when it is called. - Initializer functions return no values.
PreFreeze callback
The PreFreeze callback program is executed before the function instance is frozen. The method signature is the same as the Initializer callback.
One of the simplest PreFreeze programs is shown below.
module.exports.preFreeze = function(context, callback) {
console.log('preFreeze');
callback(null, "");
};
PreStop callback
The pre-stop callback program (PreStop callback) is executed before the function instance is destroyed. The method signature is the same as the Initializer callback.
One of the simplest preStop programs is shown below.
module.exports.preStop = function(context, callback){
console.log('preStop');
callback(null, "");
}
Configure the lifecycle callback function
Use the Function Compute console
[File name. Method name]
. Example:
- The Initializer callback program is set to
index.initialize
to indicate theinitialize
method in theindex.py
file. - The PreFreeze callback is set to
index.preFreeze
to indicate thepreFreeze
method in theindex.py
file. - The PreStop callback program is set to
index.preStop
, which indicates thepreStop
method in theindex.py
file.

Configure by using the Serverless Devs tool
s.yaml
configuration file.
- Initializer callback configuration
Add the initializer and initializationTimeout fields under function configuration.
- PreFreeze callback configuration
Add instanceLifecycleConfig.preFreeze fields under function configuration, including handler and timeout.
- PreStop callback configuration
Add instanceLifecycleConfig.preStop fields under function configuration, including handler and timeout.
The following shows an example.
edition: 1.0.0 # The version of the YAML syntax. The version complies with the semantic versioning specification.
name: hello-world # The name of the project.
access: default # Key alias
services:
fc-deploy-test: # Module name
component: devsapp/fc # The name of the component.
props: # Attribute values of components
region: cn-hangzhou # Region
service: # Service configuration
name: the name of the fc-deploy-service# service.
description: a short description of the dem component# Service
function: # Function configuration
name: the name of the fc-base-service# function.
description: 'Short description of the this is test'# function
codeUri: './code' # Code location
handler: 'The entry for index.handler'# function execution. The specific format is related to the language.
memorySize: 128 # The memory specifications of the function.
runtime: nodejs14 # run time
timeout: 60 # The timeout period of the function.
initializationTimeout: 20 # The timeout period of the initialization method.
initializer: index.initialize # The initialization method.
instanceLifecycleConfig: # Extension function
preFreeze: # The PreFreeze function
handler: index.preFreeze # Function entry
timeout: 60 # The timeout period.
preStop: # PreStop functions
handler: index.preStop # Function entry
timeout: 60 # The timeout period.
For more information about the YAML configuration specifications of Serverless Devs, see Basic features.
Sample programs
Function Compute provides a MySQL sample program that uses Initializer callback and PreStop callback. In this example, the Initializer callback function is used to obtain the MySQL database configuration from environment variables, create a MySQL connection, and test the connectivity. The PreStop callback function is responsible for closing the MySQL connection.
For more information, see nodejs14-mysql.