Last active
August 1, 2016 20:22
-
-
Save tbrianjones/08b9cae7ac9fa5036842 to your computer and use it in GitHub Desktop.
A Simple Web App to manage Amazon SQS Queues ... delete/clear ... and add items to the queues ... all via web forms.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// include api config file | |
require_once( dirname( __FILE__ ) . '/../../config.php' ); | |
// create aws sqs conneciton | |
require_once( BASE_PATH . 'libraries/aws_php_sdk_v2/vendor/autoload.php' ); | |
use Aws\Sqs\SqsClient; | |
try { | |
$config = array( | |
'key' => AWS_KEY, | |
'secret' => AWS_SECRET_KEY, | |
'region' => AWS_REGION | |
); | |
$Sqs = SqsClient::factory( $config ); | |
} catch ( Exception $e ) { | |
die( 'Could not connect to Amazon SQS: ' . $e->getMessage() ); | |
} | |
// get current queues | |
$Response = $Sqs->listQueues(); | |
$queues = $Response->get( 'QueueUrls' );var | |
// --- DO STUFF BECASUE OF FORMS ------------------------------------------------- | |
if( isset( $_POST['queue_url'] ) AND $_POST['queue_url'] != 'choose' ) | |
{ | |
// process clear queue request | |
if( isset( $_POST['submit_clear_queue'] ) ) | |
{ | |
try { | |
$queue_url = $_POST['queue_url']; | |
$queue_name = substr( $queue_url, 49 ); | |
// get queue's attributes so we can recreate it correctly | |
$Response = $Sqs->getQueueAttributes( | |
array( | |
'QueueUrl' => $queue_url, | |
'AttributeNames' => array( 'VisibilityTimeout', 'MessageRetentionPeriod' ) | |
) | |
); | |
$attributes = $Response->get( 'Attributes' ); | |
// delete queue | |
$Response = $Sqs->deleteQueue( | |
array( | |
'QueueUrl' => $queue_url | |
) | |
); | |
echo "<p>Queue Deleted: $queue_url</p>"; | |
// sleep for 70 seconds ( to allow queue to delete ... takes up to 60 seconds ) | |
sleep( 70 ); | |
// create queue again with same attributes | |
$Response = $Sqs->createQueue( | |
array( | |
'QueueName' => $queue_name, | |
'Attributes' => $attributes | |
) | |
); | |
echo "<p>Queue Created: $queue_url</p>"; | |
} catch ( Exception $e ) { | |
die( 'SQS Queue Requests failed: ' . $e->getMessage() ); | |
} | |
} | |
// process add ids to queue request | |
if( isset( $_POST['submit_add_to_queue'] ) ) | |
{ | |
$queue_url = $_POST['queue_url']; | |
$company_ids = explode( "\n", $_POST['ids'] ); | |
foreach( $company_ids as $key => $id ) | |
$company_ids[$key] = trim( $id ); | |
// send ids to queue | |
try { | |
$i = 0; | |
$total_companies_to_queue = count( $company_ids ); | |
$entries = array(); | |
foreach( $company_ids as $id ) { | |
$i++; | |
$company['company_id'] = $id; | |
$entry['Id'] = $id; | |
$entry['MessageBody'] = json_encode( $company ); | |
$entries[] = $entry; | |
// max of ten entries can be batched at once | |
if( | |
count( $entries ) == 10 | |
OR $i == $total_companies_to_queue | |
) { | |
$Response = $Sqs->sendMessageBatch( | |
array( | |
'QueueUrl' => $queue_url, | |
'Entries' => $entries | |
) | |
); | |
$entries = array(); | |
} | |
} | |
} catch ( Exception $e ) { | |
die( 'SQS sendMessageBatch failed: ' . $e->getMessage() ); | |
} | |
$response_message = "<p>Company IDs added to Queue: $queue_url</p>"; | |
} | |
} else { | |
echo '<p>Choose some stuff below.</p>'; | |
} | |
?> | |
<html> | |
<head> | |
<style> | |
h1 { | |
background-color: #000; | |
color: #fff; | |
padding: 10px; | |
margin: 0 0 40px 0; | |
} | |
h2 { | |
margin: 50px 0 10px 0; | |
padding: 0 0 10px 0; | |
border-bottom: 1px solid #ccc; | |
} | |
p { | |
margin: 5px 0; | |
width: 450px; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Manage Cortex SQS</h1> | |
<h2>Clear a Queue</h2> | |
<p>This queue will be deleted, deleting all messages. Then it will be recreated. This is the only way to clear an SQS Queue.</p> | |
<p style="color: red;">This takes ~65 seconds.</p> | |
<form name="clear_queue" method="post" action=""> | |
<select name="queue_url"> | |
<option value="choose">Choose A Queue</option> | |
<?php | |
foreach( $queues as $queue ) | |
echo '<option value="'.$queue.'">'.substr( $queue, 49 ).'</option>'; | |
?> | |
</select> | |
<input type="submit" name="submit_clear_queue" value="Submit"> | |
</form> | |
<h2>Add IDs to a Queue</h2> | |
<p>Paste IDs in one per line.</p> | |
<form name="add_to_queue" method="post" action=""> | |
<textarea name="ids" rows="20" cols="50"></textarea> | |
<br /> | |
<select name="queue_url"> | |
<option value="choose">Choose A Queue</option> | |
<?php | |
foreach( $queues as $queue ) | |
echo '<option value="'.$queue.'">'.substr( $queue, 49 ).'</option>'; | |
?> | |
</select> | |
<input type="submit" name="submit_add_to_queue" value="Submit"> | |
</form> | |
<div style="padding: 100px 0;"> </div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment