Options
# RuleContextOption
Configuration items for the execution of a rule engine instance. Usage: ruleEngine.OnMsg(msg, ...RuleContextOption), example:
ruleEngine.OnMsg(msg, types.WithOnEnd(func(ctx types.RuleContext, msg types.RuleMsg, err error) {
// do something
}), types.WWithOnRuleChainCompleted(func(ctx RuleContext, snapshot RuleChainRunSnapshot){
// do something
}))
2
3
4
5
The following options are available:
# WithOnEnd
Callback for when a rule chain branch execution ends. If the rule chain has multiple endpoints, the callback function will be executed multiple times
types.WithOnEnd(func(ctx types.RuleContext, msg types.RuleMsg, err error) {
}
2
3
# WithContext
Context
- Used for sharing data or semaphores between different component instances
- Used for timeout cancellation
types.WithContext(c context.Context)
# WithOnAllNodeCompleted
Callback function when all branches of the rule chain have completed execution.
- Called only once
types.WithOnAllNodeCompleted(func(){
})
2
3
# WithOnRuleChainCompleted
Callback function when the rule chain execution is completed, and collects the runtime logs of each node.
types.WithOnRuleChainCompleted(func(ctx RuleContext, snapshot RuleChainRunSnapshot){
})
2
3
- RuleChainRunSnapshot:
| Field | Type | Description | Default Value |
|---|---|---|---|
| RuleChain | RuleChain | Rule chain snapshot: Reference | None |
| id | string | Message ID | None |
| startTs | int64 | Rule chain start execution time | None |
| endTs | int64 | Rule chain end execution time | None |
| logs | []RuleNodeRunLog | Execution log of each node | None |
| additionalInfo | object | Additional information | None |
- RuleNodeRunLog:
| Field | Type | Description | Default Value |
|---|---|---|---|
| nodeId | string | Node ID | None |
| inMsg | RuleMsg | Input message: Reference | None |
| outMsg | RuleMsg | Output message: Reference | None |
| relationType | string | Connection type with the next node | None |
| err | string | Error information | None |
| logItems | []string | Other logs during execution | None |
| startTs | int64 | Rule chain start execution time | None |
| endTs | int64 | Rule chain end execution time | None |
# WithOnNodeCompleted
Callback function when a node is completed, and collects the node's runtime log.
types.WithOnNodeCompleted(func(ctx RuleContext, nodeRunLog RuleNodeRunLog){
})
2
3
# WithOnNodeDebug
Node debug log callback function.
Same as config.OnDebug. It is called in real-time asynchronously and will only trigger if the node or rule chain is configured with debugMode enabled
types.WithOnNodeDebug(func(ruleChainId string, flowType string, nodeId string, msg RuleMsg, relationType string, err error)){
})
2
3
TIP
WithOnNodeDebug callback function contains custom logic that is triggered asynchronously, and the execution order cannot be guaranteed. It may be executed after WithOnAllNodeCompleted and WithOnRuleChainCompleted.
# WithStartNode
v0.22.0+ Set the first or multiple start execution nodes of the rule chain. If not set, it defaults to starting execution from the Root node of the rule chain.types.WithStartNode(nodeIds ...string)
# WithTellNext
v0.22.0+ Set to search for the next or multiple execution nodes by specifying the node ID. Used to restore the execution link of the rule chain.types.WithTellNext(fromNodeId string, relationTypes ...string)
# WithRestoreNodes
v0.35.0+ Sets the list of nodes to restore execution. Compared to `WithStartNode` and `WithTellNext`, this method provides a more flexible way to restore, supporting independent messages and relationships for each restored node.// Restore nodes using the default message
ruleEngine.OnMsg(msg, types.WithRestoreNodes(
types.ExecuteNode("nodeId1"),
types.ExecuteNode("nodeId2"),
))
// Restore nodes using custom messages
ruleEngine.OnMsg(defaultMsg, types.WithRestoreNodes(
types.ExecuteNodeWithMsg("nodeId1", msg1),
types.ExecuteNodeWithMsg("nodeId2", msg2),
))
// Restore and find child nodes to execute
ruleEngine.OnMsg(msg, types.WithRestoreNodes(
types.ExecuteNext("nodeId1", "True", "False"),
))
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16