首页 > 解决方案 > lambda 返回后 AWS 转录作业未完成

问题描述

我正在尝试在 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 秒。一些帮助或指示将不胜感激。

标签: javaaws-lambdaaws-transcribe

解决方案


您正在启动CompletableFuture,但不等待它完成。

调用get()以等待它等待它完成执行。

        [...]
        logger.log("jobResult =  " + jobResult.toString());
        jobResult.get();

        APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
        [...]

这也解释了为什么它在你调用时会起作用sleep(),因为它给了 Future 足够的时间来完成。

即使调用只执行 HTTPS 请求,lambda 也会更快完成(HTTPS 连接的创建成本很高)。


推荐阅读