Custom Code
This microservice is capable of running custom user functions (NodeJS or Python).
Installation prerequisites¶
Here is a few thing that should be considered:
- The service must have access to a npm registry. You can use the
NPM_CONFIG_REGISTRY
environment variable to specify your own. - The service needs a volume, we recommend using volumes with a high I/O speed as it might be highly solicited when installing dependencies for example.
Environment variables¶
Name | Description | Default value |
PYTHON_FUNCTIONS_RUN_TIMEOUT | Python Functions execution timeout (ms) | 20000 |
PYTHON_API_URL | Python API url | http://localhost:8000 |
PORT | HTTP port | 4000 |
FUNCTIONS_STORAGE_FILESYSTEM_DIRPATH | Functions directory path | data/functions/ |
FUNCTIONS_RUN_TIMEOUT | Functions execution timeout (ms) | 20000 |
FUNCTIONS_WORKERS_MAX_LRU | Maximum number of function workers kept in memory | 500 |
NODE_BUILTIN_MODULES | Allowed builtin modules | http, https, url, util, zlib, dns, stream, buffer, crypto |
NPM_CONFIG_REGISTRY | NPM registry url | https://registry.npmjs.org/ |
NODE_WORKER_MAX_OLD_GENERATION_SIZE_MB | NodeJS function worker maxOldGenerationSizeMb | 100MB |
INACTIVE_NODE_WORKER_DELETION_TIMEOUT | Inactive period in seconds after which node workers are automatically terminated | 3600 |
UPDATE_SCRIPTS_ON_STARTUP | If set to yes or true, makes sure function scripts are in-sync with corresponding workspace.yaml files on start-up. Useful to migrate functions after an upgrade with breaking changes. Please care that in case of a multi-instances deployment, every instance will run this migration. | no |
REQUEST_MAX_SIZE | Maximum request body size (format from bodyParser.json) | 1mb |
Microservice testing¶
-
Create a function "workspace" (named
test
and containing ahello
function):curl --location 'http://localhost:4000/v2/functions/test' \ --header 'Content-Type: application/json' \ --data '{ "functions": { "hello": { "code": "return \"Hello \" + name;", "parameters": { "name": { "type": "string" } } } } }'
If successful, the same body should be returned in response.
-
Run the function:
curl --location 'http://localhost:4000/v2/functions/test/run/hello' \ --header 'Content-Type: application/json' \ --data '{ "parameters": { "name": "world" } }'
If successful, the answer should be similar to this:
{ "result": "Hello world", "logs": [], "duration": 38 }
Congratulations, you service is up and running!
Features¶
Async functions¶
Even if they do not use any async call, every functions are always annotated as "async".
As such, calling a function from the same workspace must be done with await keyword. See Shared execution context for a full example.
Shared execution context¶
Functions from the same workspace are executed within a generated JS file gathering these functions together. This allows functions to call each others :
funcA :
return "world";
funcB :
return "hello " + (await funcA());
Shared memory¶
Functions from the same workspace are executed within the same worker NodeJS and VM2 instance. Although their inner variables are automatically destroyed after each execution, a special cache variable is provided to share memory between functions and accross executions :
Example:
funcA :
let counter = cache['counter']
counter = (counter || 0) + 1
cache['counter'] = counter;
return counter;
funcB :
let counter = cache['counter'];
return "Counter : " + counter;