调整时间

This commit is contained in:
2026-01-23 17:24:11 +08:00
parent 32f5fe7604
commit 78fb532fa2
3 changed files with 137 additions and 15 deletions

View File

@@ -4,7 +4,6 @@ import com.tem.bocai.entity.CompletedToday;
import com.tem.bocai.entity.LotteryResult; import com.tem.bocai.entity.LotteryResult;
import com.tem.bocai.repository.CompletedTodayRepository; import com.tem.bocai.repository.CompletedTodayRepository;
import com.tem.bocai.repository.LotteryResultRepository; import com.tem.bocai.repository.LotteryResultRepository;
import com.tem.bocai.util.DateUtils;
import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;

View File

@@ -0,0 +1,117 @@
package com.tem.bocai.schedules;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 执行统计脚本
*/
@Component
public class ExAggregateDataScriptSchedule {
/**
* 处理文件路径,确保路径正确
* @param filePath 文件路径
* @return 处理后的文件路径
*/
private String handleFilePath(String filePath) {
// 处理路径分隔符,统一使用系统默认分隔符
filePath = filePath.replace("/", System.getProperty("file.separator"));
filePath = filePath.replace("\\", System.getProperty("file.separator"));
// 如果路径包含空格,确保路径被正确处理
// Runtime.exec会自动处理带空格的路径不需要手动添加引号
return filePath;
}
public static void main(String[] args) {
ExAggregateDataScriptSchedule schedule = new ExAggregateDataScriptSchedule();
schedule.executePythonScript();
}
// 每天凌晨2点执行
@Scheduled(cron = "0 10 6 * * ?")
public void executePythonScript() {
System.out.println("开始执行Python脚本...");
// 执行aggregate_data_v7.py脚本不需要参数
String[] params = {};
executePythonScriptWithParams("aggregate_data_v7.py", params);
}
/**
* 执行带参数的Python脚本
* @param scriptPath Python脚本路径
* @param params 脚本参数数组(可以包含文件路径)
*/
public void executePythonScriptWithParams(String scriptPath, String[] params) {
try {
// 处理脚本路径,确保路径正确
scriptPath = handleFilePath(scriptPath);
// 构建命令数组
String[] command = new String[params.length + 2];
command[0] = "python";
command[1] = scriptPath;
// 添加参数,处理文件路径参数
for (int i = 0; i < params.length; i++) {
// 检查参数是否为文件路径(包含路径分隔符)
if (params[i].contains("\\") || params[i].contains("/")) {
command[i + 2] = handleFilePath(params[i]);
} else {
command[i + 2] = params[i];
}
}
System.out.println("执行命令: " + String.join(" ", command));
// 创建ProcessBuilder并设置工作目录为PyModel
ProcessBuilder pb = new ProcessBuilder(command);
// 设置工作目录为PyModel
pb.directory(new java.io.File("PyModel"));
// 执行Python脚本
Process process = pb.start();
// 读取脚本输出
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream(), "UTF-8")
);
String line;
StringBuilder output = new StringBuilder();
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
// 读取错误输出
BufferedReader errorReader = new BufferedReader(
new InputStreamReader(process.getErrorStream(), "UTF-8")
);
StringBuilder errorOutput = new StringBuilder();
while ((line = errorReader.readLine()) != null) {
errorOutput.append(line).append("\n");
}
// 等待脚本执行完成
int exitCode = process.waitFor();
System.out.println("Python脚本执行完成退出码: " + exitCode);
System.out.println("脚本输出:\n" + output.toString());
if (exitCode != 0) {
System.err.println("脚本执行错误:\n" + errorOutput.toString());
}
} catch (IOException | InterruptedException e) {
System.err.println("执行Python脚本时发生错误:");
e.printStackTrace();
}
}
}

View File

@@ -2,12 +2,16 @@ package com.tem.bocai.schedules;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
/**
* 执行预测脚本
*/
@Component @Component
public class GetPredictResultSchedule { public class ExBetScriptSchedule {
/** /**
* 处理文件路径确保路径正确 * 处理文件路径确保路径正确
@@ -25,22 +29,19 @@ public class GetPredictResultSchedule {
return filePath; return filePath;
} }
public static void main(String[] args) {
ExBetScriptSchedule schedule = new ExBetScriptSchedule();
schedule.executePythonScript();
}
// 每天凌晨2点执行 // 每天凌晨2点执行
@Scheduled(cron = "0 0 2 * * ?") @Scheduled(cron = "0 10 6 * * ?")
public void executePythonScript() { public void executePythonScript() {
System.out.println("开始执行Python脚本..."); System.out.println("开始执行Python脚本...");
// 示例1基本参数 // 执行aggregate_data_v7.py脚本不需要参数
// String[] params = {"param1", "param2", "param3"}; String[] params = {};
// executePythonScriptWithParams("your_script.py", params); executePythonScriptWithParams("batch_predict_betting_v7.py", params);
// 示例2传递文件路径作为参数
String[] paramsWithFiles = {
"--input", "data/input.csv",
"--output", "results/output.json",
"--config", "config/settings.ini"
};
executePythonScriptWithParams("scripts/process_data.py", paramsWithFiles);
} }
/** /**
@@ -70,8 +71,13 @@ public class GetPredictResultSchedule {
System.out.println("执行命令: " + String.join(" ", command)); System.out.println("执行命令: " + String.join(" ", command));
// 创建ProcessBuilder并设置工作目录为PyModel
ProcessBuilder pb = new ProcessBuilder(command);
// 设置工作目录为PyModel
pb.directory(new java.io.File("PyModel"));
// 执行Python脚本 // 执行Python脚本
Process process = Runtime.getRuntime().exec(command); Process process = pb.start();
// 读取脚本输出 // 读取脚本输出
BufferedReader reader = new BufferedReader( BufferedReader reader = new BufferedReader(