Skip to content

Latest commit

 

History

History
76 lines (49 loc) · 2.8 KB

File metadata and controls

76 lines (49 loc) · 2.8 KB

Here's the content with issues fixed and presented in a single Markdown file:

Basic Test Sample

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.

Default Customization

Basic customization is available via the corresponding .env file. For a detailed description of variables, refer here.

Calling a Stateful Function

  1. Start watching logs of the stateful function:
docker logs tests-runtime-1 --tail 100 -f
  1. Get into the nats-io container:
docker exec -it tests-io-1 sh
  1. Send a message:
nats pub --count=1 -s nats://nats:foliage@nats:4222 functions.tests.basic.master.id "{\"payload\":{\"foo\":\"bar\"}, \"options\":{\"increment\":10}}"

Advanced Customization

Creating Your Own Stateful Function

  1. In main.go, in the RegisterFunctionTypes function, 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.

  1. 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)
}
  1. 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)
  1. 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!!!