Here's the content with issues fixed and presented in a single Markdown file:
The basic test sample provides a single stateful function called "functions.tests.basic.master". The documentation about the basic test sample can be found here.
Basic customization is available via the corresponding .env file. For a detailed description of variables, refer here.
- Start watching logs of the stateful function:
docker logs tests-runtime-1 --tail 100 -f- Get into the nats-io container:
docker exec -it tests-io-1 sh- Send a message:
nats pub --count=1 -s nats://nats:foliage@nats:4222 functions.tests.basic.master.id "{\"payload\":{\"foo\":\"bar\"}, \"options\":{\"increment\":10}}"- In
main.go, in theRegisterFunctionTypesfunction, add the following code to register a new typename function:
// Create a new typename function "functions.tests.basic.custom," each stateful instance of which uses the Go function "MasterFunction."
ft := statefun.NewFunctionType(runtime, "functions.tests.basic.custom", MasterFunction, statefun.NewFunctionTypeConfig())
// Add TypenameExecutorPlugin, which will provide StatefunExecutor for each stateful instance for this typename function (skip this if TypenameExecutorPlugin is not needed).
if content, err := os.ReadFile("./custom_function_plugin.js"); err == nil {
// Assign JavaScript StatefunExecutor for TypenameExecutorPlugin.
ft.SetExecutor(jsFileName, string(content), sfPluginJS.StatefunExecutorPluginJSConstructor)
}Don't forget to create custom_function_plugin.js if you plan to use the StatefunExecutorPlugin.
- Create a Go function "CustomFunction" to be used by your typename function. It should correspond to the following type:
type FunctionLogicHandler func(sfPlugins.StatefunExecutor, *sfPlugins.StatefunContextProcessor)For example:
func CustomFunction(executor sfPlugins.StatefunExecutor, ctx *sfPlugins.StatefunContextProcessor) {
ctx.Call("functions.tests.basic.master", ctx.Self.ID, ctx.Payload)
}- Making "functions.tests.basic.custom" Call "functions.tests.basic.master"
At the end of the body of "CustomFunction," add the following code:
ctx.Call("functions.tests.basic.master", ctx.Self.ID, ctx.Payload, ctx.Options)- Working with Context Within the Function:
Use the ctx variable to call the needed methods.
Do not forget to remove NATS stream from NATS server if a new function typename was added!!!