This topic was translated by AI and is currently in queue for revision by our editors. Alibaba Cloud does not guarantee the accuracy of AI-translated content. Request expedited revision

Function instance lifecycle hooks

Updated at: 2025-01-10 16:23

This topic explains how to implement lifecycle hooks for function instances within a custom runtime environment.

Callback methods

Once you have implemented and configured lifecycle hooks for function instances, Function Compute will call the appropriate hook when a corresponding instance lifecycle event takes place. You can configure the following lifecycle hooks for a function instance: Initializer, PreFreeze, and PreStop hooks. For more information, see function instance lifecycle hooks.

PathInput requestExpected response
PathInput requestExpected response
(Optional) POST /initialize

Request body: none.

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

Response body: the return value of the Initializer function.
StatusCode
  • 2xx: successful
  • Other codes: failed
The following is an example code of initialize in Python language:
@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"
The following is an example code of function execution error in Python language:
@app.route('/initialize', methods=['POST'])
def init():
    raise Exception("hahaha")
    return "OK", 200, []
@app.route('/initialize', methods=['POST'])
def init():
    return "OK", 404, []
(Optional) GET /pre-freezeResponse body: the return value of the PreFreeze hook.
StatusCode
  • 2xx: successful
  • Other codes: failed
(Optional) GET /pre-stopResponse body: the return value of the PreStop hook.
StatusCode
  • 2xx: successful
  • Other codes: failed
To utilize the Initializer hook method in a custom runtime, simply implement the required logic at the path /initialize with the POST method on your HTTP server. Refer to the example code of initialize in the table above for guidance.
Important
If the function you create does not specify an Initializer, there is no need to implement /initialize. Without setting an Initializer, the HTTP server's implementation of /initialize will not be called or executed./initialize

The PreFreeze and PreStop hook methods are implemented in the same way as the Initializer hook method.

Error codes

Error code IDDescription
Error code IDDescription
400
  • If the Initializer hook fails, the 400 or 404 error code is returned. In this case, Function Compute does not resend the request, but retries the Initializer hook until the Initializer hook is successful.
  • If the PreFreeze and PreStop hooks fail, the 400 or 404 error code is returned. This does not affect the freezing and stopping of the function instance.
404
500Function Compute restarts the instance.
  • On this page (1)
  • Callback methods
  • Error codes
Feedback