Skip to content

Instantly share code, notes, and snippets.

@lyrixx
Created June 19, 2014 07:40
Show Gist options
  • Save lyrixx/911baf71e12b96420420 to your computer and use it in GitHub Desktop.
Save lyrixx/911baf71e12b96420420 to your computer and use it in GitHub Desktop.
Export all messages from an AMQP queue
<?php
class MessageExporter
{
private $broker;
public function __construct(Broker $broker = null)
{
$this->broker = $broker;
}
public function export($queueName, $ack = false)
{
if ('.dead' !== substr($queueName, -5)) {
throw new \InvalidArgumentException('Only dead queue can be exported.');
}
$messages = array();
while (false !== $message = $this->broker->get($queueName)) {
$messages[] = $message;
}
if (!$messages) {
return null;
}
$filename = sprintf('%s/insight-consumer-queue-%s.tar', sys_get_temp_dir(), str_replace('.', '-', $queueName));
$tgz = $filename.'.gz';
// A previous phar could exist
file_exists($filename) and unlink($filename);
file_exists($tgz) and unlink($tgz);
$phar = new \PharData($filename);
foreach ($messages as $i => $message) {
if ($ack) {
$this->broker->ack($message);
} else {
$this->broker->nack($message, \AMQP_REQUEUE);
}
$phar->addFromString('message-'.$i, $message->getBody());
}
$phar->compress(\Phar::GZ);
// we can remove the phar, as we only use the gz'ed one
unlink($filename);
return $tgz;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment