From bba4f01afcf58cce7b1da82c0377203b4506363f Mon Sep 17 00:00:00 2001 From: JiyangTang Date: Thu, 12 Oct 2023 15:34:51 +0800 Subject: [PATCH] add python script schedule --- .../admin/schedule/ScriptSchedule.java | 31 +++++++++++++++++++ .../services/impl/ScriptServiceImpl.java | 25 ++++++--------- 2 files changed, 40 insertions(+), 16 deletions(-) create mode 100644 baogutang-admin/src/main/java/top/baogutang/admin/schedule/ScriptSchedule.java diff --git a/baogutang-admin/src/main/java/top/baogutang/admin/schedule/ScriptSchedule.java b/baogutang-admin/src/main/java/top/baogutang/admin/schedule/ScriptSchedule.java new file mode 100644 index 0000000..937130c --- /dev/null +++ b/baogutang-admin/src/main/java/top/baogutang/admin/schedule/ScriptSchedule.java @@ -0,0 +1,31 @@ +package top.baogutang.admin.schedule; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import top.baogutang.admin.services.IScriptService; + +import javax.annotation.Resource; + +/** + * @description: 脚本schedule + * @author: nikooh + * @date: 2023/10/12 : 15:30 + */ +@Slf4j +@Component +@RefreshScope +public class ScriptSchedule { + + @Resource + private IScriptService scriptService; + + /** + * 每天11:50执行京东抢购茅台脚本 + */ + @Scheduled(cron = "0 50 11 * * ?") + public void maoTaiScript() { + scriptService.execPython("/usr/local/scripts/10-07MaoTai/main.py"); + } +} 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 index 900b6c9..252d75a 100644 --- 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 @@ -1,13 +1,12 @@ package top.baogutang.admin.services.impl; import lombok.extern.slf4j.Slf4j; -import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; +import org.springframework.scheduling.annotation.Async; 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; @@ -21,29 +20,23 @@ import java.io.InputStreamReader; @Service public class ScriptServiceImpl implements IScriptService { - @Resource(name = "commonExecutor") - private ThreadPoolTaskExecutor executor; @Override + @Async("commonExecutor") public void execPython(String scriptPath) { try { ProcessBuilder pb = new ProcessBuilder("/usr/bin/python3", 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); - } - }); + BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line; + while ((line = in.readLine()) != null) { + //处理Python脚本的输出 + log.info(">>>>>>>>>>script exec:{}<<<<<<<<<<", line); + } // 等待 Python 脚本执行完成 int exitCode = process.waitFor(); + in.close(); log.info("Python script execution completed with exit code: {}", exitCode); } catch (IOException | InterruptedException e) { log.error(">>>>>>>>>>exec python :{} error:{}<<<<<<<<<<", scriptPath, e.getMessage(), e);