简体   繁体   English

如何使用PHP SDK在AWS Elastic Beanstalk Worker环境中删除SQS消息

[英]How to delete SQS message in AWS Elastic Beanstalk Worker Environments using PHP SDK

I have an AWS Elastic Beanstalk Worker Environment setup using SQS. 我有一个使用SQS的AWS Elastic Beanstalk Worker环境设置。 The SQS is posting to a URL, which is an endpoint to a codebase that uses Laravel. SQS正在发布到URL,该URL是使用Laravel的代码库的终结点。 From this endpoint, it takes up the message and process the payload. 从此端点开始,它接收消息并处理有效负载。 Some of my processes are time-consuming and is taking more than 20 minutes to complete. 我的某些过程很耗时,需要20多个分钟才能完成。 I am sending back a success response from the endpoint, but as it takes a lot of time to complete the process, most of the time, the messages are going to the inflight mode in SQS. 我正在从端点发送回成功响应,但是由于要花费大量时间来完成该过程,因此大多数情况下,消息将进入SQS中的飞行模式。 I am trying to make a deleteMessage() call using PHP SDK, but I need to pass ReceiptHandle for deleting a message. 我正在尝试使用PHP SDK进行deleteMessage()调用,但是我需要传递ReceiptHandle来删除消息。 As per the documentation here , SQS is not posting ReceiptHandle to my application and so I can't make a delete call. 根据此处的文档,SQS不会将ReceiptHandle发布到我的应用程序,因此无法进行删除调用。 The problem with messages in inFlight mode is that it will be called again in the next time the and so the process is duplicated. 处于inFlight模式的消息的问题在于,下次将再次调用该消息,因此该过程被重复。

How can I delete a message once the process is completed ? 完成该过程后,如何删除一条消息?

My current code is as follows : 我当前的代码如下:

 $worker->process(
            $request->header('X-Aws-Sqsd-Queue'), $job, [
                'maxTries' => 0,
                'delay' => 0
            ]
        );
        return $this->response([
            'Processed ' . $job->getJobId()
        ]);

Where worker is an instance of 其中worker是...的实例

Illuminate\Queue\Worker;

and the response function is json encoding the data and respond with 200 response函数是对数据进行json编码并以200响应

You must have ReceiptHandle to delete a message in queue. 您必须具有ReceiptHandle才能删除队列中的消息。 In core PHP below is the function to read and delete a message in queue. 下面的核心PHP中的功能是读取和删除队列中的消息。

function ReadMessages($client,$queueUrl){
    try {
        $result = $client->receiveMessage(array(
            'AttributeNames' => ['SentTimestamp'],
            'MaxNumberOfMessages' => 1,
            'MessageAttributeNames' => ['All'],
            'QueueUrl' => $queueUrl, // REQUIRED
            'WaitTimeSeconds' => 0,
        ));
        if (count($result->get('Messages')) > 0) {
            var_dump($result->get('Messages')[0]);

            //to delete a message pass the receipt Handle
            $result = $client->deleteMessage([
                'QueueUrl' => $queueUrl, // REQUIRED
                'ReceiptHandle' => $result->get('Messages')[0]['ReceiptHandle'] // REQUIRED
            ]);
            } else {
                echo "No messages in queue. \n";
            }
        }   
    catch (AwsException $e) {
        // output error message if fails
        return 'error';
        error_log($e->getMessage());
    }
}

Do some workaround so you get a ReceiptHandle to delete a message in queue. 采取一些变通办法,以便获得一个ReceiptHandle来删除队列中的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM