Other
# About Rule Engine Execution Interruption and Recovery
There are scenarios where the rule engine execution instance needs to be interrupted and then resumed, such as:
- Workflow Approval Scenario: In workflows that require manual approval, the execution of the rule engine may need to be paused until the necessary approval is obtained.
- Third-Party System Asynchronous Callback: In scenarios that depend on the response of external systems, the rule engine may need to wait for the callback from the third-party system to continue execution.
- Dependency Service Unavailable: When the execution of the rule engine depends on external services (such as databases, message queues, etc.), and these services are temporarily unavailable, the execution of the rule engine will be interrupted.
The above requirements for the rule engine interruption and recovery scenario, without introducing a workflow, RuleGo
is a good lightweight solution. RuleGo
provides stateless interfaces,
supporting the interruption and recovery of the rule engine execution, that is, when the rule engine execution is interrupted, it can be resumed to the state before the interruption.
Here are the specific steps:
- Control the interruption of this execution instance through the execution context
ctx.DoOnEnd
, both the custom node message handling function and the custom function have this context.
func OnMsg(ctx types.RuleContext, msg types.RuleMsg) {
// Processing logic
// Interrupt this execution instance, do not execute subsequent nodes, and notify the rule engine `OnEnd` callback function
ctx.DoOnEnd(msg, err, relationType)
}
1
2
3
4
5
2
3
4
5
- Register the rule engine execution end callback function to persist msg and related state (if necessary): WithOnEnd.
ruleEngine.OnMsg(msg, types.WithOnEnd(func(ctx types.RuleContext, msg types.RuleMsg, err error, relationType string) {
// Persist the data of the current interrupted node
// ctx.GetSelfId() The ID of the current node, used for recovery
// msg.Id The ID of the message for this execution instance
// msg.Data The data of the message for this execution instance
// msg.Type The type of the message for this execution instance
// msg.Metadata The metadata of the message for this execution instance
// err.Error() The error information of this execution instance
// relationType The relationship used to find the next node
}))
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
- Resume execution by providing the msg saved at the last interruption, the ID of the interrupted node, and specify the
relationType
to query the next node to resume execution.
ruleEngine.OnMsg(msg, types.WithTellNext(fromNodeId string, relationTypes ...string))
1
Edit this page on GitHub (opens new window)
Last Updated: 2024/10/23, 10:13:01