Переглянути джерело

Added some functs, checked describer, Added annotations, added exception and modified testWorker

Marc 13 роки тому
батько
коміт
c90f20ea5e

+ 1 - 1
Command/GearmanClientExecuteCommand.php

@@ -40,6 +40,6 @@ class GearmanClientExecuteCommand extends ContainerAwareCommand
     protected function execute(InputInterface $input, OutputInterface $output)
     {
         $job = $input->getArgument('job');
-        $this->getContainer()->get('gearman')->doJob($job);
+        $this->getContainer()->get('gearman')->callJob($job);
     }
 }

+ 1 - 24
Command/GearmanJobDescribeCommand.php

@@ -41,29 +41,6 @@ class GearmanJobDescribeCommand extends ContainerAwareCommand
     {
         $job = $input->getArgument('job');
         $worker = $this->getContainer()->get('gearman')->getWorker($job);
-
-        $output->writeln('');
-        $output->writeln('<info>    @Worker\className : '.$worker['className'].'</info>');
-        $output->writeln('<info>    @Worker\fileName : '.$worker['fileName'].'</info>');
-        $output->writeln('<info>    @Worker\namespace : '.$worker['namespace'].'</info>');
-        $output->writeln('<info>    @Worker-jobsnumber : '.count($worker['jobs']).'</info>');
-        $output->writeln('<info>    @Worker\description :</info>');
-        $output->writeln('');
-        $output->writeln('<comment>        '.$worker['description'].'</comment>');
-        $output->writeln('');
-        $job = $worker['job'];
-        $output->writeln('<info>    @job\methodName : '.$job['methodName'].'</info>');
-        $output->writeln('<info>    @job\callableName : '.$job['realCallableName'].'</info>');
-        $output->writeln('<info>    @job\iterations : '.$job['iterations'].'</info>');
-        $output->writeln('<info>    @job\servers :</info>');
-        $output->writeln('');
-        foreach ($job['servers'] as $name => $server) {
-            $output->writeln('<comment>        '.$name.' - '.$server.'</comment>');
-        }
-        $output->writeln('');
-        $output->writeln('<info>    @job\description :</info>');
-        $output->writeln('');
-        $output->writeln('<comment>        '.$job['description'].'</comment>');
-        $output->writeln('');
+        $this->getContainer()->get('gearman.describer')->describeJob($output, $worker);
     }
 }

+ 4 - 0
Command/GearmanJobExecuteCommand.php

@@ -43,8 +43,12 @@ class GearmanJobExecuteCommand extends ContainerAwareCommand
         if (!$input->getOption('no-interaction') && !$dialog->askConfirmation($output, '<question>This will execute asked worker?</question>', 'y')) {
             return;
         }
+        $output->writeln('<info>loading...</info>');
 
         $job = $input->getArgument('job');
+        $worker = $this->getContainer()->get('gearman')->getWorker($job);
+        $this->getContainer()->get('gearman.describer')->describeJob($output, $worker);
+        $output->writeln('<info>loaded. Ctrl+C to break</info>');
         $this->getContainer()->get('gearman.execute.job')->executeJob($job);
     }
 }

+ 6 - 3
Command/GearmanJobListCommand.php

@@ -39,15 +39,18 @@ class GearmanJobListCommand extends ContainerAwareCommand
     protected function execute(InputInterface $input, OutputInterface $output)
     {
         $workers = $this->getContainer()->get('gearman')->getWorkers();
-
+        $it = 1;
         if (is_array($workers)) {
 
             foreach ($workers as $worker) {
                 $output->writeln('<info>    @'.$worker['className'].'</info>');
-
+                $output->writeln('<info></info>');
                 foreach ($worker['jobs'] as $job) {
-                    $output->writeln('<comment>      # '.$job['realCallableName'].'</comment>');
+                    $output->writeln('<comment>      - #'.$it++.'</comment>');
+                    $output->writeln('<comment>          name: '.$job['methodName'].'</comment>');
+                    $output->writeln('<comment>          callablename:</comment><info> '.$job['realCallableName'].'</info>');
                 }
+                $output->writeln('');
             }
         }
     }

+ 14 - 0
Driver/Gearman/GearmanAnnotations.php

@@ -40,6 +40,13 @@ final class Work extends Annotation
      */
     public $servers = null;
 
+    /**
+     * Default method to call for all jobs inside this work
+     *
+     * @var string
+     */
+    public $defaultMethod = null;
+
     /**
      * Service typeof Class. If it's defined, object will be instanced throught service dependence injection.
      * Otherwise, class will be instance with new() method
@@ -79,4 +86,11 @@ final class Job extends Annotation
      * @var mixed
      */
     public $servers = null;
+
+    /**
+     * Default method to call for this job
+     *
+     * @var string
+     */
+    public $defaultMethod = null;
 }

