-
Notifications
You must be signed in to change notification settings - Fork 0
<fix>[core]: improve SimpleFlowChain #3548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: zsv_5.0.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,12 @@ | ||
| package org.zstack.header.core.workflow; | ||
|
|
||
| import org.zstack.utils.DebugUtils; | ||
| import org.zstack.utils.FieldUtils; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.function.BiConsumer; | ||
| import java.util.function.Consumer; | ||
| import java.util.function.Predicate; | ||
|
|
||
| public interface Flow { | ||
| void run(FlowTrigger trigger, Map data); | ||
|
|
@@ -23,4 +27,101 @@ default String name() { | |
| } | ||
| return String.format("%s", this.getClass().getSimpleName()); | ||
| } | ||
|
|
||
| @SuppressWarnings("rawtypes") | ||
| public static class FlowBuilder { | ||
| public final String flowName; | ||
| private Predicate<Map> skipPredicate; | ||
| private BiConsumer<FlowTrigger, Map> triggerConsumer; | ||
| private BiConsumer<FlowRollback, Map> rollbackConsumer; | ||
|
|
||
| private FlowBuilder(String flowName) { | ||
| DebugUtils.Assert(flowName != null, "flowName should not be null"); | ||
| this.flowName = flowName; | ||
| } | ||
|
|
||
| public FlowBuilder withSkipPredicate(Predicate<Map> predicate) { | ||
| DebugUtils.Assert(predicate != null, "skipPredicate of FlowBuilder should not be null"); | ||
| this.skipPredicate = predicate; | ||
| return this; | ||
| } | ||
|
|
||
| public FlowBuilder skipIf(Predicate<Map> predicate) { | ||
| return withSkipPredicate(predicate); | ||
| } | ||
|
|
||
| public FlowBuilder runIf(Predicate<Map> predicate) { | ||
| DebugUtils.Assert(predicate != null, "predicate of FlowBuilder.runIf() should not be null"); | ||
| return withSkipPredicate(predicate.negate()); | ||
| } | ||
|
Comment on lines
+49
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
当前 Also applies to: 70-109 🤖 Prompt for AI Agents |
||
|
|
||
| public FlowBuilder handle(BiConsumer<FlowTrigger, Map> consumer) { | ||
| DebugUtils.Assert(consumer != null, "consumer of FlowBuilder.handle() should not be null"); | ||
| this.triggerConsumer = consumer; | ||
| return this; | ||
| } | ||
|
|
||
| public FlowBuilder handle(Consumer<FlowTrigger> consumer) { | ||
| DebugUtils.Assert(consumer != null, "consumer of FlowBuilder.handle() should not be null"); | ||
| this.triggerConsumer = (trigger, data) -> consumer.accept(trigger); | ||
| return this; | ||
| } | ||
|
|
||
| public FlowBuilder rollback(BiConsumer<FlowRollback, Map> consumer) { | ||
| DebugUtils.Assert(consumer != null, "consumer of FlowBuilder.rollback() should not be null"); | ||
| this.rollbackConsumer = consumer; | ||
| return this; | ||
| } | ||
|
|
||
| public FlowBuilder rollback(Consumer<FlowRollback> consumer) { | ||
| DebugUtils.Assert(consumer != null, "consumer of FlowBuilder.rollback() should not be null"); | ||
| this.rollbackConsumer = (trigger, data) -> consumer.accept(trigger); | ||
| return this; | ||
| } | ||
|
|
||
| public Flow build() { | ||
| DebugUtils.Assert(triggerConsumer != null, "handle() must be called before build()"); | ||
| Predicate<Map> skipPredicateSnapshot = skipPredicate; | ||
| BiConsumer<FlowTrigger, Map> triggerConsumerSnapshot = triggerConsumer; | ||
| BiConsumer<FlowRollback, Map> rollbackConsumerSnapshot = rollbackConsumer; | ||
|
|
||
| return new Flow() { | ||
| @Override | ||
| public boolean skip(Map data) { | ||
| if (skipPredicateSnapshot == null) { | ||
| return false; | ||
| } | ||
| return skipPredicateSnapshot.test(data); | ||
| } | ||
|
|
||
| @Override | ||
| public void run(FlowTrigger trigger, Map data) { | ||
| triggerConsumerSnapshot.accept(trigger, data); | ||
| } | ||
|
|
||
| @Override | ||
| public void rollback(FlowRollback trigger, Map data) { | ||
| if (rollbackConsumerSnapshot == null) { | ||
| trigger.rollback(); | ||
| } else { | ||
| rollbackConsumerSnapshot.accept(trigger, data); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String name() { | ||
| return flowName; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return name(); | ||
| } | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| public static FlowBuilder of(String flowName) { | ||
| return new FlowBuilder(flowName); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
propagateExceptionTo()目前只对新便捷重载生效,旧 handler 入口会静默失效这里收集到的
asyncBackups只被接到了error(Consumer<ErrorCode>)和done(Runnable)上;如果调用方继续使用现有的error(FlowErrorHandler)、done(FlowDoneHandler)或setFlow*Handler(...),链级别的propagateExceptionTo()配置不会生效,而且没有任何提示。建议要么统一在旧入口里做包装,要么在启用该配置后显式拒绝旧重载,避免链级配置静默无效。Also applies to: 321-352
🤖 Prompt for AI Agents