Created
September 9, 2012 14:26
-
-
Save rande/3684668 to your computer and use it in GitHub Desktop.
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 | |
namespace Sonata\Bundle\ScreenyBundle\Consumer; | |
use Sonata\Bundle\ScreenyBundle\Screeny\UrlShotManager; | |
use PhpAmqpLib\Message\AMQPMessage; | |
use OldSound\RabbitMqBundle\RabbitMq\Producer; | |
class UrlShotConsumer | |
{ | |
protected $manager; | |
protected $producer; | |
/** | |
* @param UrlShotManager $manager | |
* @param Producer $producer | |
*/ | |
public function __construct(UrlShotManager $manager, Producer $producer) | |
{ | |
$this->manager = $manager; | |
$this->producer = $producer; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function execute(AMQPMessage $message) | |
{ | |
$data = json_decode($message->body, true); | |
if (!isset($data['url'])) { | |
return true; | |
} | |
try { | |
$this->manager->shot($data['url']); | |
} catch(\Exception $e) { | |
$data = json_encode(array( | |
'url' => $data['url'], | |
'status' => 'ko', | |
'reason' => $e->getMessage() | |
)); | |
// $this->producer->publish($data, 'shot_topic.error'); | |
} | |
echo sprintf("shot - %s - %s\n", time(), $data['url']); | |
$data = json_encode(array( | |
'url' => $data['url'], | |
'status' => 'ok' | |
)); | |
// $this->producer->publish($data, 'shot_topic.completed'); | |
$msg = new AMQPMessage($data, array('content_type' => 'text/plain', 'delivery_mode' => 2)); | |
$message->get('channel')->basic_publish($msg, 'shot_topic', 'shot_topic.completed'); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment