[ server ] [ executor ] - polish code -1

This commit is contained in:
IceDerce
2022-12-14 20:17:37 +08:00
parent d0bdca5ced
commit 3934bf17f8
12 changed files with 223 additions and 83 deletions

View File

@@ -0,0 +1,52 @@
package io.wdd.rpc.execute.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.Subscription;
import org.springframework.data.redis.connection.stream.Consumer;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.connection.stream.ReadOffset;
import org.springframework.data.redis.connection.stream.StreamOffset;
import org.springframework.data.redis.stream.StreamListener;
import org.springframework.data.redis.stream.StreamMessageListenerContainer;
import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.Duration;
@Configuration
public class RedisStreamReaderConfig {
public String streamKey;
@Resource
private StreamListener<String, MapRecord<String, String, String>> streamListener;
@Bean
public org.springframework.data.redis.stream.Subscription subscription(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
streamKey = "manual-command";
StreamMessageListenerContainer.StreamMessageListenerContainerOptions<String, MapRecord<String, String, String>> options = StreamMessageListenerContainer.StreamMessageListenerContainerOptions.builder().pollTimeout(Duration.ofSeconds(1)).build();
StreamMessageListenerContainer<String, MapRecord<String, String, String>> listenerContainer = StreamMessageListenerContainer.create(redisConnectionFactory, options);
org.springframework.data.redis.stream.Subscription subscription = listenerContainer.receive(
Consumer.from(streamKey, InetAddress.getLocalHost().getHostName()),
StreamOffset.create(streamKey, ReadOffset.lastConsumed()),
streamListener);
listenerContainer.start();
return subscription;
}
}

View File

@@ -0,0 +1,40 @@
package io.wdd.rpc.execute.result;
import io.wdd.rpc.execute.config.RedisStreamReaderConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.stream.StreamListener;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
@Slf4j
public class CommandResultReader implements StreamListener<String, MapRecord<String,String, String >> {
@Resource
RedisStreamReaderConfig redisStreamReaderConfig;
@Override
public void onMessage(MapRecord<String, String, String> message) {
String commandLog = message.getValue().values().iterator().next();
System.out.println("commandLog = " + commandLog);
log.info("intend to be handled already !");
}
public void readFromStreamKey(String streamKey) {
String formerKey = redisStreamReaderConfig.streamKey;
log.info("start to change StreamReader streamKey from {} to ==> {}",formerKey, streamKey);
redisStreamReaderConfig.streamKey = streamKey;
}
}

View File

@@ -1,23 +1,20 @@
package io.wdd.rpc.execute.service;
import org.springframework.stereotype.Service;
import java.util.List;
public interface CoreExecutionService {
void SendCommandToAgent(String topicName, String command);
String SendCommandToAgent(String topicName, String command);
void SendCommandToAgent(String topicName, List<String> commandList);
String SendCommandToAgent(String topicName, List<String> commandList);
void SendCommandToAgent(String topicName, String type, List<String> command);
String SendCommandToAgent(String topicName, String type, List<String> command);
void SendCommandToAgent(List<String> topicNameList, String type, String command);
void SendCommandToAgent(List<String> topicNameList, String type, List<String> command);
List<String> SendCommandToAgent(List<String> topicNameList, String type, List<String> command);
}

View File

@@ -10,6 +10,7 @@ import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class CoreExecutionServiceImpl implements CoreExecutionService {
@@ -19,29 +20,35 @@ public class CoreExecutionServiceImpl implements CoreExecutionService {
@Override
public void SendCommandToAgent(String topicName, String command) {
this.SendCommandToAgent(topicName, List.of(command));
public String SendCommandToAgent(String topicName, String command) {
return this.SendCommandToAgent(topicName, List.of(command));
}
@Override
public void SendCommandToAgent(String topicName, List<String> commandList) {
this.SendCommandToAgent(topicName,"manual-command", commandList);
public String SendCommandToAgent(String topicName, List<String> commandList) {
return this.SendCommandToAgent(topicName,"manual-command", commandList);
}
@Override
public void SendCommandToAgent(String topicName, String type, List<String> commandList) {
public String SendCommandToAgent(String topicName, String type, List<String> commandList) {
OctopusMessage octopusMessage = this.generateOctopusMessage(topicName, type, commandList);
messageSender.send(octopusMessage);
ExecutionMessage content = (ExecutionMessage) octopusMessage.getContent();
return content.getResultKey();
}
@Override
public void SendCommandToAgent(List<String> topicNameList, String type, String command) {
}
@Override
public void SendCommandToAgent(List<String> topicNameList, String type, List<String> command) {
public List<String> SendCommandToAgent(List<String> topicNameList, String type, List<String> command) {
return topicNameList.stream().map(
topicName -> {
return this.SendCommandToAgent(topicName, type, command);
}
).collect(Collectors.toList());
}
private OctopusMessage generateOctopusMessage(String topicName, String type, List<String> commandList){
@@ -73,7 +80,6 @@ public class CoreExecutionServiceImpl implements CoreExecutionService {
String TimeString = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
return topicName + TimeString;
}

View File

@@ -0,0 +1,44 @@
package io.wdd.rpc.execute.web;
import io.wdd.common.beans.response.R;
import io.wdd.rpc.execute.service.CoreExecutionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Nullable;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("octopus/server/executor")
public class ExecutionController {
@Resource
CoreExecutionService coreExecutionService;
@PostMapping("command")
public R<String> patchCommandToAgent(
@RequestParam(value = "topicName") String topicName,
@RequestParam(value = "commandList", required = false) @Nullable List<String> commandList,
@RequestParam(value = "type", required = false) @Nullable String type
) {
String streamKey = "";
if (StringUtils.isEmpty(type)) {
streamKey = coreExecutionService.SendCommandToAgent(topicName, commandList);
} else {
streamKey = coreExecutionService.SendCommandToAgent(topicName, type,commandList);
}
return R.ok(streamKey);
}
}

View File

@@ -2,11 +2,11 @@ spring:
application:
name: octopus-server
profiles:
active: local
active: k3s
cloud:
nacos:
config:
group: local
group: k3s
config-retry-time: 3000
file-extension: yaml
max-retry: 3
@@ -16,5 +16,5 @@ spring:
timeout: 5000
config-long-poll-timeout: 5000
extension-configs:
- group: local
data-id: common-local.yaml
- group: k3s
data-id: common-k3s.yaml