Browse Source

Some fixes for reducing NPath complexity

Marc 11 năm trước cách đây
mục cha
commit
5f6386fe80

+ 1 - 0
DependencyInjection/GearmanExtension.php

@@ -41,6 +41,7 @@ class GearmanExtension extends Extension
         $container->setParameter('gearman.default.settings', $config['defaults']);
 
         $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
+        $loader->load('parameters.yml');
         $loader->load('services.yml');
     }
 }

+ 9 - 0
Module/WorkerClass.php

@@ -178,10 +178,19 @@ class WorkerClass
             $reflectionMethod = new ReflectionMethod($method->class, $method->name);
             $methodAnnotations = $reader->getMethodAnnotations($reflectionMethod);
 
+            /**
+             * Every annotation found is parsed
+             */
             foreach ($methodAnnotations as $methodAnnotation) {
 
+                /**
+                 * Annotation is only laoded if is typeof JobAnnotation
+                 */
                 if ($methodAnnotation instanceof JobAnnotation) {
 
+                    /**
+                     * Creates new Job
+                     */
                     $job = new Job($methodAnnotation, $reflectionMethod, $this->callableName, $this->servers, $defaultSettings);
                     $this->jobCollection->add($job);
                 }

+ 7 - 0
Resources/config/parameters.yml

@@ -0,0 +1,7 @@
+parameters:
+
+    gearman.client.class:           Mmoreram\GearmanBundle\Service\GearmanClient
+    gearman.execute.class:          Mmoreram\GearmanBundle\Service\GearmanExecute
+    gearman.describer.class:        Mmoreram\GearmanBundle\Service\GearmanDescriber
+    gearman.cache.wrapper.class:    Mmoreram\GearmanBundle\Service\GearmanCacheWrapper
+    gearman.cache.id:               gearman.workers

+ 6 - 16
Resources/config/services.yml

@@ -1,13 +1,3 @@
-parameters:
-
-    gearman.client.class:           Mmoreram\GearmanBundle\Service\GearmanClient
-    gearman.cache.id:               gearman.workers
-    gearman.cache.wrapper.class:    Mmoreram\GearmanBundle\Service\GearmanCacheWrapper
-    gearman.execute.class:          Mmoreram\GearmanBundle\Service\GearmanExecute
-    gearman.describer.class:        Mmoreram\GearmanBundle\Service\GearmanDescriber
-
-
-
 services:
 
     gearman.cache.wrapper:
@@ -23,6 +13,11 @@ services:
           - [loadNamespaceMap,  []]
           - [load,  []]
 
+    gearman.describer:
+        class: %gearman.describer.class%
+        arguments:
+            kernel: @kernel
+
     gearman.abstract.service:
         abstract:  true
         arguments:
@@ -36,9 +31,4 @@ services:
 
     gearman:
         class: %gearman.client.class%
-        parent: gearman.abstract.service
-
-    gearman.describer:
-        class: %gearman.describer.class%
-        arguments:
-            kernel: @kernel
+        parent: gearman.abstract.service

+ 36 - 18
Service/GearmanCacheWrapper.php

@@ -20,7 +20,7 @@ use Symfony\Component\Finder\Finder;
 use Mmoreram\GearmanBundle\Module\WorkerCollection;
 use Mmoreram\GearmanBundle\Module\WorkerDirectoryLoader;
 use Mmoreram\GearmanBundle\Module\WorkerClass as Worker;
-use Mmoreram\GearmanBundle\Driver\Gearman\Work;
+use Mmoreram\GearmanBundle\Driver\Gearman\Work as WorkAnnotation;
 use ReflectionClass;
 
 /**
@@ -177,33 +177,37 @@ class GearmanCacheWrapper
      */
     public function loadNamespaceMap()
     {
+        /**
+         * Iteratinc all bundle settings
+         */
         foreach ($this->bundles as $bundleSettings) {
 
-            $bundleNamespace = $bundleSettings['name'];
+            if (!$bundleSettings['active']) {
 
-            if ($bundleSettings['active']) {
+                break;
+            }
 
-                $bundlePath = $this->kernelBundles[$bundleNamespace]->getPath();
+            $bundleNamespace = $bundleSettings['name'];
+            $bundlePath = $this->kernelBundles[$bundleNamespace]->getPath();
 
-                if (!empty($bundleSettings['include'])) {
+            if (!empty($bundleSettings['include'])) {
 
-                    foreach ($bundleSettings['include'] as $include) {
+                foreach ($bundleSettings['include'] as $include) {
 
-                        $this->paths[] = rtrim(rtrim($bundlePath, '/') . '/' . $include, '/') . '/';
-                    }
+                    $this->paths[] = rtrim(rtrim($bundlePath, '/') . '/' . $include, '/') . '/';
+                }
 
-                } else {
+            } else {
 
-                    /**
-                     * If no include is set, include all namespace
-                     */
-                    $this->paths[] = rtrim($bundlePath, '/') . '/';
-                }
+                /**
+                 * If no include is set, include all namespace
+                 */
+                $this->paths[] = rtrim($bundlePath, '/') . '/';
+            }
 
-                foreach ($bundleSettings['ignore'] as $ignore) {
+            foreach ($bundleSettings['ignore'] as $ignore) {
 
-                    $this->excludedPaths[] = trim($ignore, '/');
-                }
+                $this->excludedPaths[] = trim($ignore, '/');
             }
         }
     }
@@ -240,6 +244,9 @@ class GearmanCacheWrapper
             ->exclude($this->excludedPaths)
             ->in($this->paths);
 
+        /**
+         * Every file found is parsed
+         */
         foreach ($finder as $file) {
 
             /**
@@ -249,10 +256,19 @@ class GearmanCacheWrapper
             $reflClass = new ReflectionClass($classNamespace);
             $classAnnotations = $reader->getClassAnnotations($reflClass);
 
+            /**
+             * Every annotation found is parsed
+             */
             foreach ($classAnnotations as $annot) {
 
-                if ($annot instanceof Work) {
+                /**
+                 * Annotation is only laoded if is typeof WorkAnnotation
+                 */
+                if ($annot instanceof WorkAnnotation) {
 
+                    /**
+                     * Creates new Worker element with all its Job data
+                     */
                     $worker = new Worker($annot, $reflClass, $reader, $this->servers, $this->defaultSettings);
                     $workerCollection->add($worker);
                 }
@@ -277,9 +293,11 @@ class GearmanCacheWrapper
         $namespace = false;
         $tokens = token_get_all(file_get_contents($file));
         for ($i = 0, $count = count($tokens); $i < $count; $i++) {
+            
             $token = $tokens[$i];
 
             if (!is_array($token)) {
+
                 continue;
             }
 

+ 39 - 3
Service/GearmanDescriber.php

@@ -20,8 +20,6 @@ use Symfony\Component\HttpKernel\Kernel;
 class GearmanDescriber
 {
 
-
-
     /**
      * @var Kernel
      *
@@ -50,21 +48,41 @@ class GearmanDescriber
      */
     public function describeJob(OutputInterface $output, array $worker)
     {
-
+        /**
+         * Commandline
+         */
         $script = $this->kernel->getRootDir() . '/console gearman:job:execute';
 
+        /**
+         * A job descriptions contains its worker description
+         */
         $this->describeWorker($output, $worker);
+
         $job = $worker['job'];
         $output->writeln('<info>    @job\methodName : ' . $job['methodName'] . '</info>');
         $output->writeln('<info>    @job\callableName : ' . $job['realCallableName'] . '</info>');
+
+        /**
+         * Also a complete and clean execution path is given , for supervisord
+         */
         $output->writeln('<info>    @job\supervisord : </info><comment>/usr/bin/php ' . $script.' ' . $job['realCallableName'] . ' --no-interaction</comment>');
         $output->writeln('<info>    @job\iterations : ' . $job['iterations'] . '</info>');
         $output->writeln('<info>    @job\defaultMethod : ' . $job['defaultMethod'] . '</info>');
+        
+
+        /**
+         * Printed every server is defined for current job
+         */
+        $output->writeln('');
         $output->writeln('<info>    @job\servers :</info>');
         $output->writeln('');
         foreach ($job['servers'] as $name => $server) {
             $output->writeln('<comment>        ' . $name . ' - ' . $server['host'] . ':' . $server['port'] . '</comment>');
         }
+
+        /**
+         * Description
+         */
         $output->writeln('');
         $output->writeln('<info>    @job\description :</info>');
         $output->writeln('');
@@ -83,6 +101,9 @@ class GearmanDescriber
      */
     public function describeWorker(OutputInterface $output, array $worker, $tinyJobDescription = false)
     {
+        /**
+         * Commandline
+         */
         $script = $this->kernel->getRootDir() . '/console gearman:worker:execute';
 
         $output->writeln('');
@@ -90,11 +111,19 @@ class GearmanDescriber
         $output->writeln('<info>    @Worker\fileName : ' . $worker['fileName'] . '</info>');
         $output->writeln('<info>    @Worker\nameSpace : ' . $worker['namespace'] . '</info>');
         $output->writeln('<info>    @Worker\callableName: ' . $worker['callableName'] . '</info>');
+
+        /**
+         * Also a complete and clean execution path is given , for supervisord
+         */
         $output->writeln('<info>    @Worker\supervisord : </info><comment>/usr/bin/php ' . $script.' ' . $worker['callableName'] . ' --no-interaction</comment>');
 
+        /**
+         * Service value is only explained if defined. Not mandatory
+         */
         if (null !== $worker['service']) {
             $output->writeln('<info>    @Worker\service : ' . $worker['service'] . '</info>');
         }
+
         $output->writeln('<info>    @worker\iterations : ' . $worker['iterations'] . '</info>');
         $output->writeln('<info>    @Worker\#jobs : '.count($worker['jobs']).'</info>');
 
@@ -107,12 +136,19 @@ class GearmanDescriber
             }
         }
 
+        /**
+         * Printed every server is defined for current job
+         */
         $output->writeln('');
         $output->writeln('<info>    @worker\servers :</info>');
         $output->writeln('');
         foreach ($worker['servers'] as $name => $server) {
             $output->writeln('<comment>        #' . $name . ' - ' . $server['host'] . ':' . $server['port'] . '</comment>');
         }
+
+        /**
+         * Description
+         */
         $output->writeln('');
         $output->writeln('<info>    @Worker\description :</info>');
         $output->writeln('');

+ 36 - 1
Service/GearmanExecute.php

@@ -12,6 +12,7 @@ namespace Mmoreram\GearmanBundle\Service;
 use Mmoreram\GearmanBundle\Service\Abstracts\AbstractGearmanService;
 use Symfony\Component\DependencyInjection\Container;
 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
+use Object;
 
 /**
  * Gearman execute methods. All Worker methods
@@ -53,6 +54,7 @@ class GearmanExecute extends AbstractGearmanService
         $worker = $this->getJob($jobName);
 
         if (false !== $worker) {
+
             $this->callJob($worker);
         }
     }
@@ -62,10 +64,12 @@ class GearmanExecute extends AbstractGearmanService
      * Given a worker, execute GearmanWorker function defined by job.
      *
      * @param array $worker Worker definition
+     * 
+     * @return GearmanExecute self Object
      */
     private function callJob(Array $worker)
     {
-        $gearmanWorker = new \GearmanWorker();
+        $gearmanWorker = new \GearmanWorker;
 
         if (isset($worker['job'])) {
 
@@ -80,7 +84,22 @@ class GearmanExecute extends AbstractGearmanService
             $this->addServers($gearmanWorker, $worker['servers']);
         }
 
+        $objInstance = $this->createJob($worker);
+        $this->runJob($gearmanWorker, $objInstance, $jobs);
+        
+        return $this;
+    }
+
 
+    /**
+     * Given a worker settings, return Job instance
+     * 
+     * @parma array $worker Worker settings
+     * 
+     * @return Object Job instance
+     */
+    private function createJob(array $worker)
+    {
         /**
          * If service is defined, we must retrieve this class with dependency injection
          * 
@@ -106,6 +125,21 @@ class GearmanExecute extends AbstractGearmanService
             }
         }
 
+        return $objInstance;
+    }
+
+
+    /**
+     * Given a GearmanWorker and an instance of Job, run it
+     * 
+     * @param \GearmanWorker $gearmanWorker Gearman Worker
+     * @param Object         $objInstance   Job instance
+     * @param array          $jobs          Array of jobs to subscribe
+     * 
+     * @return GearmanExecute self Object
+     */
+    private function runJob(\GearmanWorker $gearmanWorker, $objInstance, array $jobs)
+    {
 
         /**
          * Every job defined in worker is added into GearmanWorker
@@ -131,6 +165,7 @@ class GearmanExecute extends AbstractGearmanService
                 break;
             }
         }
+
     }