From 3ce520ebe6f1e4e5a5f381519ee5554ec0a54903 Mon Sep 17 00:00:00 2001 From: Zhang Ji <125540670@qq.com> Date: Mon, 2 Sep 2024 12:23:46 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E5=9C=BA=E6=99=AF=E8=81=94=E5=8A=A8):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=87=8D=E5=90=AF=E6=9C=8D=E5=8A=A1=E5=90=8E?= =?UTF-8?q?=EF=BC=8C=E5=9C=BA=E6=99=AF=E8=81=94=E5=8A=A8=E6=9C=AA=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E7=9A=84=E9=97=AE=E9=A2=98=20(#566)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/LocalRuleInstanceRepository.java | 19 ++++++++++++++++++- .../engine/service/RuleInstanceService.java | 17 +---------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/jetlinks-manager/rule-engine-manager/src/main/java/org/jetlinks/community/rule/engine/service/LocalRuleInstanceRepository.java b/jetlinks-manager/rule-engine-manager/src/main/java/org/jetlinks/community/rule/engine/service/LocalRuleInstanceRepository.java index 838e372e8..548ef82ab 100644 --- a/jetlinks-manager/rule-engine-manager/src/main/java/org/jetlinks/community/rule/engine/service/LocalRuleInstanceRepository.java +++ b/jetlinks-manager/rule-engine-manager/src/main/java/org/jetlinks/community/rule/engine/service/LocalRuleInstanceRepository.java @@ -5,9 +5,11 @@ import org.jetlinks.community.rule.engine.entity.RuleInstanceEntity; import org.jetlinks.community.rule.engine.entity.SceneEntity; import org.jetlinks.community.rule.engine.enums.RuleInstanceState; +import org.jetlinks.rule.engine.api.RuleEngine; import org.jetlinks.rule.engine.api.model.RuleEngineModelParser; import org.jetlinks.rule.engine.cluster.RuleInstance; import org.jetlinks.rule.engine.cluster.RuleInstanceRepository; +import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -17,13 +19,15 @@ @Component @AllArgsConstructor @Slf4j -public class LocalRuleInstanceRepository implements RuleInstanceRepository { +public class LocalRuleInstanceRepository implements RuleInstanceRepository, CommandLineRunner { private final RuleInstanceService instanceService; private final SceneService sceneService; private final RuleEngineModelParser parser; + private final RuleEngine ruleEngine; + @Nonnull @Override public Flux findAll() { @@ -71,4 +75,17 @@ public Flux findById(String id) { ) ; } + + @Override + public void run(String... args) throws Exception { + this + .findAll() + .flatMap(ruleInstance -> ruleEngine + .startRule(ruleInstance.getId(), ruleInstance.getModel()) + .onErrorResume(err -> { + log.warn("启动规则[{}]失败: {}", ruleInstance.getModel().getName(), ruleInstance); + return Mono.empty(); + })) + .subscribe(); + } } diff --git a/jetlinks-manager/rule-engine-manager/src/main/java/org/jetlinks/community/rule/engine/service/RuleInstanceService.java b/jetlinks-manager/rule-engine-manager/src/main/java/org/jetlinks/community/rule/engine/service/RuleInstanceService.java index 26723f365..9af527541 100644 --- a/jetlinks-manager/rule-engine-manager/src/main/java/org/jetlinks/community/rule/engine/service/RuleInstanceService.java +++ b/jetlinks-manager/rule-engine-manager/src/main/java/org/jetlinks/community/rule/engine/service/RuleInstanceService.java @@ -15,7 +15,6 @@ import org.jetlinks.rule.engine.api.model.RuleModel; import org.reactivestreams.Publisher; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import reactor.core.publisher.Flux; @@ -23,7 +22,7 @@ @Service @Slf4j -public class RuleInstanceService extends GenericReactiveCrudService implements CommandLineRunner { +public class RuleInstanceService extends GenericReactiveCrudService { @Autowired private RuleEngine ruleEngine; @@ -78,18 +77,4 @@ public Mono deleteById(Publisher idPublisher) { .as(super::deleteById); } - @Override - public void run(String... args) { - createQuery() - .where() - .is(RuleInstanceEntity::getState, RuleInstanceState.started) - .fetch() - .flatMap(e -> this - .doStart(e) - .onErrorResume(err -> { - log.warn("启动规则[{}]失败", e.getName(), e); - return Mono.empty(); - })) - .subscribe(); - } }