Browse Source

Updated #50 changes.

* Method is called getJobStatus insetad of old one.
* Last method was returning "The job is known?" instead of real method responsability "Is job still running?"
* New method return all job status data wrapped by a class called JobStatus
* This class has needed getters
* Also tested
mmoreram 12 years ago
parent
commit
36e6933e6e

+ 145 - 0
Module/JobStatus.php

@@ -0,0 +1,145 @@
+<?php
+
+/**
+ * Gearman Bundle for Symfony2
+ * 
+ * @author Marc Morera <yuhu@mmoreram.com>
+ * @since 2013
+ */
+
+namespace Mmoreram\GearmanBundle\Module;
+
+/**
+ * Job status class
+ */
+class JobStatus
+{
+
+    /**
+     * @var boolean 
+     * 
+     * Job is known
+     */
+    private $known;
+
+
+    /**
+     * @var boolean
+     * 
+     * Job is running
+     */
+    private $running;
+
+
+    /**
+     * @var Integer
+     * 
+     * Job completition
+     */
+    private $completed;
+
+
+    /**
+     * @var integer
+     * 
+     * Job completition total
+     */
+    private $completionTotal;
+
+
+    /**
+     * Construct method
+     * 
+     * @param array $response Response to parse
+     */
+    public function __construct(array $response)
+    {
+        $this->known = ( isset($response[0]) && $response[0] );
+        $this->running = ( isset($response[1]) && $response[1] );
+
+        $this->completed    = ( isset($response[2]) && !$response[2] )
+                            ? 0
+                            : $response[2];
+
+        $this->completionTotal  = ( isset($response[3]) && !$response[3] )
+                                ? 0
+                                : $response[3];
+    }
+
+
+    /**
+     * Return if job is known
+     * 
+     * @return boolean Job is still known
+     */
+    public function isKnown()
+    {
+        return $this->known;
+    }
+
+
+    /**
+     * Return if job is still running
+     * 
+     * @return boolean Jon is still running
+     */
+    public function isRunning()
+    {
+        return $this->running;
+    }
+
+
+    /**
+     * Return completed value
+     * 
+     * @return integer Completed
+     */
+    public function getCompleted()
+    {
+        return $this->completed;
+    }
+
+
+    /**
+     * Return completition total
+     * 
+     * @return integer Completition total
+     */
+    public function getCompletionTotal()
+    {
+        return $this->completionTotal;
+    }
+
+
+    /**
+     * Return percent completed.
+     * 
+     * 0 is not started or not known
+     * 1 is finished
+     * Between 0 and 1 is in process. Value is a float
+     * 
+     * @return float Percent completed
+     */
+    public function getCompletionPercent()
+    {
+        $percent = 0;
+
+        if (($this->completed > 0) && ($this->completionTotal > 0)) {
+
+            $percent = $this->completed / $this->completionTotal;
+        }
+
+        return $percent;
+    }
+
+
+    /**
+     * Return if job is still running
+     * 
+     * @return boolean Jon is still running
+     */
+    public function isFinished()
+    {
+        return $this->isKnown() && !$this->isRunning() && $this->getCompletionPercent() == 1;
+    }
+}

+ 17 - 7
README.md

