浏览代码

Updated the bundle to support prefixes for jobs/tasks. This adds support for using a single gearmand server but have different environments for dev and prod.

Now you can optionally specify a job_prefix under the default settings. By default this will be null and have no affect on anything.
If you specify one such as 'dev' it will prepend all tasks and job.  This will require zero updates between live and dev environments of your code.
Also updated the describer and list commands to show the prefix is there is one in use.
Matt Daum 11 年之前
父节点
当前提交
5c7b117ee4

+ 5 - 1
Command/GearmanWorkerListCommand.php

@@ -59,7 +59,11 @@ class GearmanWorkerListCommand extends ContainerAwareCommand
                 foreach ($worker['jobs'] as $job) {
                 foreach ($worker['jobs'] as $job) {
                     $output->writeln('<comment>      - #'.$it++.'</comment>');
                     $output->writeln('<comment>      - #'.$it++.'</comment>');
                     $output->writeln('<comment>          name: '.$job['methodName'].'</comment>');
                     $output->writeln('<comment>          name: '.$job['methodName'].'</comment>');
-                    $output->writeln('<comment>          callablename:</comment><info> '.$job['realCallableName'].'</info>');
+                    $output->writeln('<comment>          callablename:</comment><info> '.$job['realCallableNameNoPrefix'].'</info>');
+
+                    if(false === is_null($job['jobPrefix'])){
+                        $output->writeln('<comment>          job prefix:</comment><info> '.$job['jobPrefix'].'</info>');
+                    }
                 }
                 }
                 $output->writeln('');
                 $output->writeln('');
             }
             }

+ 3 - 0
DependencyInjection/Configuration.php

@@ -80,6 +80,9 @@ class Configuration implements ConfigurationInterface
                         ->scalarNode('callbacks')
                         ->scalarNode('callbacks')
                             ->defaultValue(true)
                             ->defaultValue(true)
                         ->end()
                         ->end()
+                        ->scalarNode('job_prefix')
+                            ->defaultValue(null)
+                        ->end()
                     ->end()
                     ->end()
                 ->end()
                 ->end()
             ->end();
             ->end();

+ 28 - 10
Module/JobClass.php

@@ -46,6 +46,14 @@ class JobClass extends ContainerAware
      */
      */
     private $methodName;
     private $methodName;
 
 
+    /**
+     * @var string
+     *
+     * RealCallable name for this job without the job prefix
+     *
+     */
+    private $realCallableNameNoPrefix;
+
 
 
     /**
     /**
      * @var string
      * @var string
@@ -87,6 +95,13 @@ class JobClass extends ContainerAware
      */
      */
     private $servers;
     private $servers;
 
 
+    /**
+     * @var string
+     *
+     * The prefix to be prepended to all job callable names.
+     */
+    private $jobPrefix;
+
 
 
     /**
     /**
      * Construct method
      * Construct method
@@ -99,13 +114,15 @@ class JobClass extends ContainerAware
      */
      */
     public function __construct(JobAnnotation $jobAnnotation, ReflectionMethod $reflectionMethod, $callableNameClass, array $servers, array $defaultSettings)
     public function __construct(JobAnnotation $jobAnnotation, ReflectionMethod $reflectionMethod, $callableNameClass, array $servers, array $defaultSettings)
     {
     {
+
         $this->callableName = is_null($jobAnnotation->name)
         $this->callableName = is_null($jobAnnotation->name)
                             ? $reflectionMethod->getName()
                             ? $reflectionMethod->getName()
                             : $jobAnnotation->name;
                             : $jobAnnotation->name;
 
 
         $this->methodName = $reflectionMethod->getName();
         $this->methodName = $reflectionMethod->getName();
-
-        $this->realCallableName = str_replace('\\', '', $callableNameClass . '~' . $this->callableName);
+        $this->realCallableNameNoPrefix = str_replace('\\', '', $callableNameClass . '~' . $this->callableName);
+        $this->jobPrefix = $defaultSettings['jobPrefix'];
+        $this->realCallableName = $this->jobPrefix . $this->realCallableNameNoPrefix;
         $this->description  = is_null($jobAnnotation->description)
         $this->description  = is_null($jobAnnotation->description)
                             ? self::DEFAULT_DESCRIPTION
                             ? self::DEFAULT_DESCRIPTION
                             : $jobAnnotation->description;
                             : $jobAnnotation->description;
@@ -193,14 +210,15 @@ class JobClass extends ContainerAware
     {
     {
         return array(
         return array(
 
 
-            'callableName'          =>  $this->callableName,
-            'methodName'            =>  $this->methodName,
-            'realCallableName'      =>  $this->realCallableName,
-            'description'           =>  $this->description,
-
-            'iterations'			=>  $this->iterations,
-            'servers'               =>  $this->servers,
-            'defaultMethod'         =>  $this->defaultMethod,
+            'callableName'                  =>  $this->callableName,
+            'methodName'                    =>  $this->methodName,
+            'realCallableName'              =>  $this->realCallableName,
+            'jobPrefix'                     =>  $this->jobPrefix,
+            'realCallableNameNoPrefix'      =>  $this->realCallableNameNoPrefix,
+            'description'                   =>  $this->description,
+            'iterations'			        =>  $this->iterations,
+            'servers'                       =>  $this->servers,
+            'defaultMethod'                 =>  $this->defaultMethod,
         );
         );
     }
     }
 }
 }

+ 15 - 3
Module/WorkerClass.php

