瀏覽代碼

Merge pull request #140 from sivolobov/managing_context_in_callbacks

Managing context in callbacks
Marc Morera 10 年之前
父節點
當前提交
1f082509ec

+ 4 - 1
Dispatcher/GearmanCallbacksDispatcher.php

@@ -90,9 +90,12 @@ class GearmanCallbacksDispatcher extends AbstractGearmanDispatcher
      *
      * @see http://www.php.net/manual/en/gearmanclient.setcompletecallback.php
      */
-    public function assignCompleteCallback(GearmanTask $gearmanTask)
+    public function assignCompleteCallback(GearmanTask $gearmanTask, $contextReference = null)
     {
         $event = new GearmanClientCallbackCompleteEvent($gearmanTask);
+        if (!is_null($contextReference)) {
+            $event->setContext($contextReference);
+        }
         $this->eventDispatcher->dispatch(
             GearmanEvents::GEARMAN_CLIENT_CALLBACK_COMPLETE,
             $event

+ 23 - 0
Event/Abstracts/AbstractGearmanClientTaskEvent.php

@@ -28,6 +28,13 @@ abstract class AbstractGearmanClientTaskEvent extends Event
      */
     protected $gearmanTask;
 
+    /**
+     * @var mixed
+     *
+     * Context that can be set in the addTask method
+     */
+    protected $context;
+
     /**
      * Construct method
      *
@@ -47,4 +54,20 @@ abstract class AbstractGearmanClientTaskEvent extends Event
     {
         return $this->gearmanTask;
     }
+
+    /**
+     * @param mixed $context Context that can be set in the addTask method
+     */
+    public function setContext($context)
+    {
+        $this->context = &$context['context'];
+    }
+
+    /**
+     * @return mixed Context that can be set in the addTask method
+     */
+    public function &getContext()
+    {
+        return $this->context;
+    }
 }

+ 3 - 3
Resources/docs/client.rst

@@ -60,9 +60,9 @@ Tasks
 .. code-block:: php
 
     $gearman
-        ->addTask('MmoreramerinoTestBundleServicesMyAcmeWorker~doSomething', 'value1')
-        ->addLowTask('MmoreramerinoTestBundleServicesMyAcmeWorker~doSomething', 'value2')
-        ->addHighBackgroundTask('MmoreramerinoTestBundleServicesMyAcmeWorker~doSomething', 'value3')
+        ->addTask('MmoreramerinoTestBundleServicesMyAcmeWorker~doSomething', 'value1', $context1)
+        ->addLowTask('MmoreramerinoTestBundleServicesMyAcmeWorker~doSomething', 'value2', $context2)
+        ->addHighBackgroundTask('MmoreramerinoTestBundleServicesMyAcmeWorker~doSomething', 'value3', $context3)
         ->runTasks();
 
 - addTask: Adds a task to be run in parallel with other tasks

+ 21 - 7
Resources/docs/kernel_events.rst

@@ -6,8 +6,10 @@ GearmanBundle transforms Gearman callbacks to Symfony2 kernel events.
 Complete Callback
 ~~~~~~~~~~~~~~~~~
 
-This event receives as a parameter an instance of `Mmoreram\GearmanBundle\Event\GearmanClientCallbackCompleteEvent` with one method `$event->getGearmanTask()`. This method returns an instance of `\GearmanTask`.
+This event receives as a parameter an instance of `Mmoreram\GearmanBundle\Event\GearmanClientCallbackCompleteEvent` with methods `$event->getGearmanTask()` and `&$event->getContext()`.
+First method returns an instance of `\GearmanTask`.
 For more information about this GearmanEvent, read [GearmanClient::setCompleteCallback](http://www.php.net/manual/en/gearmanclient.setcompletecallback.php) documentation.
+The second method will return `$context` that you could add in the `addTask()` method.
 
 .. code-block:: yml
 
@@ -20,8 +22,10 @@ For more information about this GearmanEvent, read [GearmanClient::setCompleteCa
 Created Callback
 ~~~~~~~~~~~~~~~~
 
-This event receives as a parameter an instance of `Mmoreram\GearmanBundle\Event\GearmanClientCallbackCreatedEvent` with one method `$event->getGearmanTask()`. This method returns an instance of `\GearmanTask`.
+This event receives as a parameter an instance of `Mmoreram\GearmanBundle\Event\GearmanClientCallbackCreatedEvent` with methods `$event->getGearmanTask()` and `&$event->getContext()`.
+First method returns an instance of `\GearmanTask`.
 For more information about this GearmanEvent, read [GearmanClient::setCreatedCallback](http://www.php.net/manual/en/gearmanclient.setcreatedcallback.php) documentation.
+The second method will return `$context` that you could add in the `addTask()` method.
 
 .. code-block:: yml
 
@@ -34,8 +38,10 @@ For more information about this GearmanEvent, read [GearmanClient::setCreatedCal
 Data Callback
 ~~~~~~~~~~~~~
 
-This event receives as a parameter an instance of `Mmoreram\GearmanBundle\Event\GearmanClientCallbackDataEvent` with one method `$event->getGearmanTask()`. This method returns an instance of `\GearmanTask`.
+This event receives as a parameter an instance of `Mmoreram\GearmanBundle\Event\GearmanClientCallbackDataEvent` with methods `$event->getGearmanTask()` and `&$event->getContext()`.
+First method returns an instance of `\GearmanTask`.
 For more information about this GearmanEvent, read [GearmanClient::setDataCallback](http://www.php.net/manual/en/gearmanclient.setdatacallback.php) documentation.
+The second method will return `$context` that you could add in the `addTask()` method.
 
 .. code-block:: yml
 
@@ -62,8 +68,10 @@ For more information about this GearmanEvent, read [GearmanClient::setExceptionC
 Fail Callback
 ~~~~~~~~~~~~~
 
-This event receives as a parameter an instance of `Mmoreram\GearmanBundle\Event\GearmanClientCallbackFailEvent` with one method `$event->getGearmanTask()`. This method returns an instance of `\GearmanTask`.
+This event receives as a parameter an instance of `Mmoreram\GearmanBundle\Event\GearmanClientCallbackFailEvent` with methods `$event->getGearmanTask()` and `&$event->getContext()`.
+First method returns an instance of `\GearmanTask`.
 For more information about this GearmanEvent, read [GearmanClient::setFailCallback](http://www.php.net/manual/en/gearmanclient.setfailcallback.php) documentation.
+The second method will return `$context` that you could add in the `addTask()` method.
 
 .. code-block:: yml
 
@@ -76,8 +84,10 @@ For more information about this GearmanEvent, read [GearmanClient::setFailCallba
 Status Callback
 ~~~~~~~~~~~~~~~
 
-This event receives as a parameter an instance of `Mmoreram\GearmanBundle\Event\GearmanClientCallbackStatusEvent` with one method `$event->getGearmanTask()`. This method returns an instance of `\GearmanTask`.
+This event receives as a parameter an instance of `Mmoreram\GearmanBundle\Event\GearmanClientCallbackStatusEvent` with methods `$event->getGearmanTask()` and `&$event->getContext()`.
+First method returns an instance of `\GearmanTask`.
 For more information about this GearmanEvent, read [GearmanClient::setStatusCallback](http://www.php.net/manual/en/gearmanclient.setstatuscallback.php) documentation.
+The second method will return `$context` that you could add in the `addTask()` method.
 
 .. code-block:: yml
 
@@ -90,8 +100,10 @@ For more information about this GearmanEvent, read [GearmanClient::setStatusCall
 Warning Callback
 ~~~~~~~~~~~~~~~~
 
-This event receives as parameter an instance of `Mmoreram\GearmanBundle\Event\GearmanClientCallbackWarningEvent` with one method `$event->getGearmanTask()`. This method returns an instance of `\GearmanTask`.
+This event receives as parameter an instance of `Mmoreram\GearmanBundle\Event\GearmanClientCallbackWarningEvent` with methods `$event->getGearmanTask()` and `&$event->getContext()`.
+First method returns an instance of `\GearmanTask`.
 For more information about this GearmanEvent, read [GearmanClient::setWarningCallback](http://www.php.net/manual/en/gearmanclient.setwarningcallback.php) documentation.
+The second method will return `$context` that you could add in the `addTask()` method.
 
 .. code-block:: yml
 
@@ -104,8 +116,10 @@ For more information about this GearmanEvent, read [GearmanClient::setWarningCal
 Workload Callback
 ~~~~~~~~~~~~~~~~~
 
-This event receives as parameter an instance of `Mmoreram\GearmanBundle\Event\GearmanClientCallbackWorkloadEvent` with one method `$event->getGearmanTask()`. This method returns an instance of `\GearmanTask`.
+This event receives as parameter an instance of `Mmoreram\GearmanBundle\Event\GearmanClientCallbackWorkloadEvent` with methods `$event->getGearmanTask()` and `&$event->getContext()`.
+First method returns an instance of `\GearmanTask`.
 For more information about this GearmanEvent, read [GearmanClient::setWorkloadCallback](http://www.php.net/manual/en/gearmanclient.setworkloadcallback.php) documentation.
+The second method will return `$context` that you could add in the `addTask()` method.
 
 .. code-block:: yml
 

+ 3 - 3
Service/GearmanClient.php

@@ -595,13 +595,13 @@ class GearmanClient extends AbstractGearmanService
      *
      * @return GearmanClient Return this object
      */
-    protected function enqueueTask($name, $params, $context, $unique, $method)
+    protected function enqueueTask($name, $params, &$context, $unique, $method)
     {
-
+        $contextReference = array('context' => &$context);
         $task = array(
             'name'    => $name,
             'params'  => $params,
-            'context' => $context,
+            'context' => $contextReference,
             'unique'  => $this->uniqueJobIdentifierGenerator->generateUniqueKey($name, $params, $unique, $method),
             'method'  => $method,
         );