Ver Fonte

Log consumer

Guillermo Espinoza há 7 anos atrás
pai
commit
b0ef9647e7

+ 6 - 0
Resources/config/rabbit_mq/config.yml

@@ -42,3 +42,9 @@ old_sound_rabbit_mq:
             exchange_options: {name: 'command', type: topic}
             queue_options:    {name: 'command', routing_keys : ["%env(AMQP_KEY)%"]}
             callback:         command_consumer_service # sf service id
+            
+        log_consumer:
+            connection:       default
+            exchange_options: {name: 'log', type: topic}
+            queue_options:    {name: 'log', routing_keys : ["#"]}
+            callback:         log_consumer_service # sf service id

+ 17 - 0
Resources/config/rabbit_mq/services.yml

@@ -2,6 +2,7 @@ parameters:
     tasklogger_service.class: WorkflowBundle\Services\TaskLoggerService
     workflow.producer_service.class: WorkflowBundle\Services\ProducerService
     command_consumer_service.class: WorkflowBundle\Services\CommandConsumer
+    log_consumer_service.class: WorkflowBundle\Services\LogConsumer
 
 services:
     flowdat_tasklogger_service:
@@ -14,3 +15,19 @@ services:
     command_consumer_service:
         class: '%command_consumer_service.class%'
         arguments: ['@service_container']
+        
+    amqp.connection:
+        class: '%old_sound_rabbit_mq.connection.class%'
+        arguments: ["%rabbit_mq.host%", "%rabbit_mq.port%", "%rabbit_mq.user%", "%rabbit_mq.password%", "%rabbit_mq.vhost%"]
+
+    amqp.channel:
+        class: PhpAmqpLib\Channel\AMQPChannel
+        arguments: ["@amqp.connection"]
+
+    monolog.amqp:
+        class: Monolog\Handler\AmqpHandler
+        arguments: ["@amqp.channel"]
+
+    log_consumer_service:
+        class: '%log_consumer_service.class%'
+        arguments: ['@service_container']

+ 98 - 0
Services/LogConsumer.php

@@ -0,0 +1,98 @@
+<?php
+
+namespace WorkflowBundle\Services;
+
+use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
+use PhpAmqpLib\Message\AMQPMessage;
+use WebserviceBundle\Services\Webservice;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+class LogConsumer implements ConsumerInterface
+{
+
+    /**
+     * @var Webservice
+     */
+    protected $webservice = null;
+
+    /**
+     * @var string
+     */
+    protected $logPostUrl = null;
+
+    /**
+     * @var ContainerInterface
+     */
+    protected $serviceContainer;
+
+
+    /**
+     * @param ContainerInterface $serviceContainer
+     */
+    public function __construct(ContainerInterface $serviceContainer)
+    {
+        $this->serviceContainer = $serviceContainer;
+        if ($serviceContainer->has('webservice')) {
+            $this->webservice = $serviceContainer->get('webservice');
+        }
+        if ($serviceContainer->hasParameter('log_post_url')) {
+            $this->logPostUrl = $serviceContainer->getParameter('log_post_url');
+        }
+    }
+
+    /**
+     * $msg will be an instance of `PhpAmqpLib\Message\AMQPMessage` 
+     * with the $msg->body being the data sent over RabbitMQ.
+     * 
+     * @param AMQPMessage $msg
+     */
+    public function execute(AMQPMessage $msg)
+    {
+        $message = '';
+        try {
+            $msgBody = json_decode($msg->body, true);
+            if (!json_last_error() && isset($msgBody['message'])) {
+                $message = $msgBody['message'];
+            }
+        } catch (\Exception $ex) {
+            $message = $ex->getMessage();
+        }
+        
+        $log = $this->createLog($message);
+        if (is_null($log)) {
+            var_dump('Error: log no creado');
+        } else {
+            var_dump($log);
+        }
+                
+        return;
+    }
+    
+    /**
+     * Crea una entidad Log y la persiste
+     * 
+     * @param string $message
+     * 
+     * @return \WorkflowBundle\Services\LogBundle\Entity\Log
+     */
+    private function createLog($message)
+    {
+        $logClass = 'LogBundle\Entity\Log';
+        if ($message && class_exists($logClass)) {
+            $log = new $logClass();
+            $log->setMessage($message);
+
+            $em = $this->serviceContainer->get('doctine.entity_manager');
+            $validator = $this->serviceContainer->get('validator');
+            if ($validator->validate($log)->count() == 0) {
+                $em->persist($log);
+                $em->flush($log);
+
+                return $log;
+            }
+        }
+        
+        return null;
+    }
+
+}