@@ -114,6 +114,14 @@ class WorkerClass
     private $jobCollection;
     private $jobCollection;
 
 
 
 
+    /**
+     * The prefix for all job names
+     *
+     * @var string $jobPrefix
+     */
+    private $jobPrefix = null;
+
+
     /**
     /**
      * Retrieves all jobs available from worker
      * Retrieves all jobs available from worker
      *
      *
@@ -148,10 +156,15 @@ class WorkerClass
         $this->className = $reflectionClass->getName();
         $this->className = $reflectionClass->getName();
         $this->service = $workAnnotation->service;
         $this->service = $workAnnotation->service;
 
 
+        if(isset($defaultSettings['job_prefix']))
+            $this->jobPrefix = $defaultSettings['job_prefix'];
+
         $this->servers = $this->loadServers($workAnnotation, $servers);
         $this->servers = $this->loadServers($workAnnotation, $servers);
         $this->iterations = $this->loadIterations($workAnnotation, $defaultSettings);
         $this->iterations = $this->loadIterations($workAnnotation, $defaultSettings);
         $this->defaultMethod = $this->loadDefaultMethod($workAnnotation, $defaultSettings);
         $this->defaultMethod = $this->loadDefaultMethod($workAnnotation, $defaultSettings);
         $this->jobCollection = $this->createJobCollection($reflectionClass, $reader);
         $this->jobCollection = $this->createJobCollection($reflectionClass, $reader);
+
+
     }
     }
 
 
 
 
@@ -249,15 +262,14 @@ class WorkerClass
             foreach ($methodAnnotations as $methodAnnotation) {
             foreach ($methodAnnotations as $methodAnnotation) {
 
 
                 /**
                 /**
-                 * Annotation is only laoded if is typeof JobAnnotation
+                 * Annotation is only loaded if is typeof JobAnnotation
                  */
                  */
                 if ($methodAnnotation instanceof JobAnnotation) {
                 if ($methodAnnotation instanceof JobAnnotation) {
-
                     /**
                     /**
                      * Creates new Job
                      * Creates new Job
                      */
                      */
                     $job = new Job($methodAnnotation, $reflectionMethod, $this->callableName, $this->servers, array(
                     $job = new Job($methodAnnotation, $reflectionMethod, $this->callableName, $this->servers, array(
-
+                        'jobPrefix'     =>  $this->jobPrefix,
                         'iterations'    =>  $this->iterations,
                         'iterations'    =>  $this->iterations,
                         'method'        =>  $this->defaultMethod,
                         'method'        =>  $this->defaultMethod,
                     ));
                     ));

+ 1 - 0
Resources/config/services.yml

@@ -27,6 +27,7 @@ services:
         abstract:  true
         abstract:  true
         arguments:
         arguments:
             gearman.cache.wrapper: @gearman.cache.wrapper
             gearman.cache.wrapper: @gearman.cache.wrapper
+            default_settings: %gearman.default.settings%
 
 
     gearman.execute:
     gearman.execute:
         class: %gearman.execute.class%
         class: %gearman.execute.class%

+ 16 - 1
Service/Abstracts/AbstractGearmanService.php

@@ -29,14 +29,27 @@ abstract class AbstractGearmanService
     protected $workers;
     protected $workers;
 
 
 
 
+    /**
+     * The prefix for all job names
+     *
+     * @var string $jobPrefix
+     */
+    protected $jobPrefix = null;
+
+
     /**
     /**
      * Construct method
      * Construct method
      *
      *
      * @param GearmanCacheWrapper $gearmanCacheWrapper GearmanCacheWrapper
      * @param GearmanCacheWrapper $gearmanCacheWrapper GearmanCacheWrapper
+     * @param array $defaultSettings The default settings for the bundle.
+     *
      */
      */
-    public function __construct(GearmanCacheWrapper $gearmanCacheWrapper)
+    public function __construct(GearmanCacheWrapper $gearmanCacheWrapper,array $defaultSettings)
     {
     {
         $this->workers = $gearmanCacheWrapper->getWorkers();
         $this->workers = $gearmanCacheWrapper->getWorkers();
+
+        if(isset($defaultSettings['job_prefix']))
+            $this->jobPrefix = $defaultSettings['job_prefix'];
     }
     }
 
 
 
 
@@ -50,6 +63,8 @@ abstract class AbstractGearmanService
      */
      */
     public function getJob($jobName)
     public function getJob($jobName)
     {
     {
+        $jobName = $this->jobPrefix . $jobName;
+
         foreach ($this->workers as $worker) {
         foreach ($this->workers as $worker) {
 
 
             if (is_array($worker['jobs'])) {
             if (is_array($worker['jobs'])) {

+ 8 - 1
Service/GearmanDescriber.php

@@ -130,7 +130,14 @@ class GearmanDescriber
             $output->writeln('<info>    @Worker\jobs</info>');
             $output->writeln('<info>    @Worker\jobs</info>');
             $output->writeln('');
             $output->writeln('');
             foreach ($worker['jobs'] as $job) {
             foreach ($worker['jobs'] as $job) {
-                $output->writeln('<comment>        # ' . $job['realCallableName'] . '</comment>');
+
+                if(false === is_null($job['jobPrefix'])){
+                    $output->writeln('<comment>        # ' . $job['realCallableNameNoPrefix'] . ' with prefix: '.$job['jobPrefix'].'</comment>');
+                }
+                else{
+                    $output->writeln('<comment>        # ' . $job['realCallableNameNoPrefix'] . ' </comment>');
+                }
+
             }
             }
         }
         }