Jelajahi Sumber

Merge branch 'master' of ssh://200.50.168.30:222/VendorSoftwareFlowdat3/WorkflowBundle

Maximiliano Schvindt 8 tahun lalu
induk
melakukan
1bd5e00e8c

+ 10 - 0
Entity/Action.php

@@ -311,5 +311,15 @@ class Action
     {
         return $this->tenancyId;
     }
+    
+    /**
+     * @param Entity $entity
+     * 
+     * @return string
+     */
+    public function getCmd($entity)
+    {
+        return '';
+    }
 
 }

+ 39 - 9
EventListener/DoctrineEventSubscriber.php

@@ -5,9 +5,25 @@ namespace WorkflowBundle\EventListener;
 use Doctrine\Common\EventSubscriber;
 use Doctrine\ORM\Event\LifecycleEventArgs;
 use WorkflowBundle\Utils\DoctrineEvents;
+use WorkflowBundle\Utils\WorkFlowEntityClasses;
+use OldSound\RabbitMqBundle\RabbitMq\Producer;
 
 class DoctrineEventSubscriber implements EventSubscriber
 {
+    
+    /**
+     * @var Producer
+     */
+    private $producer;
+    
+    
+    /**
+     * @param Producer $producer
+     */    
+    public function __construct(Producer $producer)
+    {
+        $this->producer = $producer;
+    }
 
     /**
      * @return array
@@ -22,7 +38,7 @@ class DoctrineEventSubscriber implements EventSubscriber
      */
     public function prePersist(LifecycleEventArgs $args)
     {
-        $this->execute($args);
+        $this->execute($args, DoctrineEvents::PRE_PERSIST);
     }
 
     /**
@@ -30,7 +46,7 @@ class DoctrineEventSubscriber implements EventSubscriber
      */
     public function postPersist(LifecycleEventArgs $args)
     {
-        $this->execute($args);
+        $this->execute($args, DoctrineEvents::POST_PERSIST);
     }
 
     /**
@@ -38,7 +54,7 @@ class DoctrineEventSubscriber implements EventSubscriber
      */
     public function preUpdate(LifecycleEventArgs $args)
     {
-        $this->execute($args);
+        $this->execute($args, DoctrineEvents::PRE_UPDATE);
     }
 
     /**
@@ -46,7 +62,7 @@ class DoctrineEventSubscriber implements EventSubscriber
      */
     public function postUpdate(LifecycleEventArgs $args)
     {
-        $this->execute($args);
+        $this->execute($args, DoctrineEvents::POST_UPDATE);
     }
 
     /**
@@ -54,7 +70,7 @@ class DoctrineEventSubscriber implements EventSubscriber
      */
     public function preRemove(LifecycleEventArgs $args)
     {
-        $this->execute($args);
+        $this->execute($args, DoctrineEvents::PRE_REMOVE);
     }
 
     /**
@@ -62,20 +78,34 @@ class DoctrineEventSubscriber implements EventSubscriber
      */
     public function postRemove(LifecycleEventArgs $args)
     {
-        $this->execute($args);
+        $this->execute($args, DoctrineEvents::POST_REMOVE);
     }
 
     /**
      * @param LifecycleEventArgs $args
+     * @param string $eventName
      */
-    public function execute(LifecycleEventArgs $args, $eventName = 'prePersist')
+    public function execute(LifecycleEventArgs $args, $eventName = DoctrineEvents::PRE_PERSIST)
     {
         $entity = $args->getEntity();
         $entityManager = $args->getEntityManager();
         $entityClass = get_class($entity);
         
-        $doctrine2WorkFlowActionRepository = $entityManager->getRepository('WorkflowBundle:Doctrine2WorkFlowAction');
-        $doctrine2WorkFlowActions = $doctrine2WorkFlowActionRepository->findAllByEventAndEntityClass($eventName, $entityClass);
+        // la $entity esta dentro de las entidades con workflow
+        if (in_array($entityClass, WorkFlowEntityClasses::getConstants())) {
+            $doctrine2WorkFlowActionRepository = $entityManager->getRepository('WorkflowBundle:Doctrine2WorkFlowAction');
+            $doctrine2WorkFlowActions = $doctrine2WorkFlowActionRepository->findAllByEventAndEntityClass($eventName, $entityClass);
+            foreach ($doctrine2WorkFlowActions as $doctrine2WorkFlowAction) {
+                $actions = $doctrine2WorkFlowAction->getActions();
+                foreach ($actions as $action) {
+                    $msg = array(
+                        'id' => uniqid(),
+                        'cmd' => $action->getCmd($entity),
+                    );
+                    $this->producer->publish(serialize($msg));
+                }
+            }
+        }
     }
 
 }

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

