浏览代码

Created new CacheLoader. Also added a check command

Marc 13 年之前
父节点
当前提交
3933dcc439

+ 59 - 0
Command/GearmanCheckCommand.php

@@ -0,0 +1,59 @@
+<?php
+
+namespace Mmoreramerino\GearmanBundle\Command;
+
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputDefinition;
+use Symfony\Component\Console\Output\OutputInterface;
+use Mmoreramerino\GearmanBundle\Service\GearmanSettings;
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Mmoreramerino\GearmanBundle\Exceptions\GearmanNotInstalledException;
+use Mmoreramerino\GearmanBundle\Exceptions\NoSettingsFileExistsException;
+
+/**
+ * Checks gearman environment
+ *
+ * @author Marc Morera <marc@ulabox.com>
+ */
+class GearmanCheckCommand extends ContainerAwareCommand
+{
+    /**
+     * Console Command configuration
+     */
+    protected function configure()
+    {
+        parent::configure();
+        $this->setName('gearman:check')
+             ->setDescription('Checks gearman environment');
+    }
+
+    /**
+     * Executes the current command.
+     *
+     * @param InputInterface  $input  An InputInterface instance
+     * @param OutputInterface $output An OutputInterface instance
+     *
+     * @return integer 0 if everything went fine, or an error code
+     *
+     * @throws \LogicException When this abstract class is not implemented
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        if (!in_array('gearman', get_loaded_extensions())) {
+            throw new GearmanNotInstalledException;
+        } else {
+            $output->writeln('<comment>* Checking gearman extension...</comment>');
+        }
+
+        $gearmanSettings = $this->getContainer()->get('gearman.settings');
+        if (!$gearmanSettings->existsSettings()) {
+            throw new NoSettingsFileExistsException($this->getFilePath());
+        } else {
+            $output->writeln('<comment>* Checking gearman settings file...</comment>');
+        }
+
+        $output->writeln('<comment>Gearman is succesfuly installed</comment>');
+    }
+}

+ 5 - 37
MmoreramerinoGearmanBundle.php

@@ -2,15 +2,10 @@
 
 namespace Mmoreramerino\GearmanBundle;
 
-use Symfony\Component\Config\FileLocator;
-use Doctrine\Common\Annotations\AnnotationReader;
-use Doctrine\Common\Annotations\AnnotationRegistry;
-use Mmoreramerino\GearmanBundle\Module\WorkerCollection;
+
+use Mmoreramerino\GearmanBundle\Service\GearmanCache;
 use Mmoreramerino\GearmanBundle\Module\GearmanBaseBundle;
-use Mmoreramerino\GearmanBundle\Module\WorkerDirectoryLoader;
-use Mmoreramerino\GearmanBundle\Module\WorkerClass as Worker;
-use Mmoreramerino\GearmanBundle\Service\GearmanCache as Cache;
-use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
+use Mmoreramerino\GearmanBundle\Service\GearmanCacheLoader;
 use Mmoreramerino\GearmanBundle\Exceptions\GearmanNotInstalledException;
 
 /**
@@ -44,35 +39,8 @@ class MmoreramerinoGearmanBundle extends GearmanBaseBundle
             if ($existsCache) {
                 $gearmanCache->emptyCache();
             }
-            $reader = new AnnotationReader();
-            AnnotationRegistry::registerFile(__DIR__ . "/Driver/Gearman/GearmanAnnotations.php");
-            $reader->setDefaultAnnotationNamespace('Mmoreramerino\GearmanBundle\Driver\\');
-            $workerCollection = new WorkerCollection;
-            $bundles = $this->container->get('kernel')->getBundles();
-
-            foreach ($bundles as $bundle) {
-
-                if (!\in_array($bundle->getNamespace(), $this->getParseableBundles())) {
-                    continue;
-                }
-                $filesLoader = new WorkerDirectoryLoader(new FileLocator('.'));
-                $files = $filesLoader->load($bundle->getPath());
-
-                foreach ($files as $file) {
-                    $reflClass = new \ReflectionClass($file['class']);
-                    $classAnnotations = $reader->getClassAnnotations($reflClass);
-
-                    foreach ($classAnnotations as $annot) {
-
-                        if ($annot instanceof \Mmoreramerino\GearmanBundle\Driver\Gearman\Work) {
-                            $workerCollection->add(new Worker($annot, $reflClass, $reader, $this->getSettings()));
-                        }
-                    }
-                }
-            }
-
-            $gearmanCache   ->set($workerCollection->__toCache())
-                            ->save();
+            $gearmanCacheLoader = $this->container->get('gearman.cache.loader');
+            $gearmanCacheLoader->load($gearmanCache);
         }
     }
 }

+ 0 - 67
Module/GearmanBaseBundle.php

@@ -13,73 +13,6 @@ use Symfony\Component\DependencyInjection\ContainerBuilder;
  */
 class GearmanBaseBundle extends Bundle
 {
-    /**
-     * Settings defined into settings file
-     *
-     * @var Array
-     */
-    private $settings = null;
-
-
-    /**
-     * Bundles available to perform search setted in bundles.yml file
-     *
-     * @var Array
-     */
-    private $bundles = null;
-
-
-    /**
-     * Return Gearman settings
-     *
-     * @return array Settings getted from gearmanSettings service
-     */
-    public function getSettings()
-    {
-        return $this->loadSettings();
-    }
-
-    /**
-     * Get yaml file and load all settings for Gearman engine
-     *
-     * @return array Settings
-     */
-    public function loadSettings()
-    {
-        $this->settings = $this->container->get('gearman.settings')->loadSettings();
-        return $this->settings;
-    }
-
-
-    /**
-     * Return Gearman bundle settings, previously loaded by method load()
-     * If settings are not loaded, a SettingsNotLoadedException Exception is thrown
-     *
-     * @return array Bundles that gearman will be able to search annotations
-     */
-    public function getParseableBundles()
-    {
-        if (null === $this->settings) {
-            $this->loadSettings();
-        }
-
-        if (null === $this->bundles) {
-            $this->bundles = array();
-
-            foreach ($this->settings['bundles'] as $properties) {
-
-                if ( isset($properties['active']) && (true === $properties['active']) ) {
-
-                    if ('' !== $properties['namespace']) {
-                        $this->bundles[] = $properties['namespace'];
-                    }
-                }
-            }
-        }
-
-        return $this->bundles;
-    }
-
     /**
      * Shutdowns the Bundle.
      *

+ 10 - 6
Project/Reportings/codesniffer.log

@@ -6,7 +6,9 @@ Time: 0 seconds, Memory: 4.50Mb
 
 Time: 0 seconds, Memory: 5.25Mb
 
-Time: 1 second, Memory: 4.75Mb
+Time: 0 seconds, Memory: 5.75Mb
+
+Time: 0 seconds, Memory: 4.75Mb
 
 Time: 0 seconds, Memory: 5.00Mb
 
@@ -22,17 +24,17 @@ Time: 0 seconds, Memory: 5.50Mb
 
 Time: 0 seconds, Memory: 4.75Mb
 
-Time: 1 second, Memory: 4.50Mb
+Time: 0 seconds, Memory: 4.50Mb
 
-Time: 0 seconds, Memory: 5.25Mb
+Time: 0 seconds, Memory: 4.50Mb
 
 Time: 0 seconds, Memory: 6.00Mb
 
 Time: 0 seconds, Memory: 4.50Mb
 
-Time: 0 seconds, Memory: 4.50Mb
+Time: 1 second, Memory: 4.50Mb
 
-Time: 0 seconds, Memory: 5.50Mb
+Time: 0 seconds, Memory: 4.75Mb
 
 Time: 0 seconds, Memory: 4.50Mb
 
@@ -40,7 +42,7 @@ Time: 0 seconds, Memory: 4.50Mb
 
 Time: 0 seconds, Memory: 4.50Mb
 
-Time: 1 second, Memory: 4.50Mb
+Time: 0 seconds, Memory: 4.50Mb
 
 Time: 0 seconds, Memory: 4.50Mb
 
@@ -50,5 +52,7 @@ Time: 0 seconds, Memory: 5.25Mb
 
 Time: 0 seconds, Memory: 4.75Mb
 
+Time: 1 second, Memory: 5.00Mb
+
 Time: 0 seconds, Memory: 4.75Mb
 

+ 12 - 7
Resources/config/services.yml

@@ -1,26 +1,31 @@
 parameters:
-    config.path:                /config/Gearman/
-  
-    gearman.client.class:              Mmoreramerino\GearmanBundle\Service\GearmanClient
+    config.path:                /config/gearman/
+
+    gearman.client.class:       Mmoreramerino\GearmanBundle\Service\GearmanClient
     gearman.cache.class:        Mmoreramerino\GearmanBundle\Service\GearmanCache
+    gearman.cache.loader.class: Mmoreramerino\GearmanBundle\Service\GearmanCacheLoader
     gearman.execute.job.class:  Mmoreramerino\GearmanBundle\Service\GearmanExecute
     gearman.settings.class:     Mmoreramerino\GearmanBundle\Service\GearmanSettings
 services:
-    gearman: 
+    gearman:
         class: %gearman.client.class%
         calls:
           - [setContainer,  [@service_container]]
-    gearman.settings: 
+    gearman.settings:
         class: %gearman.settings.class%
         calls:
           - [setContainer,  [@service_container]]
           - [loadSettings,  []]
-    gearman.cache: 
+    gearman.cache:
         class: %gearman.cache.class%
         calls:
           - [setContainer,  [@service_container]]
           - [loadCache,  []]
-    gearman.execute.job: 
+    gearman.cache.loader:
+        class: %gearman.cache.loader.class%
+        calls:
+          - [setContainer,  [@service_container]]
+    gearman.execute.job:
         class: %gearman.execute.job.class%
         calls:
           - [setContainer,  [@service_container]]

+ 3 - 4
Service/GearmanCache.php

@@ -102,12 +102,11 @@ class GearmanCache extends ContainerAware
      * Save data into cache
      * Returns self object
      *
-     * @return GearmanCache
+     * @return boolean Return if saved
      */
     public function save()
     {
-        file_put_contents($this->cachedir . $this->cachefile, $this->data);
-        return $this;
+        return file_put_contents($this->cachedir . $this->cachefile, $this->data);
     }
 
     /**
@@ -146,7 +145,7 @@ class GearmanCache extends ContainerAware
     {
         if (null === $this->cachedir) {
             $rootDir = $this->container->get('kernel')->getRootDir();
-            $this->cachedir = $rootDir . '/cache/'.$this->container->get('kernel')->getEnvironment().'/Gearman/';
+            $this->cachedir = $rootDir . '/cache/'.$this->container->get('kernel')->getEnvironment().'/gearman/';
         }
 
         return $this->cachedir;

+ 130 - 0
Service/GearmanCacheLoader.php

@@ -0,0 +1,130 @@
+<?php
+
+namespace Mmoreramerino\GearmanBundle\Service;
+
+use Symfony\Component\Config\FileLocator;
+use Doctrine\Common\Annotations\AnnotationReader;
+use Doctrine\Common\Annotations\AnnotationRegistry;
+use Mmoreramerino\GearmanBundle\Service\GearmanCache;
+use Mmoreramerino\GearmanBundle\Module\WorkerCollection;
+use Symfony\Component\DependencyInjection\ContainerAware;
+use Mmoreramerino\GearmanBundle\Module\WorkerDirectoryLoader;
+use Mmoreramerino\GearmanBundle\Module\WorkerClass as Worker;
+use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
+
+/**
+ * Gearman cache loader class
+ *
+ * @author Marc Morera <marc@ulabox.com>
+ */
+class GearmanCacheLoader extends ContainerAware
+{
+
+
+    /**
+     * Settings defined into settings file
+     *
+     * @var Array
+     */
+    private $settings = null;
+
+
+    /**
+     * Bundles available to perform search setted in bundles.yml file
+     *
+     * @var Array
+     */
+    private $bundles = null;
+
+    /**
+     * This method load all data and saves all annotations into cache.
+     * Also, it load all settings from Yaml file format
+     *
+     * @param GearmanCache $cache Cache object to perform saving
+     *
+     * @return boolean Return result of saving result on cache
+     */
+    public function load(GearmanCache $cache)
+    {
+        $reader = new AnnotationReader();
+        AnnotationRegistry::registerFile(__DIR__ . "/../Driver/Gearman/GearmanAnnotations.php");
+        $reader->setDefaultAnnotationNamespace('Mmoreramerino\GearmanBundle\Driver\\');
+        $workerCollection = new WorkerCollection;
+        $bundles = $this->container->get('kernel')->getBundles();
+
+        foreach ($bundles as $bundle) {
+
+            if (!\in_array($bundle->getNamespace(), $this->getParseableBundles())) {
+                continue;
+            }
+            $filesLoader = new WorkerDirectoryLoader(new FileLocator('.'));
+            $files = $filesLoader->load($bundle->getPath());
+
+            foreach ($files as $file) {
+                $reflClass = new \ReflectionClass($file['class']);
+                $classAnnotations = $reader->getClassAnnotations($reflClass);
+
+                foreach ($classAnnotations as $annot) {
+
+                    if ($annot instanceof \Mmoreramerino\GearmanBundle\Driver\Gearman\Work) {
+                        $workerCollection->add(new Worker($annot, $reflClass, $reader, $this->getSettings()));
+                    }
+                }
+            }
+        }
+
+        return $cache   ->set($workerCollection->__toCache())
+                        ->save();
+    }
+
+    /**
+     * Return Gearman bundle settings, previously loaded by method load()
+     * If settings are not loaded, a SettingsNotLoadedException Exception is thrown
+     *
+     * @return array Bundles that gearman will be able to search annotations
+     */
+    public function getParseableBundles()
+    {
+        if (null === $this->settings) {
+            $this->loadSettings();
+        }
+
+        if (null === $this->bundles) {
+            $this->bundles = array();
+
+            foreach ($this->settings['bundles'] as $properties) {
+
+                if ( isset($properties['active']) && (true === $properties['active']) ) {
+
+                    if ('' !== $properties['namespace']) {
+                        $this->bundles[] = $properties['namespace'];
+                    }
+                }
+            }
+        }
+
+        return $this->bundles;
+    }
+
+
+    /**
+     * Return Gearman settings
+     *
+     * @return array Settings getted from gearmanSettings service
+     */
+    public function getSettings()
+    {
+        return $this->loadSettings();
+    }
+
+    /**
+     * Get yaml file and load all settings for Gearman engine
+     *
+     * @return array Settings
+     */
+    public function loadSettings()
+    {
+        $this->settings = $this->container->get('gearman.settings')->loadSettings();
+        return $this->settings;
+    }
+}

+ 21 - 9
Service/GearmanExecute.php

@@ -36,15 +36,7 @@ class GearmanExecute extends GearmanService
         $gmworker= new \GearmanWorker();
         $job = $worker['job'];
 
-        if (is_array($job['servers'])) {
-
-            foreach ($job['servers'] as $server) {
-                list($addr, $port) = explode(':', $server, 2);
-                $gmworker->addServer($addr, $port);
-            }
-        } else {
-            $gmworker->addServer();
-        }
+        $this->addServers($gmworker, $job);
 
 
         if (null !== $worker['service']) {
@@ -72,4 +64,24 @@ class GearmanExecute extends GearmanService
             }
         }
     }
