This topic describes how to implement lifecycle hooks for function instances in the C# runtime environment.
Background information
After you configure a lifecycle hook for a function instance, Function Compute invokes the hook when a related lifecycle event for the instance occurs. The following lifecycle hooks can be configured for a function instance: Initializer, PreFreeze, and PreStop. For more information, see Function instance lifecycle.
The billing rules for the 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, Instance Logs, or Advanced Logs. The logs for lifecycle hooks are not displayed in Call Request List. For more information, see View the logs of instance lifecycle hooks..
Method signature
- Initializer hooks
An Initializer hook is executed after a function instance is started and before a handler runs. Function Compute ensures that an Initializer hook is successfully executed only once in the lifecycle of a function instance. If your Initializer hook fails to be executed, an error is returned for the function invocation. When you invoke the function next time, the system creates another function instance to execute the Initializer hook.
- PreFreeze hooks
A PreFreeze hook is executed before a function instance is frozen.
- PreStop hooks
A PreStop hook is executed before a function instance is destroyed.
public void FunctionName(IFcContext context);
public static void FunctionName(IFcContext context);
Example: StreamRequestHandler
using System;
using System.IO;
using System.Threading.Tasks;
using Aliyun.Serverless.Core;
using Microsoft.Extensions.Logging;
namespace Example
{
public class Hello
{
public void Initialize(IFcContext context)
{
IFcLogger logger = context.Logger;
logger.LogInformation("Initialize start");
logger.LogInformation("Handle initializer: {0}", context.RequestId);
logger.LogInformation("Initialize end");
}
public void PreStop(IFcContext context)
{
IFcLogger logger = context.Logger;
logger.LogInformation("PreStop start");
logger.LogInformation("Handle PreStop: {0}", context.RequestId);
logger.LogInformation("PreStop end");
}
public void PreFreeze(IFcContext context)
{
IFcLogger logger = context.Logger;
logger.LogInformation("PreFreeze start");
logger.LogInformation("Handle PreFreeze: {0}", context.RequestId);
logger.LogInformation("PreFreeze end");
}
public async Task<Stream> StreamHandler(Stream input, IFcContext context)
{
IFcLogger logger = context.Logger;
logger.LogInformation("Handle request: {0}", context.RequestId);
MemoryStream copy = new MemoryStream();
await input.CopyToAsync(copy);
copy.Seek(0, SeekOrigin.Begin);
return copy;
}
static void Main(string[] args){}
}
}
Configure lifecycle hooks
Use the Function Compute console
You can configure lifecycle hooks on the Function Details page in the Function Compute console. For more information, see Configure the PreFreeze hook and the PreStop hook in the Function Compute console. The format of a hook method is the same as the format of a request handler. The
format is Assembly name::Namespace.Class name::Method name
. For more information, see Handler.
- Initializer hooks:
HelloFcApp::Example.Hello::Initialize
- PreFreeze hooks:
HelloFcApp::Example.Hello::PreFreeze
- PreStop hooks:
HelloFcApp::Example.Hello::PreStop
Use Serverless Devs
- Configure the Initializer hook
Add the initializer and initializationTimeout fields to the function parameter.
- Configure the PreFreeze hook
Add the instanceLifecycleConfig.preFreeze field, including handler and timeout, to the function parameter.
- Configure the PreStop hook
Add the instanceLifecycleConfig.preStop field, including handler and timeout, to the function parameter.
edition: 1.0.0
name: hello-world # The name of the project.
access: default # The alias of the key.
vars: # The global variable.
region: cn-shanghai # The ID of the region.
service:
name: fc-example
description: 'fc example by serverless devs'
services:
helloworld: # The name of the service or module.
component: fc
actions: # The custom execution logic.
pre-deploy: # The operation before the deployment.
- run: mvn package # The command to run.
path: ./ # The path in which the command runs.
props: # The property value of the component.
region: ${vars.region}
service: ${vars.service}
function:
name: dotnet-lifecycle-hook-demo
description: 'fc example by serverless devs'
runtime: dotnetcore3.1
codeUri: ./target
handler: example.App::handleRequest
memorySize: 128
timeout: 60
initializer: HelloFcApp::Example.Hello::Initialize
initializationTimeout: 30
instanceLifecycleConfig:
preStop:
Handler: HelloFcApp::Example.Hello::PreStop
Timeout: 30
preFreeze:
Handler: HelloFcApp::Example.Hello::PreFreeze
Timeout: 30
For information about the YAML syntax of Serverless Devs, see Serverless Devs commands.
View the logs of instance lifecycle hooks.
You can view the logs for lifecycle hook in Function Logs.