From d013ff024b70dd61aa1acce9b738c7c5d69b2b3a Mon Sep 17 00:00:00 2001 From: JiyangTang Date: Thu, 12 Oct 2023 14:16:15 +0800 Subject: [PATCH] add python script controller --- .../admin/controller/ScriptController.java | 31 +++++++++++ .../admin/schedule/NoticeSchedule.java | 26 +++++---- .../admin/services/IScriptService.java | 16 ++++++ .../services/impl/ScriptServiceImpl.java | 53 +++++++++++++++++++ .../common/constants/ErrorCodeEnum.java | 2 + log/jd.log | 0 6 files changed, 117 insertions(+), 11 deletions(-) create mode 100644 baogutang-admin/src/main/java/top/baogutang/admin/controller/ScriptController.java create mode 100644 baogutang-admin/src/main/java/top/baogutang/admin/services/IScriptService.java create mode 100644 baogutang-admin/src/main/java/top/baogutang/admin/services/impl/ScriptServiceImpl.java create mode 100644 log/jd.log diff --git a/baogutang-admin/src/main/java/top/baogutang/admin/controller/ScriptController.java b/baogutang-admin/src/main/java/top/baogutang/admin/controller/ScriptController.java new file mode 100644 index 0000000..0755478 --- /dev/null +++ b/baogutang-admin/src/main/java/top/baogutang/admin/controller/ScriptController.java @@ -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 execPython(@RequestParam(name = "scriptPath", required = false) String scriptPath) { + scriptService.execPython(scriptPath); + return Results.ok(); + } +} diff --git a/baogutang-admin/src/main/java/top/baogutang/admin/schedule/NoticeSchedule.java b/baogutang-admin/src/main/java/top/baogutang/admin/schedule/NoticeSchedule.java index 6104035..b88ff19 100644 --- a/baogutang-admin/src/main/java/top/baogutang/admin/schedule/NoticeSchedule.java +++ b/baogutang-admin/src/main/java/top/baogutang/admin/schedule/NoticeSchedule.java @@ -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)) { - 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()); + 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)) { - 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()); + 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()); } - } diff --git a/baogutang-admin/src/main/java/top/baogutang/admin/services/IScriptService.java b/baogutang-admin/src/main/java/top/baogutang/admin/services/IScriptService.java new file mode 100644 index 0000000..2c346d6 --- /dev/null +++ b/baogutang-admin/src/main/java/top/baogutang/admin/services/IScriptService.java @@ -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); +} diff --git a/baogutang-admin/src/main/java/top/baogutang/admin/services/impl/ScriptServiceImpl.java b/baogutang-admin/src/main/java/top/baogutang/admin/services/impl/ScriptServiceImpl.java new file mode 100644 index 0000000..eca7deb --- /dev/null +++ b/baogutang-admin/src/main/java/top/baogutang/admin/services/impl/ScriptServiceImpl.java @@ -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); + } + } +} diff --git a/baogutang-common/src/main/java/top/baogutang/common/constants/ErrorCodeEnum.java b/baogutang-common/src/main/java/top/baogutang/common/constants/ErrorCodeEnum.java index 2f3e5dd..dca509c 100644 --- a/baogutang-common/src/main/java/top/baogutang/common/constants/ErrorCodeEnum.java +++ b/baogutang-common/src/main/java/top/baogutang/common/constants/ErrorCodeEnum.java @@ -68,6 +68,8 @@ public enum ErrorCodeEnum { CURRENT_USER_EXISTS(81011036, "当前用户已存在"), + E_EXEC_SCRIPT_ERROR(81011037, "脚本执行失败"), + ; private final int code; diff --git a/log/jd.log b/log/jd.log new file mode 100644 index 0000000..e69de29