All Products
Search
Document Center

Function Compute:Lifecycle hooks for function instances

Last Updated:Feb 06, 2024

This topic describes how to implement lifecycle hooks for function instances in the Go runtime environment.

Background information

After you configure a lifecycle hook for a function instance, Function Compute invokes the hook when the related instance lifecycle event occurs. You can configure the Initializer and PreStop hooks for a function instance. For more information, see Function instance lifecycles.

The billing rules for the lifecycle hooks of function instances are the same as the billing rules for common invocation requests. However, the run logs of lifecycle hooks can be queried only on the Function Logs, Instance Logs, and SLS Logs tabs of the function details page in the Function Compute console. The logs of lifecycle hooks are not displayed on the Request List tab. For more information, see the View the logs of instance lifecycle hooks section of this topic.

Important

To use the PreStop hook, update the version of fc-runtime-go-sdk to V0.1.0 or later.

Method signature

  • The 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.

  • The PreStop hook is invoked before a function instance is destroyed.

The method signature of the Initializer hook is the same as that of the PreStop hook. Both hooks have only the Context input parameter, and no value is returned. Syntax:

function(ctx context.Context)

Implement lifecycle hooks

You must implement lifecycle hooks in the code and use the corresponding methods for registration. The following sample code provides examples on how to register lifecycle hooks:

// Register the Initializer hook.
fc.RegisterInitializerFunction(initialize)
// Register the PreStop hook.
fc.RegisterPreStopFunction(preStop)

Sample program:

package main

import (
    "context"
    "log"

    "github.com/aliyun/fc-runtime-go-sdk/fc"
    "github.com/aliyun/fc-runtime-go-sdk/fccontext"
)

func HandleRequest(ctx context.Context) (string, error) {
    return "hello world!", nil
}

func preStop(ctx context.Context) {
    log.Print("this is preStop handler")
    fctx, _ := fccontext.FromContext(ctx)
    fctx.GetLogger().Infof("context: %#v\n", fctx)
}

func initialize(ctx context.Context) {
    log.Print("this is initialize handler")
    fctx, _ := fccontext.FromContext(ctx)
    fctx.GetLogger().Infof("context: %#v\n", fctx)
}

func main() {
    fc.RegisterInitializerFunction(initialize)
    fc.RegisterPreStopFunction(preStop)
    fc.Start(HandleRequest)
}                

Description:

  • func initialize(ctx context.Context): the Initializer hook. The ctx context.Context parameter specifies the context information to be used when the hook is invoked. For more information, see Context.

  • func preStop(ctx context.Context): the PreStop hook. The ctx context.Context parameter specifies the context information to be used when the hook is invoked. For more information, see Context.

  • func main(): the entry point for running function code in FC. A Go program must contain the main method. You can add fc.Start(HandleRequest) to your code to specify the method used to run the handler, and add fc.RegisterInitializerFunction(initialize) to register the Initializer hook. Then, register the PreStop hook in the similar way.

    Important

    A lifecycle hook must be registered before fc.Start(HandleRequest) or fc.StartHttp(HandleRequest) is run. Otherwise, the registration fails.

Configure lifecycle hooks

Use the Function Compute console

You can configure the Initializer hook and PreStop hook on the FC tab of a function in the Function Compute console. The following figure shows an example.

db-go-lifecycle

For more information, see Function instance lifecycles.

View the logs of instance lifecycle hooks

You can view the logs for lifecycle hook in Logs.

  1. Log on to the Function Compute console. In the left-side navigation pane, click Functions.

  2. In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.

  3. On the function details page, click the Test Function tab, click Test Function, and then choose Logs > Function Logs.

    On the Logs tab, you can view function invocation logs and Initializer logs. Example:

    2023-09-06 11:18:10FC Initialize Start RequestId: 1-64f7ef72-64caf1ff0046194d9a26bbd7
    2023-09-06 11:18:10load code for handler:index.initialize
    2023-09-06 11:18:102023-09-06 11:18:10 1-64f7ef72-64caf1ff0046194d9a26bbd7 [verbose] initializer
    2023-09-06 11:18:10FC Initialize End RequestId: 1-64f7ef72-64caf1ff0046194d9a26bbd7
    2023-09-06 11:18:10FC Invoke Start RequestId: 1-64f7ef72-64caf1ff0046194d9a26bbd7
    2023-09-06 11:18:10load code for handler:index.handler
    2023-09-06 11:18:10FC Invoke End RequestId: 1-64f7ef72-64caf1ff0046194d9a26bbd7

    Each function instance is cached for a period of time and not destroyed immediately, you cannot view the logs for PreStop hooks right away. To quickly trigger the 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:

    2023-09-06 11:08:10FC PreStop Start RequestId: 944bca62-b209-47a1-9e48-2723647bce0a
    2023-09-06 11:08:10load code for handler:index.preStop
    2023-09-06 11:08:102023-09-06 11:08:10 944bca62-b209-47a1-9e48-2723647bce0a [verbose] preStop
    2023-09-06 11:08:10FC PreStop End RequestId: 944bca62-b209-47a1-9e48-2723647bce0a

Sample program

Function Compute provides a sample program for invoking the Initializer hook. The sample program provides an example on how to use the Initializer hook in the Go runtime environment to initialize a MySQL connection pool. In the sample program, a MySQL database is configured by using the environment variables of a function. For more information, see the s.yaml file. The Initializer hook is used to obtain the configurations of the MySQL database from environment variables, create a MySQL connection pool, and then test the connectivity.

For more information, visit the go-initializer-mysql page on GitHub.