TASK:异步调用generate及取消generate

This commit is contained in:
2024-01-24 11:43:56 +08:00
parent a9ce35200c
commit 96858c2cc3
10 changed files with 159 additions and 112 deletions

View File

@@ -56,6 +56,9 @@ public class PythonService {
private String accessPythonIp;
@Value("${access.python.port:''}")
private String accessPythonPort;
@Value("${access.generate.port:''}")
private String accessGeneratePort;
@Resource
private PythonTAllInfoService pythonTAllInfoService;
@@ -2251,9 +2254,10 @@ public class PythonService {
Request request = new Request.Builder()
// .url("http://18.167.251.121:9992")
// .url("http://127.0.0.1:5000/api/diffusion")
.url(accessPythonIp + ":" + accessPythonPort + "/api/diffusion")
// .url(accessPythonIp + ":" + accessPythonPort + "/api/diffusion")
.url(accessPythonIp + ":" + accessPythonPort + "/api/generate_image")
.method("POST", body)
.addHeader("Authorization", "Basic YWlkbGFiOjEyMw==")
// .addHeader("Authorization", "Basic YWlkbGFiOjEyMw==")
.addHeader("Content-Type", "application/json")
.build();
Response response = null;
@@ -2263,23 +2267,28 @@ public class PythonService {
response = client.newCall(request).execute();
} catch (IOException ioException) {
log.error("PythonService##generateSketchOrPrint异常###{}", ExceptionUtil.getThrowableList(ioException));
throw new BusinessException("generate.interface.error");
// throw new BusinessException("generate.interface.error");
throw new BusinessException(ioException.getMessage());
}
//去除限流
AccessLimitUtils.validateOut("generateSketchOrPrint");
// 判断是否生成失败
if (Objects.isNull(response) || Objects.isNull(response.body())) {
if (Objects.isNull(response.body())) {
log.error("PythonService##generateSketchOrPrint异常###{}", "response or body is empty!");
throw new BusinessException("generate.interface.error");
// throw new BusinessException("generate.interface.error");
throw new BusinessException("PythonService##generateSketchOrPrint异常###: response or body is empty!");
} else if (response.code() != HttpURLConnection.HTTP_OK){
log.error("PythonService##generateSketchOrPrint异常###{}", "Response error!Response code ## " + response.code() + " ##");
throw new BusinessException("generate.interface.error");
// throw new BusinessException("generate.interface.error");
throw new BusinessException("PythonService##generateSketchOrPrint异常### Response error!Response code ## " + response.code() + " ##");
} else {
try {
bodyString = response.body().string();
} catch (IOException e) {
throw new BusinessException("generate.interface.error");
log.error(e.getMessage());
// throw new BusinessException("generate.interface.error");
throw new BusinessException(e.getMessage());
}
}
JSONObject jsonObject = JSON.parseObject(bodyString);
@@ -2417,4 +2426,28 @@ public class PythonService {
//生成失败
throw new BusinessException("cloth-classification.interface.exception");
}
public void cancelGenerateTask(String taskId){
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
.readTimeout(60, TimeUnit.SECONDS)//读取超时(单位:秒)
.writeTimeout(60, TimeUnit.SECONDS)//写入超时(单位:秒)
.build();
HttpUrl.Builder builder = HttpUrl.parse(accessPythonIp + ":" + accessGeneratePort + "/cancel_task").newBuilder();
builder.addQueryParameter("task_id",taskId);
Request request = new Request.Builder()
.url(builder.build().toString())
.addHeader("Authorization", "Basic YWlkbGFiOjEyMw==")
.addHeader("Content-Type", "application/json")
.build();
try {
log.info("getGenerateResult请求入参content###{}", taskId);
client.newCall(request).execute();
} catch (IOException ioException) {
log.error("PythonService##getGenerateResult异常###{}", ExceptionUtil.getThrowableList(ioException));
throw new BusinessException("generate.interface.error");
}
}
}