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

dispatch starting event on new job

David Moreau 10 роки тому
батько
коміт
e93b010fd7
2 змінених файлів з 14 додано та 2 видалено
  1. 7 1
      Service/GearmanExecute.php
  2. 7 1
      Tests/Service/GearmanExecuteTest.php

+ 7 - 1
Service/GearmanExecute.php

@@ -21,6 +21,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 
 use Mmoreram\GearmanBundle\Command\Util\GearmanOutputAwareInterface;
 use Mmoreram\GearmanBundle\Event\GearmanWorkExecutedEvent;
+use Mmoreram\GearmanBundle\Event\GearmanWorkStartingEvent;
 use Mmoreram\GearmanBundle\GearmanEvents;
 use Mmoreram\GearmanBundle\Service\Abstracts\AbstractGearmanService;
 
@@ -211,6 +212,7 @@ class GearmanExecute extends AbstractGearmanService
                 array(
                     'job_object_instance' => $objInstance,
                     'job_method' => $job['methodName'],
+                    'jobs' => $jobs
                 )
             );
         }
@@ -301,8 +303,12 @@ class GearmanExecute extends AbstractGearmanService
             || !array_key_exists('job_object_instance', $context)
             || !array_key_exists('job_method', $context)
         ) {
-            
+            throw new \InvalidArgumentException('$context shall be an array with job_object_instance and job_method key.');
         }
+
+        $event = new GearmanWorkStartingEvent($context['jobs']);
+        $this->eventDispatcher->dispatch(GearmanEvents::GEARMAN_WORK_STARTING, $event);
+
         $result = call_user_func_array(
             array($context['job_object_instance'], $context['job_method']),
             array($job, $context)

+ 7 - 1
Tests/Service/GearmanExecuteTest.php

@@ -79,9 +79,13 @@ class GearmanExecuteTest extends WebTestCase
             ->willReturn($workers);
 
         // Prepare a dispatcher to listen to tested events
+        $startingFlag = false;
         $executedFlag = false;
 
         $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
+        $dispatcher->addListener(GearmanEvents::GEARMAN_WORK_STARTING, function() use (&$startingFlag){
+            $startingFlag = true;
+        });
         $dispatcher->addListener(GearmanEvents::GEARMAN_WORK_EXECUTED, function() use (&$executedFlag){
             $executedFlag = true;
         });
@@ -98,7 +102,8 @@ class GearmanExecuteTest extends WebTestCase
         $worker->method('work')->will($this->returnCallback(function() use ($service, $object){
             $service->handleJob(new \GearmanJob(), array(
                 'job_object_instance' => $object,
-                'job_method' => 'myMethod'
+                'job_method' => 'myMethod',
+                'jobs' => array()
             ));
             return true;
         }));
@@ -107,6 +112,7 @@ class GearmanExecuteTest extends WebTestCase
         $service->executeJob('test', $worker);
 
         // Do we have the events ?
+        $this->assertTrue($startingFlag);
         $this->assertTrue($executedFlag);
     }
 }