An initializer function is used to ensure certain business logic to be successfully executed on a Docker instance only once. This topic describes the structure and features of Node.js initializer functions.

Definition

The following example shows the definition of a simple initializer function.

exports.my_initializer = function(context, callback) {
    console.log('hello world');
    callback(null, "");
};       

Function name

my_initializer must correspond to the initializer field that you specify when you add the initializer function. For example, if you set the initializer field to main.my_initializer when you create a function, after the initializer function is configured, Function Compute loads the my_initializer function defined in the main.js file.

Features

  • Input parameters of an initializer function contain context and callback parameters. These parameters are defined the same as those in event functions. For more information, see The context parameter and The callback function.
  • In the context parameter, the initializer and initializationTimeout fields are designed specially for initializer functions. When you enable the initializer feature, these fields are set to the values that you specify when you create a function. Otherwise, these fields are empty and do not take effect.
  • Initializer functions do not return any value.