我正在尝试在lambda中启动异步转录作业。我配置了一个cloudwatch事件,该事件应在转录作业完成时触发;这样我就可以在不同的Lambda中完成作业。 但是问题在于,异步转录作业已与日志中的以下jobResult成功启动,但是该作业从未完成,并且未触发作业完成事件。
jobResult = java.util.concurrent.CompletableFuture@481a996b[Not completed, 1 dependents]
我的代码在以下几行-
public class APIGatewayTranscriptHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent event, Context context) {
S3Client s3Client = S3Client.create();
String fileUrl = s3Client.utilities().getUrl(GetUrlRequest.builder().bucket("srcBucket").key("fileName").build()).toString();
Media media = Media.builder().mediaFileUri(fileUrl).build();
StartTranscriptionJobRequest request = StartTranscriptionJobRequest.builder().
languageCode(LanguageCode.ES_ES)
.media(media).outputBucketName("destBucket")
.transcriptionJobName("jobName")
.mediaFormat("mp3")
.settings(Settings.builder().showSpeakerLabels(true).maxSpeakerLabels(2).build())
.build();
TranscribeAsyncClient transcribeAsyncClient = TranscribeAsyncClient.create();
CompletableFuture<StartTranscriptionJobResponse> jobResult = transcribeAsyncClient.startTranscriptionJob(request);
logger.log("jobResult = " + jobResult.toString());
jobResult.whenComplete((jobResponse, err) -> {
try {
if (jobResponse != null) {
logger.log("CompletableFuture : response = " + jobResponse.toString());
} else {
logger.log("CompletableFuture : NULL response: error = " + err.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
}
});
//Job is completed only if Thread is made to sleep
/*try {
Thread.sleep(50000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
response.setStatusCode(200);
Map<String, String> responseBody = new HashMap<String, String>();
responseBody.put("Status", jobResult.toString());
String responseBodyString = new JSONObject(responseBody).toJSONString();
response.setBody(responseBodyString);
return response;
}
}
我已经验证,音频文件存在于源存储桶中。
以上作业完成,并且只有在启动作业后在lambda中添加一些睡眠时间后,才会触发作业完成事件。
例如,
Thread.sleep(50000);
如果添加了睡眠时间,那么一切都会按预期进行。 但是如果没有Thread.sleep(),该工作将永远无法完成。 lambda的超时配置为60秒。 一些帮助或指针将不胜感激。
答案 0 :(得分:2)
您正在启动CompletableFuture
,但不等待其完成。
调用get()
等待它等待执行完成。
[...]
logger.log("jobResult = " + jobResult.toString());
jobResult.get();
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
[...]
这也解释了为什么当您致电sleep()
时它可以工作,因为它为将来提供了足够的时间来完成。
即使调用仅执行HTTPS请求,lambda也将更快完成(HTTPS连接的创建成本很高)。