Browse Source

Fixed issues with GearmanWorkExecutedEvent feature

* Changed GearmanExecuteWorkEvent to GearmanWorkExecutedEvent.
* Fixed doc blocks on event class.
* Fixed documentation number of methods reference.
* Fixed missing constant on GearmanEvents.
* Made $eventListener property protected.
* Updated documentation with example of when you would use this event.
orolin 11 years ago
parent
commit
7a4af2833d

+ 90 - 0
Event/GearmanWorkExecutedEvent.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>
+ * @author Dominic Grostate <codekestrel@googlemail.com>
+ */
+
+namespace Mmoreram\GearmanBundle\Event;
+
+use Symfony\Component\EventDispatcher\Event;
+
+/**
+ * GearmanWorkExecutedEvent
+ *
+ * @since 2.4.2
+ */
+class GearmanWorkExecutedEvent 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 Jobs
+     * @param int   $iterationsRemaining Iterations Remaining
+     * @param int   $returnCode Return code
+     */
+    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;
+    }
+}

+ 9 - 0
GearmanEvents.php

@@ -106,4 +106,13 @@ class GearmanEvents
      * @var string
      */
     const GEARMAN_CLIENT_CALLBACK_WORKLOAD = 'gearman.client.callback.workload';
+
+    /**
+     * Sets a function to be called when a worker has completed a job.
+     *
+     * This will be fired by the worker after completion of a job before preparing to start another work cycle.
+     *
+     * @var string
+     */
+    const GEARMAN_WORK_EXECUTED = 'gearman.work.executed';
 }

+ 2 - 2
Resources/docs/kernel_events.rst

@@ -118,7 +118,7 @@ For more information about this GearmanEvent, read [GearmanClient::setWorkloadCa
 Execute Work Event
 ~~~~~~~~~~~~~~~~~~
 
-This event receives as parameter an instanceof `Mmoreram\GearmanBundle\Event\GearmanExecuteWorkEvent` with four methods:
+This event receives as parameter an instanceof `Mmoreram\GearmanBundle\Event\GearmanWorkExecutedEvent` with three methods:
 `$event->getJobs()` returns the configuration of the jobs,
 `$event->getIterationsRemaining()` returns the remaining iterations for these jobs,
 `$event->getReturnCode()` returns the return code of the last executed job.
@@ -131,4 +131,4 @@ This event is dispatched after a job has been completed.  After this event is co
         my_event_listener:
             class: AcmeBundle\EventListener\MyEventListener
             tags:
-              - { name: kernel.event_listener, event: gearman.execute.work, method: onWork }
+              - { name: kernel.event_listener, event: gearman.work.executed, method: onWorkExecuted }

+ 7 - 7
Service/GearmanExecute.php

@@ -13,13 +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 Symfony\Component\EventDispatcher\EventDispatcherInterface;
 
 use Mmoreram\GearmanBundle\Service\Abstracts\AbstractGearmanService;
-use Symfony\Component\EventDispatcher\EventDispatcherInterface;
+use Mmoreram\GearmanBundle\Event\GearmanWorkExecutedEvent;
+use Mmoreram\GearmanBundle\GearmanEvents;
 
 /**
  * Gearman execute methods. All Worker methods
@@ -40,7 +40,7 @@ class GearmanExecute extends AbstractGearmanService
      *
      * EventDispatcher instance
      */
-    private $eventDispatcher;
+    protected $eventDispatcher;
 
     /**
      * Set container
@@ -182,10 +182,10 @@ class GearmanExecute extends AbstractGearmanService
          */
         while ($gearmanWorker->work()) {
 
-            $iterations = $iterations - 1;
+            $iterations--;
 
-            $event = new GearmanExecuteWorkEvent($jobs, $iterations, $gearmanWorker->returnCode());
-            $this->eventDispatcher->dispatch(GearmanEvents::GEARMAN_EXECUTE_WORK, $event);
+            $event = new GearmanWorkExecutedEvent($jobs, $iterations, $gearmanWorker->returnCode());
+            $this->eventDispatcher->dispatch(GearmanEvents::GEARMAN_WORK_EXECUTED, $event);
 
             if ($gearmanWorker->returnCode() != GEARMAN_SUCCESS) {