+ 25 - 0
Exceptions/NoCallableGearmanMethodException.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace Mmoreramerino\GearmanBundle\Exceptions;
+
+/**
+ * GearmanBundle can't find calling method
+ *
+ * @author Marc Morera <marc@ulabox.com>
+ */
+class NoCallableGearmanMethodException extends \Exception
+{
+
+    /**
+     * Construct method for Exception
+     *
+     * @param string     $calledMethod Called and not found method
+     * @param integer    $code         Code of exception
+     * @param \Exception $previous     Previos Exception
+     */
+    public function __construct($calledMethod='', $code = 0, \Exception $previous = null)
+    {
+        $message = 'GearmanBundle can\'t find "'.$calledMethod.'" method. Call "php app/console gearman:methods:list' . PHP_EOL;
+        parent::__construct($message, $code, $previous);
+    }
+}

+ 20 - 1
Module/JobClass.php

@@ -4,6 +4,7 @@ namespace Mmoreramerino\GearmanBundle\Module;
 
 use Mmoreramerino\GearmanBundle\Driver\Gearman\Job;
 use Mmoreramerino\GearmanBundle\Driver\Gearman\Work;
+use Symfony\Component\DependencyInjection\ContainerAware;
 use Mmoreramerino\GearmanBundle\Exceptions\SettingValueMissingException;
 use Mmoreramerino\GearmanBundle\Exceptions\SettingValueBadFormatException;
 
@@ -12,7 +13,7 @@ use Mmoreramerino\GearmanBundle\Exceptions\SettingValueBadFormatException;
  *
  * @author Marc Morera <marc@ulabox.com>
  */
-class JobClass
+class JobClass extends ContainerAware
 {
     /**
      * Callable name for this job
@@ -68,6 +69,23 @@ class JobClass
         }
         $this->iterations = $iter;
 
+
+        if (null !== $settings['defaults']['method']) {
+            $defaultMethod = ($settings['defaults']['method']);
+
+            if (null !== $classAnnotation->defaultMethod) {
+                $defaultMethod = ($classAnnotation->defaultMethod);
+            }
+
+            if (null !== $methodAnnotation->defaultMethod) {
+                $defaultMethod = ($methodAnnotation->defaultMethod);
+            }
+        } else {
+            throw new SettingValueMissingException('defaults/method');
+        }
+
+        $this->defaultMethod = $defaultMethod;
+
         /**
          * Servers definition for job
          */
@@ -119,6 +137,7 @@ class JobClass
             'description'           =>  $this->description,
             'iterations'			=>  $this->iterations,
             'servers'               =>  $this->servers,
+            'defaultMethod'         =>  $this->defaultMethod,
         );
     }
 }

+ 0 - 58
Project/Reportings/codesniffer.log

@@ -1,58 +0,0 @@
-Time: 0 seconds, Memory: 4.50Mb
-
-Time: 0 seconds, Memory: 4.00Mb
-
-Time: 0 seconds, Memory: 4.50Mb
-
-Time: 0 seconds, Memory: 5.25Mb
-
-Time: 0 seconds, Memory: 5.75Mb
-
-Time: 0 seconds, Memory: 4.75Mb
-
-Time: 0 seconds, Memory: 5.00Mb
-
-Time: 0 seconds, Memory: 5.00Mb
-
-Time: 0 seconds, Memory: 6.00Mb
-
-Time: 0 seconds, Memory: 5.75Mb
-
-Time: 0 seconds, Memory: 5.75Mb
-
-Time: 0 seconds, Memory: 5.50Mb
-
-Time: 0 seconds, Memory: 4.75Mb
-
-Time: 0 seconds, Memory: 4.50Mb
-
-Time: 0 seconds, Memory: 4.50Mb
-
-Time: 0 seconds, Memory: 6.00Mb
-
-Time: 0 seconds, Memory: 4.50Mb
-
-Time: 1 second, Memory: 4.50Mb
-
-Time: 0 seconds, Memory: 4.75Mb
-
-Time: 0 seconds, Memory: 4.50Mb
-
-Time: 0 seconds, Memory: 4.50Mb
-
-Time: 0 seconds, Memory: 4.50Mb
-
-Time: 0 seconds, Memory: 4.50Mb
-
-Time: 0 seconds, Memory: 4.50Mb
-
-Time: 0 seconds, Memory: 4.75Mb
-
-Time: 0 seconds, Memory: 5.25Mb
-
-Time: 0 seconds, Memory: 4.75Mb
-
-Time: 1 second, Memory: 5.00Mb
-
-Time: 0 seconds, Memory: 4.75Mb
-

+ 5 - 0
Resources/config/services.yml

@@ -6,6 +6,7 @@ parameters:
     gearman.cache.loader.class: Mmoreramerino\GearmanBundle\Service\GearmanCacheLoader
     gearman.execute.job.class:  Mmoreramerino\GearmanBundle\Service\GearmanExecute
     gearman.settings.class:     Mmoreramerino\GearmanBundle\Service\GearmanSettings
