add python script schedule
This commit is contained in:
parent
2dcdab78b5
commit
bba4f01afc
@ -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");
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user