add python script controller

This commit is contained in:
JiyangTang 2023-10-12 14:16:15 +08:00
parent e64cb542a2
commit d013ff024b
6 changed files with 117 additions and 11 deletions

View File

@ -0,0 +1,31 @@
package top.baogutang.admin.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.baogutang.admin.services.IScriptService;
import top.baogutang.common.domain.Results;
import javax.annotation.Resource;
/**
* @description: 执行脚本的controller
* @author: nikooh
* @date: 2023/10/12 : 13:43
*/
@Slf4j
@RestController
@RequestMapping("/api/v1/script")
public class ScriptController {
@Resource
private IScriptService scriptService;
@GetMapping("/python")
public Results<Void> execPython(@RequestParam(name = "scriptPath", required = false) String scriptPath) {
scriptService.execPython(scriptPath);
return Results.ok();
}
}

View File

@ -77,12 +77,15 @@ public class NoticeSchedule {
String cacheKey = String.format(MSG_PUSH_PREFIX_KEY, "edge_x", announcementsDto.getId());
Boolean result = redisTemplate.opsForValue().setIfAbsent(cacheKey, 1, 5, TimeUnit.DAYS);
if (Boolean.TRUE.equals(result)) {
if (!Boolean.TRUE.equals(result)) {
log.info(">>>>>>>>>>notice :{} has already pushed<<<<<<<<<<", announcementsDto.getId());
return;
}
String content = "# EDGE-X-" + announcementsDto.getTitle() + "\n\n![](" + announcementsDto.getCover() + ")\n\n> 点击下方链接查看更多详情:\n\n[查看详情](" + "https://app.edge-x.cn/#/noticeDetail?noticeId=" + announcementsDto.getId() + ")";
dingTalkMsgPushUtils.robotMarkdownMsgPush(announcementsDto.getTitle(), content);
// wxMsgPushService.msgPush(Message.CONTENT_TYPE_HTML, announcementsDto.getTitle(), announcementsDto.getContent(), wxMsgPushProperties.getTopicIds());
noticeLogService.saveNotice(NoticeTypeEnum.EDGE_X, announcementsDto.getId(), announcementsDto.getType(), announcementsDto.getTitle(), announcementsDto.getCover(), "https://app.edge-x.cn/#/noticeDetail?noticeId=" + announcementsDto.getId(), announcementsDto.getCreator(), announcementsDto.getCreatedAt());
}
}
/**
@ -107,12 +110,13 @@ public class NoticeSchedule {
String cacheKey = String.format(MSG_PUSH_PREFIX_KEY, "edge", announcementsDto.getId());
Boolean result = redisTemplate.opsForValue().setIfAbsent(cacheKey, 1, 5, TimeUnit.DAYS);
if (Boolean.TRUE.equals(result)) {
if (!Boolean.TRUE.equals(result)) {
log.info(">>>>>>>>>>notice :{} has already pushed<<<<<<<<<<", announcementsDto.getId());
return;
}
String content = "# EDGE-" + announcementsDto.getTitle() + "\n\n![](" + announcementsDto.getCover() + ")\n\n> 点击下方链接查看更多详情:\n\n[查看详情](" + "https://activities-h5.heishiapp.com/#/noticeDetail?noticeId=" + announcementsDto.getId() + ")";
dingTalkMsgPushUtils.robotMarkdownMsgPush(announcementsDto.getTitle(), content);
// wxMsgPushService.msgPush(Message.CONTENT_TYPE_HTML, announcementsDto.getTitle(), announcementsDto.getContent(), wxMsgPushProperties.getTopicIds());
noticeLogService.saveNotice(NoticeTypeEnum.EDGE, announcementsDto.getId(), announcementsDto.getType(), announcementsDto.getTitle(), announcementsDto.getCover(), "https://app.edge-x.cn/#/noticeDetail?noticeId=" + announcementsDto.getId(), announcementsDto.getCreator(), announcementsDto.getCreatedAt());
}
}
}

View File

@ -0,0 +1,16 @@
package top.baogutang.admin.services;
/**
* @description:
* @author: nikooh
* @date: 2023/10/12 : 13:47
*/
public interface IScriptService {
/**
* 执行指定的python脚本
*
* @param scriptPath 脚本位置
*/
void execPython(String scriptPath);
}

View File

@ -0,0 +1,53 @@
package top.baogutang.admin.services.impl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import top.baogutang.admin.services.IScriptService;
import top.baogutang.common.constants.ErrorCodeEnum;
import top.baogutang.common.exceptions.BusinessException;
import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @description:
* @author: nikooh
* @date: 2023/10/12 : 13:47
*/
@Slf4j
@Service
public class ScriptServiceImpl implements IScriptService {
@Resource(name = "commonExecutor")
private ThreadPoolTaskExecutor executor;
@Override
public void execPython(String scriptPath) {
try {
ProcessBuilder pb = new ProcessBuilder("/usr/bin/python", scriptPath);
//启动进程
Process process = pb.start();
executor.execute(() -> {
try (BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = in.readLine()) != null) {
//处理Python脚本的输出
log.info(">>>>>>>>>>script exec:{}<<<<<<<<<<", line);
}
} catch (IOException e) {
log.error("Error reading Python script output: {}", e.getMessage(), e);
throw new BusinessException(ErrorCodeEnum.E_EXEC_SCRIPT_ERROR);
}
});
// 等待 Python 脚本执行完成
int exitCode = process.waitFor();
log.info("Python script execution completed with exit code: {}", exitCode);
} catch (IOException | InterruptedException e) {
log.error(">>>>>>>>>>exec python :{} error:{}<<<<<<<<<<", scriptPath, e.getMessage(), e);
throw new BusinessException(ErrorCodeEnum.E_EXEC_SCRIPT_ERROR);
}
}
}

View File

@ -68,6 +68,8 @@ public enum ErrorCodeEnum {
CURRENT_USER_EXISTS(81011036, "当前用户已存在"),
E_EXEC_SCRIPT_ERROR(81011037, "脚本执行失败"),
;
private final int code;

0
log/jd.log Normal file
View File