瀏覽代碼

Added GearmannExecuteWorkEvent

This event will be executed on each iteration of work by GearmanExecute.
The event includes the following information:

- The configurations for each job when ::runJob is called.
- Same for the remaining iterations, this decreases with the iterations on each event.
- The return code of the last work.

Changes:
* added GearmanExecuteWorkEvent class
* added ::setEventDispatcher to GearmanExecute
* added DI call on GearmanExecute to populate event dispatcher
* added gearman.execute.work constant to GearmanEvents
* dispatched gearman.execute.work from GearmanExecute

Tested locally without issue.
Hopefully BC is maintained, aside from the dependency on EventDispatcherInterface, this feature
can be made optional by checking if the dispatcher is null before attempting to call,
looking for second opinion.
orolin 11 年之前
父節點
當前提交
087c018cf7
共有 3 個文件被更改,包括 121 次插入2 次删除
  1. 90 0
      Event/GearmanExecuteWorkEvent.php
  2. 1 0
      Resources/config/services.yml
  3. 30 2
      Service/GearmanExecute.php

+ 90 - 0
Event/GearmanExecuteWorkEvent.php

@@ -0,0 +1,90 @@
+<?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>
+ */
+
+namespace Mmoreram\GearmanBundle\Event;
+
+use Symfony\Component\EventDispatcher\Event;
+use GearmanWorker;
+
+/**
+ * GearmanExecuteWorkEvent
+ *
+ * @since 2.4.2
+ */
+class GearmanExecuteWorkEvent extends Event
+{
+    /**
+     * @var array
+     *
+     * Gearman jobs running
+     */
+    protected $jobs;
+
+    /**
+     * @var int
+     *
+     * Remaining iterations on work
+     */
+    protected $iterationsRemaining;
+
+    /**
+     * @var int
+     *
+     * Return code from last ran job
+     */
+    protected $returnCode;
+
+    /**
+     * Construct method
+     *
+     * @param array $jobs
+     * @param int $iterationsRemaining
+     * @param int $returnCode
+     */
+    public function __construct(array $jobs, $iterationsRemaining, $returnCode)
+    {
+        $this->jobs = $jobs;
+        $this->iterationsRemaining = $iterationsRemaining;
+        $this->returnCode = $returnCode;
+    }
+
+    /**
+     * Get Gearman Work subscribed jobs
+     *
+     * @return array Subscribed jobs
+     */
+    public function getJobs()
+    {
+        return $this->jobs;
+    }
+
+    /**
+     * Get Gearman Work remaining iteration length
+     *
+     * @return int Remaining iterations
+     */
+    public function getIterationsRemaining()
+    {
+        return $this->iterationsRemaining;
+    }
+
+    /**
+     * Get Gearman Job return code
+     *
+     * @return int Last return code
+     */
+    public function getReturnCode()
+    {
+        return $this->returnCode;
+    }
+}

+ 1 - 0
Resources/config/services.yml

@@ -41,6 +41,7 @@ services:
         parent: gearman.abstract.service
         calls:
             - [setContainer,  [@service_container]]
+            - [setEventDispatcher, [@event_dispatcher]]
 
     gearman:
         class: %gearman.client.class%

+ 30 - 2
Service/GearmanExecute.php

@@ -13,10 +13,13 @@
 
 namespace Mmoreram\GearmanBundle\Service;
 
+use Mmoreram\GearmanBundle\Event\GearmanExecuteWorkEvent;
+use Mmoreram\GearmanBundle\GearmanEvents;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
 
 use Mmoreram\GearmanBundle\Service\Abstracts\AbstractGearmanService;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 
 /**
  * Gearman execute methods. All Worker methods
@@ -32,6 +35,13 @@ class GearmanExecute extends AbstractGearmanService
      */
     private $container;
 
+    /**
+     * @var EventDispatcherInterface
+     *
+     * EventDispatcher instance
+     */
+    private $eventDispatcher;
+
     /**
      * Set container
      *
@@ -41,8 +51,21 @@ class GearmanExecute extends AbstractGearmanService
      */
     public function setContainer(ContainerInterface $container)
     {
-
         $this->container = $container;
+
+        return $this;
+    }
+
+    /**
+     * @param EventDispatcherInterface $eventDispatcher
+     *
+     * @return GearmanExecute self Object
+     */
+    public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
+    {
+        $this->eventDispatcher = $eventDispatcher;
+
+        return $this;
     }
 
     /**
@@ -159,6 +182,11 @@ class GearmanExecute extends AbstractGearmanService
          */
         while ($gearmanWorker->work()) {
 
+            $iterations = $iterations - 1;
+
+            $event = new GearmanExecuteWorkEvent($jobs, $iterations, $gearmanWorker->returnCode());
+            $this->eventDispatcher->dispatch(GearmanEvents::GEARMAN_EXECUTE_WORK, $event);
+
             if ($gearmanWorker->returnCode() != GEARMAN_SUCCESS) {
 
                 break;
@@ -168,7 +196,7 @@ class GearmanExecute extends AbstractGearmanService
              * Only finishes its execution if alive is false and iterations
              * arrives to 0
              */
-            if (!$alive && --$iterations <= 0) {
+            if (!$alive && $iterations <= 0) {
 
                 break;
             }