Skip to content

Instantly share code, notes, and snippets.

@dsantuc
Last active March 1, 2023 16:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsantuc/28b09e21c04620065da0e1b4caf0477b to your computer and use it in GitHub Desktop.
Save dsantuc/28b09e21c04620065da0e1b4caf0477b to your computer and use it in GitHub Desktop.
<?php
require_once("vendor/autoload.php");
use Aws\Sqs\SqsClient,
GuzzleHttp\Promise;
class AsyncQueueMonitor {
protected $sqsClient;
protected $guzzleClient;
protected $queueName;
protected $endpoint;
protected $continuePolling = true;
public function __construct ($queueName, $endpoint) {
$this->queueName = $queueName;
$this->endpoint = $endpoint;
$this->sqsClient = SqsClient::factory([
'region' => 'us-east-1',
'version' => 'latest'
]);
$this->guzzleClient = new GuzzleHttp\Client();
}
public function poll () {
while ($this->continuePolling) {
$this->fetchMessages()->then(function ($messages) {
$this->writeInfo("Received ".count($messages)." messages");
$messagePromises = [];
foreach ($messages as $message) {
$this->writeInfo("Posting message body to {$this->endpoint} : ".$message['Body']);
$messagePromises[] = $this->getMessagePromise($message);
}
return Promise\settle($messagePromises);
})
->otherwise(function ($e) {
$this->writeInfo("Polling failed: ".$e->getMessage());
$this->continuePolling = false;
})
->wait();
}
}
protected function fetchMessages () {
return $this->getQueueUrl()
->then(function ($url) {
$this->writeInfo("Fetching messages");
return $this->sqsClient->receiveMessageAsync([
'QueueUrl' => $url,
'WaitTimeSeconds' => 20, // use long polling
'MaxNumberOfMessages' => 10 // get as many messages as you can
]);
})
->then(function ($result) {
$messages = $result->get("Messages");
return $messages ?: [];
});
}
protected function getMessagePromise ($message) {
return $this->guzzleClient
->postAsync($this->endpoint, ['body' => $message['Body']])
->then(function ($result) {
$this->writeInfo("Post succeeded");
return true;
})
->otherwise(function ($e) {
$this->writeInfo("Post failed: ".$e->getMessage());
return false;
})
->then(function ($result) use ($message) {
$this->writeInfo("Deleting message");
return $this->getQueueUrl()->then(function ($queueUrl) use ($message) {
return $this->sqsClient->deleteMessageAsync([
'QueueUrl' => $queueUrl,
'ReceiptHandle' => $message['ReceiptHandle']
]);
});
})
->otherwise(function ($e) {
$this->writeInfo("Message deletion failed: ".$e->getMessage());
});
}
protected function getQueueUrl () {
return $this->sqsClient
->getQueueUrlAsync(['QueueName' => $this->queueName])
->then(function ($result) {
return $result->get('QueueUrl');
});
}
protected function writeInfo ($msg) {
echo gmdate('[Y-m-d H:i:s] ').$msg.PHP_EOL;
}
}
$monitor = new AsyncQueueMonitor("test", "https://httpbin.org/delay/3");
$monitor->poll();
@dsantuc
Copy link
Author

dsantuc commented Oct 23, 2019

The AWS PHP SDK conveniently includes async versions of all the API methods, implemented with the Guzzle HTTP and Promise libraries. (Yes, async PHP is a thing now.)

The nice thing about this is that it allows you to pull multiple messages off of a queue and process them in parallel, as demonstrated here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment