Explorar o código

Added some features:

* Added Work annotation to support Service Jobs
* Support to Set server when calling Job
Marc %!s(int64=13) %!d(string=hai) anos
pai
achega
5dcd9be0d6

+ 8 - 0
Driver/Gearman/GearmanAnnotations.php

@@ -39,6 +39,14 @@ final class Work extends Annotation
      * @var mixed
      */
     public $servers = null;
+    
+    /**
+     * Service typeof Class. If it's defined, object will be instanced throught service dependence injection.
+     * Otherwise, class will be instance with new() method
+     * 
+     * @var string
+     */
+    public $service = null;
 }
 
 /** @Annotation */

+ 1 - 3
MmoreramerinoGearmanBundle.php

@@ -32,11 +32,10 @@ class MmoreramerinoGearmanBundle extends GearmanBaseBundle
         if (!in_array('gearman', get_loaded_extensions())) {
             throw new GearmanNotInstalledException;
         }
-
         $gearmanCache = $this->container->get('gearman.cache');
         $existsCache = $gearmanCache->existsCacheFile();
 
-        if (in_array($this->container->get('kernel')->getEnvironment(), array('dev', 'test')) || !$existsCache) {
+        if (in_array($this->container->get('kernel')->getEnvironment(), array('back_dev', 'back_test')) || !$existsCache) {
 
             if ($existsCache) {
                 $gearmanCache->emptyCache();
@@ -52,7 +51,6 @@ class MmoreramerinoGearmanBundle extends GearmanBaseBundle
                 if (!\in_array($bundle->getNamespace(), $this->getParseableBundles())) {
                     continue;
                 }
-
                 $filesLoader = new WorkerDirectoryLoader(new FileLocator('.'));
                 $files = $filesLoader->load($bundle->getPath());
 

+ 2 - 0
Module/WorkerClass.php

@@ -59,6 +59,7 @@ class WorkerClass
 
         $this->fileName = $reflectionClass->getFileName();
         $this->className = $reflectionClass->getName();
+        $this->service = $classAnnotation->service;
 
         $this->jobCollection = new JobCollection;
 
@@ -86,6 +87,7 @@ class WorkerClass
             'fileName'      =>  $this->fileName,
             'callableName'  =>  $this->callableName,
             'description'   =>  $this->description,
+            'service'       =>  $this->service,
             'jobs'          =>  array(),
         );
 

+ 27 - 1
Service/Gearman.php

@@ -12,6 +12,14 @@ use Mmoreramerino\GearmanBundle\Service\GearmanInterface;
  */
 class Gearman extends GearmanService implements GearmanInterface
 {
+    
+    /**
+     * Server variable to define in what server must connect to
+     *
+     * @var array
+     */
+    public $server = null;
+    
     /**
      * If workers are not loaded, they're loaded and returned.
      * Otherwise, they are simply returned
@@ -152,8 +160,26 @@ class Gearman extends GearmanService implements GearmanInterface
     private function doEnqueue(Array $worker, $params='', $method='do')
     {
         $gmclient= new \GearmanClient();
-        $gmclient->addServer();
+        
+        if (null === $this->server || !is_array($this->server)) {
+            $gmclient->addServer();
+        } else {
+            $gmclient->addServer($this->server[0], $this->server[1]);
+        }
 
         return $gmclient->$method($worker['job']['realCallableName'], serialize($params));
     }
+    
+    /**
+     *
+     * @param type $servername Server name (must be ip)
+     * @param type $port       Port of server. By default 4730
+     * 
+     * @return Gearman Returns self object
+     */
+    public function setServer($servername, $port = 4730)
+    {
+        $this->server = array($servername, $port);
+        return $this;
+    }
 }

+ 9 - 1
Service/GearmanExecute.php

@@ -46,7 +46,15 @@ class GearmanExecute extends GearmanService
         } else {
             $gmworker->addServer();
         }
-        $gmworker->addFunction($job['realCallableName'], array(new $worker['className'], $job['methodName']));
+        
+        
+        if (null !== $worker['service']) {
+            $objInstance = $this->container->get($worker['service']);
+        } else {
+            $objInstance = new $worker['className'];
+        }
+        
+        $gmworker->addFunction($job['realCallableName'], array($objInstance, $job['methodName']));
 
         $iter = isset($job['iter']) ? (int) ($job['iter']) : 0;
         $shouldStop = ($iter > 0) ? true : false;