This topic describes how to implement lifecycle hooks for function instances in a custom container runtime.

Lifecycle hooks

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 hooks. For more information, see Function instance lifecycle.

PathRequestExpected response
(Optional) POST /initialize

Request body: none.

Request header: common request headers. For more information, see Common request headers in Function Compute.

Response body: the return value of the Initializer hook.
StatusCode
  • 200: The request succeeded.
  • 404: The request failed.
Sample code of initialize in Python:
@app.route('/initialize', methods=['POST'])
def init_invoke():
    rid = request.headers.get(x-fc-request-id)
    print("FC Initialize Start RequestId: " + rid)
    # do your things
    print("FC Initialize End RequestId: " + rid)
    return "OK"
(Optional) GET /pre-freeze Response body: the return value of the PreFreeze hook.
StatusCode
  • 200: The request succeeded.
  • 404: The request failed.
(Optional) GET /pre-stopResponse body: the return value of the PreStop function.
StatusCode
  • 200: The request succeeded.
  • 404: The request failed.
If you want to use the Initializer hook in a custom runtime, you need to only implement the logic that corresponds to the /initialize path and the POST method in your HTTP server. You can refer to the sample code for initialize in the preceding table.
Important If you do not configure the Initializer hook for the function, the /initialize path does not need to be implemented. Even if the /initialize path is implemented by the HTTP server, you cannot invoke or execute the /initialize logic in the code.

The PreFreeze and PreStop methods are used in the same manner as the Initializer method.