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(); - } }