Explorar el Código

Merge pull request #122 from albenik/master

ref #121 Public access to native GearmanClient instance
Marc Morera hace 10 años
padre
commit
261b10a413
Se han modificado 1 ficheros con 26 adiciones y 3 borrados
  1. 26 3
      Service/GearmanClient.php

+ 26 - 3
Service/GearmanClient.php

@@ -26,6 +26,13 @@ use Mmoreram\GearmanBundle\Service\Abstracts\AbstractGearmanService;
  */
 class GearmanClient extends AbstractGearmanService
 {
+    /**
+     * @var \GearmanClient
+     *
+     * Gearman native client instance
+     */
+    protected $gearmanClient;
+
     /**
      * @var GearmanCallbacksDispatcher
      *
@@ -75,6 +82,16 @@ class GearmanClient extends AbstractGearmanService
      */
     protected $returnCode;
 
+    /**
+     * @return \GearmanClient
+     */
+    public function getNativeClient(){
+        if ($this->gearmanClient === null){
+            $this->gearmanClient = new \GearmanClient();
+        }
+        return $this->gearmanClient;
+    }
+
     /**
      * Init tasks structure
      *
@@ -229,12 +246,14 @@ class GearmanClient extends AbstractGearmanService
      */
     protected function doEnqueue(array $worker, $params, $method, $unique)
     {
-        $gearmanClient = new \GearmanClient();
+        $gearmanClient = $this->getNativeClient();
         $this->assignServers($gearmanClient);
 
         $result = $gearmanClient->$method($worker['job']['realCallableName'], $params, $unique);
         $this->returnCode = $gearmanClient->returnCode();
 
+        $this->gearmanClient = null;
+
         return $result;
     }
 
@@ -426,12 +445,14 @@ class GearmanClient extends AbstractGearmanService
      */
     public function getJobStatus($idJob)
     {
-        $gearmanClient = new \GearmanClient();
+        $gearmanClient = $this->getNativeClient();
         $this->assignServers($gearmanClient);
         $statusData = $gearmanClient->jobStatus($idJob);
 
         $jobStatus = new JobStatus($statusData);
 
+        $this->gearmanClient = null;
+
         return $jobStatus;
     }
 
@@ -642,7 +663,7 @@ class GearmanClient extends AbstractGearmanService
      */
     public function runTasks()
     {
-        $gearmanClient = new \GearmanClient();
+        $gearmanClient = $this->getNativeClient();
         $this->assignServers($gearmanClient);
 
         if ($this->settings['callbacks']) {
@@ -669,6 +690,8 @@ class GearmanClient extends AbstractGearmanService
 
         $this->initTaskStructure();
 
+        $this->gearmanClient = null;
+
         return $gearmanClient->runTasks();
     }
 }