浏览代码

Merge pull request #119 from orolin/worker_output

Added GearmanOutputAwareInterface for worker output
Marc Morera 11 年之前
父节点
当前提交
d546de0118

+ 1 - 0
Command/GearmanJobExecuteCommand.php

@@ -184,6 +184,7 @@ class GearmanJobExecuteCommand extends AbstractGearmanCommand
 
         $this
             ->gearmanExecute
+            ->setOutput($output)
             ->executeJob($job);
     }
 }

+ 1 - 0
Command/GearmanWorkerExecuteCommand.php

@@ -183,6 +183,7 @@ class GearmanWorkerExecuteCommand extends AbstractGearmanCommand
 
         $this
             ->gearmanExecute
+            ->setOutput($output)
             ->executeWorker($worker);
     }
 }

+ 32 - 0
Command/Util/GearmanOutputAwareInterface.php

@@ -0,0 +1,32 @@
+<?php
+
+/**
+ * Gearman Bundle for Symfony2
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * Feel free to edit as you please, and have fun.
+ *
+ * @author Marc Morera <yuhu@mmoreram.com>
+ * @author Dominic Grostate <codekestrel@googlemail.com>
+ */
+
+namespace Mmoreram\GearmanBundle\Command\Util;
+
+use Symfony\Component\Console\Output\OutputInterface;
+
+/**
+ * Interface GearmanOutputAwareInterface
+ *
+ * @since 2.4.2
+ */
+interface GearmanOutputAwareInterface
+{
+    /**
+     * Set the output
+     *
+     * @param OutputInterface $output
+     */
+    public function setOutput(OutputInterface $output);
+}

+ 70 - 1
Resources/docs/definition_workers.rst

@@ -126,6 +126,8 @@ variable in Worker annotation.
 
     namespace Acme\AcmeBundle\Services;
 
+    use Mmoreram\GearmanBundle\Driver\Gearman;
+
     /**
      * @Gearman\Work(
      *     service="myServiceName"
@@ -162,4 +164,71 @@ And have this service defined in your dependency injection definition file
              class: Acme\AcmeBundle\Services\AcmeService
              arguments:
                 event_dispatcher: @event_dispatcher
-                mailer: @mailer
+                mailer: @mailer
+
+Console output from workers
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+If you need your worker to output information to the console, you can have your worker class implement `Mmoreram\\GearmanBundle\\Command\\Util\\GearmanOutputAwareInterface`.
+
+This interface requires a single method be implemented `public function setOutput(OutputInterface $output);`.
+To avoid needing to check the output is available, you can by default set it to an instance of `Symfony\\Component\\Console\\Output\\NullOutput`.
+
+.. code-block:: php
+
+    namespace Acme\AcmeBundle\Services;
+
+    use Symfony\Component\Console\Output\NullOutput;
+    use Mmoreram\GearmanBundle\Command\Util\GearmanOutputAwareInterface;
+    use Mmoreram\GearmanBundle\Driver\Gearman;
+
+    /**
+     * @Gearman\Work(
+     *     iterations = 3,
+     *     description = "Worker test description",
+     *     defaultMethod = "doBackground"
+     * )
+     */
+    class AcmeWorker implements GearmanOutputAwareInterface
+    {
+        /**
+         * @var OutputInterface
+         */
+        protected $output;
+
+        /**
+         * Constructor
+         */
+        public function __construct()
+        {
+            $this->output = new NullOutput();
+        }
+
+        /**
+         * @param OutputInterface $output
+         */
+        public function setOutput(OutputInterface $output)
+        {
+            $this->output = $output;
+        }
+
+        /**
+         * Test method to run as a job with console output
+         *
+         * @param \GearmanJob $job Object with job parameters
+         *
+         * @return boolean
+         *
+         * @Gearman\Job(
+         *     iterations = 3,
+         *     name = "test",
+         *     description = "This is a description"
+         * )
+         */
+        public function testA(\GearmanJob $job)
+        {
+            $this->output->writeln('Job testA done!');
+
+            return true;
+        }
+    }

+ 33 - 0
Service/GearmanExecute.php

@@ -13,10 +13,13 @@
 
 namespace Mmoreram\GearmanBundle\Service;
 
+use Symfony\Component\Console\Output\NullOutput;
+use Symfony\Component\Console\Output\OutputInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 
+use Mmoreram\GearmanBundle\Command\Util\GearmanOutputAwareInterface;
 use Mmoreram\GearmanBundle\Service\Abstracts\AbstractGearmanService;
 use Mmoreram\GearmanBundle\Event\GearmanWorkExecutedEvent;
 use Mmoreram\GearmanBundle\GearmanEvents;
@@ -42,6 +45,13 @@ class GearmanExecute extends AbstractGearmanService
      */
     protected $eventDispatcher;
 
+    /**
+     * @var OutputInterface
+     *
+     * Output instance
+     */
+    protected $output;
+
     /**
      * Set container
      *
@@ -57,6 +67,8 @@ class GearmanExecute extends AbstractGearmanService
     }
 
     /**
+     * Set event dispatcher
+     *
      * @param EventDispatcherInterface $eventDispatcher
      *
      * @return GearmanExecute self Object
@@ -68,6 +80,20 @@ class GearmanExecute extends AbstractGearmanService
         return $this;
     }
 
+    /**
+     * Set output
+     *
+     * @param OutputInterface $output
+     *
+     * @return GearmanExecute self Object
+     */
+    public function setOutput(OutputInterface $output)
+    {
+        $this->output = $output;
+
+        return $this;
+    }
+
     /**
      * Executes a job given a jobName and given settings and annotations of job
      *
@@ -164,6 +190,13 @@ class GearmanExecute extends AbstractGearmanService
     private function runJob(\GearmanWorker $gearmanWorker, $objInstance, array $jobs, $iterations)
     {
 
+        /**
+         * Set the output of this instance, this should allow workers to use the console output.
+         */
+        if($objInstance instanceof GearmanOutputAwareInterface) {
+            $objInstance->setOutput($this->output ? : new NullOutput());
+        }
+
         /**
          * Every job defined in worker is added into GearmanWorker
          */