@@ -426,6 +426,23 @@ The difference between them is that an instance of a worker can execute any of t
 > You can have as many as worker instances as you want.  
 > You can have as many as worker instances as you want.  
 > Get some [Supervisord](http://supervisord.org/) info
 > Get some [Supervisord](http://supervisord.org/) info
 
 
+## Request Job Status
+
+With the Handle given if requesting a background job you can request the status of the job. The Method returns a JobStatus object placed in `Mmoreram\GearmanBundle\Module\JobStatus'
+
+    $jobStatus = $gearman->getJobStatus($result);
+    $jobIsKnown = $jobStatus->isKnown();
+    $jobIsRunning = $jobStatus->isRunning();
+    $jobIsFinished = $jobStatus->isFinished();
+
+    /**
+     * Also gives completition data
+     */
+     $completed = $jobStatus->getCompleted();
+     $completitionTotal = $jobStatus->getCompletitionTotal();
+     $completitionPercent = $jobStatus->getCompletionPercent(); 
+
+
 
 
 Client
 Client
 -----
 -----
@@ -486,13 +503,6 @@ You can request a Job by using the gearman client.
 * addTaskLowBackground: Add a low priority background task to be run in parallel
 * addTaskLowBackground: Add a low priority background task to be run in parallel
 * runTasks: Run a list of tasks in parallel
 * runTasks: Run a list of tasks in parallel
 
 
-## Request Job Status
-
-With the Handle given if requesting a job you can request the status of the job. The Method gives a boolean.
-
-    $result = $gearman
-        ->jobIsRunning($result);
-
 # Kernel Events
 # Kernel Events
 
 
 GearmanBundle transforms Gearman callbacks to Symfony2 kernel events.
 GearmanBundle transforms Gearman callbacks to Symfony2 kernel events.

+ 3 - 3
Service/GearmanCacheWrapper.php

@@ -261,16 +261,16 @@ class GearmanCacheWrapper
          * Every file found is parsed
          * Every file found is parsed
          */
          */
         foreach ($finder as $file) {
         foreach ($finder as $file) {
-            
+
             /**
             /**
              * File is checked to be parsed. Is just parsed if is a php file
              * File is checked to be parsed. Is just parsed if is a php file
              * Otherwise, jump to next file
              * Otherwise, jump to next file
              */
              */
             if ('php' !== strtolower($file->getExtension())) {
             if ('php' !== strtolower($file->getExtension())) {
-                
+
                 continue;
                 continue;
             }
             }
-            
+
             /**
             /**
              * File is accepted to be parsed
              * File is accepted to be parsed
              */
              */

+ 20 - 16
Service/GearmanClient.php

@@ -11,6 +11,7 @@ namespace Mmoreram\GearmanBundle\Service;
 
 
 use Mmoreram\GearmanBundle\Service\Abstracts\AbstractGearmanService;
 use Mmoreram\GearmanBundle\Service\Abstracts\AbstractGearmanService;
 use Mmoreram\GearmanBundle\GearmanMethods;
 use Mmoreram\GearmanBundle\GearmanMethods;
+use Mmoreram\GearmanBundle\Module\JobStatus;
 
 
 /**
 /**
  * GearmanClient. Implementation of AbstractGearmanService
  * GearmanClient. Implementation of AbstractGearmanService
@@ -351,6 +352,25 @@ class GearmanClient extends AbstractGearmanService
     }
     }
 
 
 
 
+    /**
+     * Fetches the Status of a special Background Job.
+     *
+     * @param string $idJob The job handle string
+     * 
+     * @return JobStatus Job status
+     */
+    public function getJobStatus($idJob)
+    {
+        $gearmanClient = new \GearmanClient();
+        $this->assignServers($gearmanClient);
+        $statusData = $gearmanClient->jobStatus($idJob);
+
+        $jobStatus = new JobStatus($statusData);
+
+        return $jobStatus;
+    }
+
+
     /**
     /**
      * Task methods
      * Task methods
      */
      */
@@ -546,20 +566,4 @@ class GearmanClient extends AbstractGearmanService
 
 
         return $gearmanClient->runTasks();
         return $gearmanClient->runTasks();
     }
     }
-
-    /**
-     * Fetches the Status of a special Job. You need the Job handle for the Task you want to check.
-     * As long as the Job is known by the Gearmen-Servers it will return a true
-     *
-     * @param string $backgroundId the job handle string
-     * @return bool
-     */
-    public function jobIsRunning($backgroundId)
-    {
-        $gearmanClient = new \GearmanClient();
-        $this->assignServers($gearmanClient);
-        $statusData = $gearmanClient->jobStatus($backgroundId);
-
-        return isset($statusData[0]) && $statusData[0];
-    }
 }
 }

+ 4 - 3
Tests/Module/JobClassTest.php

@@ -1,9 +1,10 @@
 <?php
 <?php
 
 
 /**
 /**
- * RSQueueBundle for Symfony2
- *
- * Marc Morera 2013
+ * Gearman Bundle for Symfony2
+ * 
+ * @author Marc Morera <yuhu@mmoreram.com>
+ * @since 2013
  */
  */
 
 
 namespace Mmoreram\GearmanBundle\Tests\Module;
 namespace Mmoreram\GearmanBundle\Tests\Module;

+ 102 - 0
Tests/Module/JobStatusTest.php

@@ -0,0 +1,102 @@
+<?php
+
+/**
+ * Gearman Bundle for Symfony2
+ * 
+ * @author Marc Morera <yuhu@mmoreram.com>
+ * @since 2013
+ */
+
+namespace Mmoreram\GearmanBundle\Tests\Module;
+
+use Mmoreram\GearmanBundle\Module\JobStatus;
+
+/**
+ * Tests JobStatusTest class
+ */
+class JobStatusTest extends \PHPUnit_Framework_TestCase
+{
+
+    /**
+     * Testing when job does not exist
+     */
+    public function testJobStatusNonExistant()
+    {
+        $jobStatus = new JobStatus(array(
+            false,
+            false,
+            null,
+            null
+        ));
+
+        $this->assertFalse($jobStatus->isKnown());
+        $this->assertFalse($jobStatus->isRunning());
+        $this->assertEquals($jobStatus->getCompleted(), 0);
+        $this->assertEquals($jobStatus->getCompletionTotal(), 0);
+        $this->assertEquals($jobStatus->getCompletionPercent(), 0);
+        $this->assertFalse($jobStatus->isFinished());
+    }
+
+
+    /**
+     * Testing when job is started
+     */
+    public function testJobStatusStarted()
+    {
+        $jobStatus = new JobStatus(array(
+            true,
+            true,
+            0,
+            10
+        ));
+
+        $this->assertTrue($jobStatus->isKnown());
+        $this->assertTrue($jobStatus->isRunning());
+        $this->assertEquals($jobStatus->getCompleted(), 0);
+        $this->assertEquals($jobStatus->getCompletionTotal(), 10);
+        $this->assertEquals($jobStatus->getCompletionPercent(), 0);
+        $this->assertFalse($jobStatus->isFinished());
+    }
+
+
+    /**
+     * Testing when job is still running
+     */
+    public function testJobStatusRunning()
+    {
+        $jobStatus = new JobStatus(array(
+            true,
+            true,
+            5,
+            10
+        ));
+
+        $this->assertTrue($jobStatus->isKnown());
+        $this->assertTrue($jobStatus->isRunning());
+        $this->assertEquals($jobStatus->getCompleted(), 5);
+        $this->assertEquals($jobStatus->getCompletionTotal(), 10);
+        $this->assertEquals($jobStatus->getCompletionPercent(), 0.5);
+        $this->assertFalse($jobStatus->isFinished());
+    }
+
+
+    /**
+     * Testing when job is already finished
+     */
+    public function testJobStatusFinished()
+    {
+        $jobStatus = new JobStatus(array(
+            true,
+            false,
+            10,
+            10
+        ));
+
+        $this->assertTrue($jobStatus->isKnown());
+        $this->assertFalse($jobStatus->isRunning());
+        $this->assertEquals($jobStatus->getCompleted(), 10);
+        $this->assertEquals($jobStatus->getCompletionTotal(), 10);
+        $this->assertEquals($jobStatus->getCompletionPercent(), 1);
+        $this->assertTrue($jobStatus->isFinished());
+    }
+}

+ 4 - 3
Tests/Module/WorkerClassTest.php

@@ -1,9 +1,10 @@
 <?php
 <?php
 
 
 /**
 /**
- * RSQueueBundle for Symfony2
- *
- * Marc Morera 2013
+ * Gearman Bundle for Symfony2
+ * 
+ * @author Marc Morera <yuhu@mmoreram.com>
+ * @since 2013
  */
  */
 
 
 namespace Mmoreram\GearmanBundle\Tests\Module;
 namespace Mmoreram\GearmanBundle\Tests\Module;