In a custom runtime, your code package is an HTTP server. Function Compute starts it using the Startup Command and Startup Parameter you configure, then routes all invocation requests to it.
How it works
When Function Compute performs a cold start for a custom runtime function, it runs the following sequence:
Extracts your code package (
function.zip).Starts your HTTP server using the Startup Command and Startup Parameter values.
Routes all function invocation requests to your HTTP server.
The HTTP server handles all requests from that point forward. If you do not configure a startup command and startup parameter, Function Compute starts the server from /code/bootstrap by default.
Configure the startup command
Set Startup Command and Startup Parameter in your function configuration using customRuntimeConfig:
customRuntimeConfig:
command:
- <executable> # Startup Command
args:
- <argument> # Startup ParameterFunction Compute concatenates command and args to form the complete startup command.
The following table summarizes the key values for common languages. All examples use a code package named function.zip.
| Language | File in package | command | args |
|---|---|---|---|
| Java 8 or Spring Boot | demo.jar | java | ['-jar', 'demo.jar'] |
| Python 3.7 | server.py | python | ['server.py'] |
| Node.js 10 | server.js | node | ['server.js'] |
| PHP 7.4 | server.php | php | ['server.php'] |
Examples by language
Java 8 or Spring Boot
.
└── demo.jarcustomRuntimeConfig:
command:
- java
args:
- '-jar'
- 'demo.jar'Python 3.7
.
└── server.pycustomRuntimeConfig:
command:
- python
args:
- 'server.py'Node.js 10
.
└── server.jscustomRuntimeConfig:
command:
- node
args:
- 'server.js'PHP 7.4
.
└── server.phpcustomRuntimeConfig:
command:
- php
args:
- 'server.php'HTTP server requirements
Your HTTP server must meet the following requirements before Function Compute can route requests to it.
Network binding
Bind your server to 0.0.0.0:CAPort or *:CAPort. Binding to 127.0.0.1:CAPort causes connection refused errors, and Function Compute returns a FunctionNotStarted error.
The default port is 9000. To use a different port, such as 8080, set the listening port in your function configuration and bind your server to the same port.
Connection settings
Enable keep-alive on all connections and set the timeout to 24 hours (the maximum function execution duration) or longer. The following example uses the Express framework for Node.js:
// Express framework example (Node.js)
var server = app.listen(PORT, HOST);
server.timeout = 0; // never timeout
server.keepAliveTimeout = 0; // keepalive, never timeoutStartup time
Your HTTP server must be ready to accept connections within 120 seconds of the cold start. If the server does not start in time, Function Compute returns a FunctionNotStarted error.
Troubleshooting
FunctionNotStarted
If you receive the following error, your HTTP server failed to start or is not reachable on the expected port:
{
"ErrorCode": "FunctionNotStarted",
"ErrorMessage": "The CA's http server cannot be started: ContainerStartDuration: 25000000000. Ping CA failed due to: dial tcp 21.0.XX.XX:9000: getsockopt: connection refused Logs: 2019-11-29T09:53:30.859837462Z Listening on port 9000"
}Common causes and fixes:
| Cause | Fix |
|---|---|
Server bound to 127.0.0.1 instead of 0.0.0.0 | Change the bind address to 0.0.0.0:CAPort or *:CAPort |
| Server port does not match the configured listening port | Make sure the port in your server code matches the port in your function configuration |
| Server did not start within 120 seconds | Check for slow initialization logic or startup errors in the logs |