Skip to content

Commit

Permalink
fix(场景联动): 修复重启服务后,场景联动未初始化的问题 (#566)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyouji authored Sep 2, 2024
1 parent 412fbd6 commit 3ce520e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<RuleInstance> findAll() {
Expand Down Expand Up @@ -71,4 +75,17 @@ public Flux<RuleInstance> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
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;
import reactor.core.publisher.Mono;

@Service
@Slf4j
public class RuleInstanceService extends GenericReactiveCrudService<RuleInstanceEntity, String> implements CommandLineRunner {
public class RuleInstanceService extends GenericReactiveCrudService<RuleInstanceEntity, String> {

@Autowired
private RuleEngine ruleEngine;
Expand Down Expand Up @@ -78,18 +77,4 @@ public Mono<Integer> deleteById(Publisher<String> 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();
}
}

0 comments on commit 3ce520e

Please sign in to comment.