@@ -0,0 +1,34 @@
+old_sound_rabbit_mq:
+    connections:
+        default:
+            host: %rabbit_mq.host%
+            port: %rabbit_mq.port%
+            user: %rabbit_mq.user%
+            password: %rabbit_mq.password%
+            vhost: %rabbit_mq.vhost%
+            lazy: false
+            connection_timeout: 3
+            read_write_timeout: 3
+
+            # requires php-amqplib v2.4.1+ and PHP5.4+
+            keepalive: false
+
+            # requires php-amqplib v2.4.1+
+            heartbeat: 0
+
+            #requires php_sockets.dll
+#            use_socket: true # default false
+            
+    producers:
+        flowdat_tasklogger:
+            connection:       default
+            exchange_options: {name: 'send', type: direct}
+            service_alias:    fd_tasklogger_service # no alias by default
+    consumers:
+        flowdat_tasklogger:
+            connection:       default
+            exchange_options: {name: 'send', type: direct}
+            queue_options:    {name: 'send'}
+            callback:         flowdat_tasklogger_service # sf service id
+            idle_timeout:           60
+            idle_timeout_exit_code: 0

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

@@ -0,0 +1,6 @@
+parameters:
+    tasklogger_service.class: WorkflowBundle\Services\TaskLoggerService
+
+services:
+    flowdat_tasklogger_service:
+        class: '%tasklogger_service.class%'

+ 6 - 1
Resources/config/services.yml

@@ -1,3 +1,7 @@
+imports:
+    - { resource: rabbit_mq/config.yml }
+    - { resource: rabbit_mq/services.yml }
+    
 services:
     workflow.admin.workflow:
         class: WorkflowBundle\Admin\WorkflowAdmin
@@ -22,6 +26,7 @@ services:
             - [setTranslationDomain, [WorkflowBundle]]
     workflow.doctrine_event.subscriber:
         class: WorkflowBundle\EventListener\DoctrineEventSubscriber
+        arguments: ['@old_sound_rabbit_mq.flowdat_tasklogger_producer']
         tags:
             - { name: doctrine.event_subscriber, connection: default }
     workflow.event.subscriber:
@@ -33,4 +38,4 @@ services:
         class: WorkflowBundle\Twig\WorkflowExtension
         tags:
             - { name: twig.extension }
-        arguments: ["@workflow.registry"]
+        arguments: ["@workflow.registry"]

+ 77 - 0
Services/TaskLoggerService.php

@@ -0,0 +1,77 @@
+<?php
+
+namespace WorkflowBundle\Services;
+
+use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
+use PhpAmqpLib\Message\AMQPMessage;
+use Symfony\Component\Process\Process;
+
+class TaskLoggerService implements ConsumerInterface
+{
+    
+    /**
+     * Directorio donde se guardan los script
+     */
+    const TASKLOGGER_DIR = '/tmp/tasklogger';
+
+    
+    /**
+     * $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)
+    {
+        $data = unserialize($msg->getBody());
+        if (isset($data['id']) && isset($data['cmd'])) {
+            $taskloggerId = $data['id'];
+            $cmd = $data['cmd'];
+                        
+            $file_name = $this->createTaskLoggerCmdFile($taskloggerId, $cmd);
+            $output = $this->runFileProcess($file_name);
+
+            return true;
+        }
+        
+        return false;
+    }
+    
+    /**
+     * @param string $taskloggerId
+     * @param string $cmd
+     * 
+     * @return string
+     */
+    public function createTaskLoggerCmdFile($taskloggerId, $cmd)
+    {
+        $mode = 0777;
+        $tasklogger_dir = self::TASKLOGGER_DIR . DIRECTORY_SEPARATOR . $taskloggerId;
+        if (!file_exists($tasklogger_dir)) {
+            mkdir($tasklogger_dir, $mode, true);
+        }
+        
+        $file_name = $tasklogger_dir . DIRECTORY_SEPARATOR .'cmd.sh';
+        file_put_contents($file_name, $cmd);
+        chmod($file_name, $mode);
+        
+        return $file_name;
+    }
+    
+    /**
+     * @param string $filename
+     * 
+     * @return string
+     */
+    public function runFileProcess($filename)
+    {
+        $process = new Process($filename);
+        $process->run();
+        
+        return array(
+            'output' => $process->getOutput(),
+            'error' => $process->getErrorOutput(),
+        );
+    }
+
+}

+ 3 - 0
composer.json

@@ -2,6 +2,9 @@
     "name": "ik/workflow-bundle",
     "description": "Flowdat 3 Workflow Bundle",
     "keywords": ["workflow", "bundle"],
+    "require": {
+        "php-amqplib/rabbitmq-bundle": "^1.12"
+    },
     "autoload": {
         "psr-4": { "WorkflowBundle\\": "" }
     },