+    gearman.describer.class:     Mmoreramerino\GearmanBundle\Service\GearmanDescriber
 services:
     gearman:
         class: %gearman.client.class%
@@ -27,5 +28,9 @@ services:
           - [setContainer,  [@service_container]]
     gearman.execute.job:
         class: %gearman.execute.job.class%
+        calls:
+          - [setContainer,  [@service_container]]
+    gearman.describer:
+        class: %gearman.describer.class%
         calls:
           - [setContainer,  [@service_container]]

+ 24 - 0
Service/GearmanClient.php

@@ -4,6 +4,7 @@ namespace Mmoreramerino\GearmanBundle\Service;
 
 use Mmoreramerino\GearmanBundle\Service\GearmanService;
 use Mmoreramerino\GearmanBundle\Service\GearmanInterface;
+use Mmoreramerino\GearmanBundle\Exceptions\NoCallableGearmanMethodException;
 
 /**
  * Implementation of GearmanInterface
@@ -34,6 +35,29 @@ class GearmanClient extends GearmanService implements GearmanInterface
         return $this->setWorkers();
     }
 
+    /**
+     * Runs a single task and returns some result, depending of method called.
+     * Method called depends of default callable method setted on gearman settings
+     *  or overwritted on work or job annotations
+     *
+     * @param string $name   A GearmanBundle registered function the worker is to execute
+     * @param Mixed  $params Parameters to send to job
+     *
+     * @return mixed result depending of method called.
+     */
+     public function callJob($name, $params = array())
+     {
+        $worker = $this->getWorker($name);
+        $methodCallable = $worker['job']['defaultMethod'] . 'Job';
+
+        if (!method_exists($this, $methodCallable)) {
+            throw new NoCallableGearmanMethodException($methodCallable);
+        }
+
+        return $this->$methodCallable($name, $params);
+     }
+
+
 
     /**
      * Runs a single task and returns a string representation of the result.

+ 51 - 0
Service/GearmanDescriber.php

@@ -0,0 +1,51 @@
+<?php
+
+namespace Mmoreramerino\GearmanBundle\Service;
+
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\DependencyInjection\ContainerAware;
+
+/**
+ * Implementation of GearmanDescriber
+ *
+ * @author Marc Morera <marc@ulabox.com>
+ */
+class GearmanDescriber extends ContainerAware
+{
+
+    /**
+     * Describe Job.
+     *
+     * Given a output object and a Job, dscribe it.
+     *
+     * @param OutputInterface $output Output object
+     * @param array           $worker Worker array with Job to describe
+     */
+    public function describeJob(OutputInterface $output, array $worker)
+    {
+        $output->writeln('');
+        $output->writeln('<info>    @Worker\className : '.$worker['className'].'</info>');
+        $output->writeln('<info>    @Worker\fileName : '.$worker['fileName'].'</info>');
+        $output->writeln('<info>    @Worker\namespace : '.$worker['namespace'].'</info>');
+        $output->writeln('<info>    @Worker-jobsnumber : '.count($worker['jobs']).'</info>');
+        $output->writeln('<info>    @Worker\description :</info>');
+        $output->writeln('');
+        $output->writeln('<comment>        '.$worker['description'].'</comment>');
+        $output->writeln('');
+        $job = $worker['job'];
+        $output->writeln('<info>    @job\methodName : '.$job['methodName'].'</info>');
+        $output->writeln('<info>    @job\callableName : '.$job['realCallableName'].'</info>');
+        $output->writeln('<info>    @job\iterations : '.$job['iterations'].'</info>');
+        $output->writeln('<info>    @job\defaultMethod : '.$job['defaultMethod'].'</info>');
+        $output->writeln('<info>    @job\servers :</info>');
+        $output->writeln('');
+        foreach ($job['servers'] as $name => $server) {
+            $output->writeln('<comment>        '.$name.' - '.$server.'</comment>');
+        }
+        $output->writeln('');
+        $output->writeln('<info>    @job\description :</info>');
+        $output->writeln('');
+        $output->writeln('<comment>        '.$job['description'].'</comment>');
+        $output->writeln('');
+    }
+}

+ 2 - 2
Workers/testWorker.php

@@ -2,7 +2,7 @@
 
 namespace Mmoreramerino\GearmanBundle\Workers;
 
-/** @Gearman\Work(description="Worker test description") */
+/** @Gearman\Work(description="Worker test description", defaultMethod="doBackground") */
 class testWorker
 {
 
@@ -13,7 +13,7 @@ class testWorker
      *
      * @return boolean
      *
-     * @Gearman\Job(iterations=3, name="test", description="This is a description")     *
+     * @Gearman\Job(iterations=3, name="test", description="This is a description", defaultMethod="doHighBackground")     *
      */
     public function testA(\GearmanJob $job)
     {