Преглед на файлове

Merge pull request #152 from maksim-grib/master

Fixed issues introduced by PR #139
Marc Morera преди 10 години
родител
ревизия
8178f6789e
променени са 4 файла, в които са добавени 54 реда и са изтрити 30 реда
  1. 16 13
      DependencyInjection/Configuration.php
  2. 7 0
      Module/JobClass.php
  3. 10 3
      Module/WorkerClass.php
  4. 21 14
      Service/GearmanExecute.php

+ 16 - 13
DependencyInjection/Configuration.php

@@ -43,7 +43,7 @@ class Configuration implements ConfigurationInterface
                                 ->isRequired()
                                 ->cannotBeEmpty()
                             ->end()
-                            ->scalarNode('active')
+                            ->booleanNode('active')
                                 ->defaultFalse()
                             ->end()
                             ->arrayNode('include')
@@ -60,8 +60,8 @@ class Configuration implements ConfigurationInterface
                     ->defaultValue(array(
                         'localhost' =>  array(
                             'host'  =>  '127.0.0.1',
-                            'port'  =>  "4730",
-                        )
+                            'port'  =>  '4730',
+                        ),
                     ))
                     ->prototype('array')
                         ->children()
@@ -69,8 +69,8 @@ class Configuration implements ConfigurationInterface
                                 ->isRequired()
                                 ->cannotBeEmpty()
                             ->end()
-                            ->scalarNode('port')
-                                ->defaultValue("4730")
+                            ->integerNode('port')
+                                ->defaultValue('4730')
                             ->end()
                         ->end()
                     ->end()
@@ -78,29 +78,32 @@ class Configuration implements ConfigurationInterface
                 ->arrayNode('defaults')
                     ->addDefaultsIfNotSet()
                     ->children()
-                        ->scalarNode('iterations')
+                        ->integerNode('iterations')
+                            ->min(0)
                             ->defaultValue(0)
                         ->end()
                         ->scalarNode('method')
                             ->defaultValue('doNormal')
                         ->end()
-                        ->scalarNode('callbacks')
+                        ->booleanNode('callbacks')
                             ->defaultTrue()
                         ->end()
                         ->scalarNode('job_prefix')
                             ->defaultNull()
                         ->end()
-                        ->scalarNode('generate_unique_key')
+                        ->booleanNode('generate_unique_key')
                             ->defaultTrue()
                         ->end()
-                        ->scalarNode('workers_name_prepend_namespace')
+                        ->booleanNode('workers_name_prepend_namespace')
                             ->defaultTrue()
                         ->end()
-                        ->scalarNode('minimum_execution_time')
-                            ->defaultNull()
+                        ->integerNode('minimum_execution_time')
+                            ->min(0)
+                            ->defaultValue(0)
                         ->end()
-                        ->scalarNode('timeout')
-                            ->defaultNull()
+                        ->integerNode('timeout')
+                            ->min(0)
+                            ->defaultValue(0)
                         ->end()
                     ->end()
                 ->end()

+ 7 - 0
Module/JobClass.php

@@ -87,6 +87,13 @@ class JobClass extends ContainerAware
      */
     private $defaultMethod;
 
+    /**
+     * @var int
+     *
+     * Job minimum execution time
+     */
+    private $minimumExecutionTime;
+
     /**
      * @var int
      *

+ 10 - 3
Module/WorkerClass.php

@@ -97,7 +97,14 @@ class WorkerClass
     /**
      * @var int
      *
-     * Timeout for idle worker
+     * Job minimum execution time
+     */
+    private $minimumExecutionTime;
+
+    /**
+     * @var int
+     *
+     * Timeout for idle job
      */
     private $timeout;
 
@@ -248,7 +255,7 @@ class WorkerClass
      * If minimumExecutionTime is defined in JobAnnotation, this one is used.
      * Otherwise is used set in Class
      *
-     * @param JobAnnotation $jobAnnotation
+     * @param WorkAnnotation $workAnnotation
      * @param array $defaultSettings
      *
      * @return int
@@ -266,7 +273,7 @@ class WorkerClass
      * If timeout is defined in JobAnnotation, this one is used.
      * Otherwise is used set in Class
      *
-     * @param JobAnnotation $jobAnnotation
+     * @param WorkAnnotation $workAnnotation
      * @param array $defaultSettings
      *
      * @return int

+ 21 - 14
Service/GearmanExecute.php