+
+    /**
+     * Adds into worker all defined Servers.
+     * If any is defined, performs default method
+     *
+     * @param \GearmanWorker $gmworker Worker to perform configuration
+     * @param array          $job      Job to check properties
+     */
+    private function addServers(\GearmanWorker $gmworker, Array $job)
+    {
+        if (is_array($job['servers'])) {
+
+            foreach ($job['servers'] as $server) {
+                list($addr, $port) = explode(':', $server, 2);
+                $gmworker->addServer($addr, $port);
+            }
+        } else {
+            $gmworker->addServer();
+        }
+    }
 }

+ 13 - 5
Service/GearmanSettings.php

@@ -45,18 +45,26 @@ class GearmanSettings extends ContainerAware
      */
     public function loadSettings()
     {
-        $settingsPath = $this->getFilePath();
-
-        if (!file_exists($settingsPath)) {
-            throw new NoSettingsFileExistsException($settingsPath);
+        if (!$this->existsSettings()) {
+            throw new NoSettingsFileExistsException($this->getFilePath());
         }
 
         $yaml = new Parser();
-        $this->settings = $yaml->parse(file_get_contents($settingsPath));
+        $this->settings = $yaml->parse(file_get_contents($this->getFilePath()));
 
         return $this->settings;
     }
 
+    /**
+     * Return if exists settings file
+     *
+     * @return boolean
+     */
+    public function existsSettings()
+    {
+        return file_exists($this->getFilePath());
+    }
+
     /**
      * Get settings file config from parameters and construct path
      *