@@ -75,12 +75,12 @@ class GearmanExecute extends AbstractGearmanService
             ->setDefaults(array(
                 'iterations'             => null,
                 'minimum_execution_time' => null,
-                'timeout'                => null
+                'timeout'                => null,
             ))
             ->setAllowedTypes(array(
                 'iterations'             => array('null', 'scalar'),
                 'minimum_execution_time' => array('null', 'scalar'),
-                'timeout'                => array('null', 'scalar')
+                'timeout'                => array('null', 'scalar'),
             ))
         ;
     }
@@ -132,7 +132,7 @@ class GearmanExecute extends AbstractGearmanService
      *
      * @param string $jobName Name of job to be executed
      * @param array $options Array of options passed to the callback
-     * @param GearmanWorker $gearmanWorker Worker instance to use
+     * @param \GearmanWorker $gearmanWorker Worker instance to use
      */
     public function executeJob($jobName, array $options = array(), \GearmanWorker $gearmanWorker = null)
     {
@@ -148,15 +148,17 @@ class GearmanExecute extends AbstractGearmanService
      *
      * @param array $worker Worker definition
      * @param array $options Array of options passed to the callback
-     * @param GearmanWorker $gearmanWorker Worker instance to use
+     * @param \GearmanWorker $gearmanWorker Worker instance to use
+     *
+     * @throws ServerConnectionException if a connection to a server was not possible.
+     *
      * @return GearmanExecute self Object
      */
     private function callJob(Array $worker, array $options = array(), \GearmanWorker $gearmanWorker = null)
     {
-        if(is_null($gearmanWorker)){
+        if(is_null($gearmanWorker)) {
             $gearmanWorker = new \GearmanWorker;
         }
-        $minimumExecutionTime = null;
 
         if (isset($worker['job'])) {
 
@@ -164,7 +166,7 @@ class GearmanExecute extends AbstractGearmanService
             $iterations = $worker['job']['iterations'];
             $minimumExecutionTime = $worker['job']['minimumExecutionTime'];
             $timeout = $worker['job']['timeout'];
-            $this->addServers($gearmanWorker, $worker['job']['servers'], $successes);
+            $successes = $this->addServers($gearmanWorker, $worker['job']['servers']);
 
         } else {
 
@@ -172,7 +174,7 @@ class GearmanExecute extends AbstractGearmanService
             $iterations = $worker['iterations'];
             $minimumExecutionTime = $worker['minimumExecutionTime'];
             $timeout = $worker['timeout'];
-            $this->addServers($gearmanWorker, $worker['servers'], $successes);
+            $successes = $this->addServers($gearmanWorker, $worker['servers']);
         }
 
         $options = $this->executeOptionsResolver->resolve($options);
@@ -182,7 +184,9 @@ class GearmanExecute extends AbstractGearmanService
         $timeout              = $options['timeout']                ?: $timeout;
 
         if (count($successes) < 1) {
-            sleep($minimumExecutionTime);
+            if ($minimumExecutionTime > 0) {
+                sleep($minimumExecutionTime);
+            }
             throw new ServerConnectionException('Worker was unable to connect to any server.');
         }
 
@@ -197,7 +201,7 @@ class GearmanExecute extends AbstractGearmanService
         /**
          * If there is a minimum expected duration, wait out the remaining period if there is any.
          */
-        if (null !== $minimumExecutionTime) {
+        if ($minimumExecutionTime > 0) {
             $now = time();
             $remaining = $minimumExecutionTime - ($now - $time);
 
@@ -254,6 +258,7 @@ class GearmanExecute extends AbstractGearmanService
      * @param Object         $objInstance   Job instance
      * @param array          $jobs          Array of jobs to subscribe
      * @param integer        $iterations    Number of iterations
+     * @param integer        $timeout       Timeout
      *
      * @return GearmanExecute self Object
      */
@@ -285,9 +290,9 @@ class GearmanExecute extends AbstractGearmanService
         /**
          * If iterations value is 0, is like worker will never die
          */
-        $alive = (0 == $iterations);
+        $alive = (0 === $iterations);
 
-        if (null !== $timeout) {
+        if ($timeout > 0) {
             $gearmanWorker->setTimeout($timeout * 1000);
         }
 
@@ -325,10 +330,12 @@ class GearmanExecute extends AbstractGearmanService
      * @param array          $servers  Servers array
      *
      * @throws ServerConnectionException if a connection to a server was not possible.
+     *
+     * @return array         Successfully added servers
      */
-    private function addServers(\GearmanWorker $gmworker, Array $servers, array &$successes = null)
+    private function addServers(\GearmanWorker $gmworker, array $servers)
     {
-        $successes = $successes ?: null;
+        $successes = array();
 
         if (!empty($servers)) {